import { world, system } from "@minecraft/server";
const off = { x: -0.75, y: 0, z: -3 };
const stp = 0.1;
const min = 0.5;
const max = 3.5;
const ez = 0.1;
function gdv(rot) {
const yaw = rot.y * (Math.PI / 180);
const pit = rot.x * (Math.PI / 180);
const fwd = {
x: -Math.sin(yaw) * Math.cos(pit),
y: -Math.sin(pit),
z: Math.cos(yaw) * Math.cos(pit)
};
const up = { x: 0, y: 1, z: 0 };
const cx = up.y * fwd.z - up.z * fwd.y;
const cy = up.z * fwd.x - up.x * fwd.z;
const cz = up.x * fwd.y - up.y * fwd.x;
const len = Math.sqrt(cx*cx + cy*cy + cz*cz);
const rgt = {
x: cx / len,
y: cy / len,
z: cz / len
};
return { fwd, rgt };
}
function fsp(plr, eye, dir, tgt) {
const dv = {
x: tgt.x - eye.x,
y: tgt.y - eye.y,
z: tgt.z - eye.z
};
const dst = Math.sqrt(dv.x**2 + dv.y**2 + dv.z**2);
if (dst < 0.01) return eye;
const dn = {
x: dv.x / dst,
y: dv.y / dst,
z: dv.z / dst
};
let lst = eye;
const cd = Math.min(dst, max);
for (let d = min; d <= cd; d += stp) {
const cp = {
x: eye.x + dn.x * d,
y: eye.y + dn.y * d,
z: eye.z + dn.z * d
};
const blk = plr.dimension.getBlock(cp);
if (blk && blk.typeId !== "minecraft:air") {
return {
x: eye.x + dn.x * (d - stp),
y: eye.y + dn.y * (d - stp),
z: eye.z + dn.z * (d - stp)
};
}
lst = cp;
}
return lst;
}
system.runInterval(() => {
const plrs = world.getPlayers({ tags: [ "v" ] });
for (const plr of plrs) {
try {
const eye = plr.getHeadLocation();
const rot = plr.getRotation();
const { fwd, rgt } = gdv(rot);
const tgt = {
x: eye.x + rgt.x * off.x + fwd.x * off.z,
y: eye.y + rgt.y * off.x + fwd.y * off.z,
z: eye.z + rgt.z * off.x + fwd.z * off.z
};
const saf = fsp(
plr,
eye,
{ x: tgt.x - eye.x, y: tgt.y - eye.y, z: tgt.z - eye.z },
tgt
);
plr.runCommand(
`camera @s set minecraft:free ease ${ez} linear ` +
`pos ${saf.x.toFixed(2)} ${saf.y.toFixed(2)} ${saf.z.toFixed(2)} ` +
`rot ${rot.x} ${rot.y}`
);
} catch (e) {
console.warn("Camera error: " + e);
}
}
}); Post #78
104
Обход препятствий камеры