Files
bworld/common/physics.ts
T
2026-10-01 21:12:30 -03:00

215 lines
7.7 KiB
TypeScript

// collision against blocks, shared by the client's entities and the server's, so both move things the same way
import { TICK_DELTA } from "./constants.ts";
// how far inside a block face still counts as touching it, so boxes resting exactly on a face don't snag
const EPSILON = 1e-7;
// the two axes that aren't the one being moved along
const OTHER_AXES = [[1, 2], [0, 2], [0, 1]] as const;
// something with a box that moves. position is the middle of its feet, velocity is in blocks per second
export interface Body {
x: number;
y: number;
z: number;
vx: number;
vy: number;
vz: number;
// width is used for both x and z
width: number;
height: number;
}
// which way the body hit something on each axis: 1 or -1, 0 for nothing. 1 on y means it landed on something
export interface Collisions {
x: number;
y: number;
z: number;
}
// falls and moves a body by its velocity for one tick. like minecraft it moves along y first, then along whichever
// of x and z it's moving more on, so walking diagonally past a corner slides along it instead of snagging. each time
// only as far as it can before touching a block, and stops its velocity on the axes it hit something.
// blocks it's already inside don't stop it, so it can get out of them.
// with a step height, a body that was on the ground (or lands during this move) and walks into something at most
// that tall steps up onto it, like minecraft's Entity.collide. that also covers the tick it starts to fall: it has
// already dropped a little, so the next block's top is a small step up instead of a wall to slide down
export function move_body(
body: Body,
gravity: number,
is_solid: (x: number, y: number, z: number) => boolean,
step_height = 0,
on_ground = false,
) {
// the average of this tick's start and end speed, so the arc is the same at any tick rate
const wanted_y = (body.vy + gravity * TICK_DELTA / 2) * TICK_DELTA;
body.vy += gravity * TICK_DELTA;
const wanted = [body.vx * TICK_DELTA, wanted_y, body.vz * TICK_DELTA];
const half = body.width / 2;
const min = [body.x - half, body.y, body.z - half];
const max = [body.x + half, body.y + body.height, body.z + half];
let moved = sweep([...min], [...max], wanted, [1, ...horizontal_order(wanted)], is_solid);
const blocked_sideways = moved[0] !== wanted[0] || moved[2] !== wanted[2];
const landed = moved[1] !== wanted[1] && wanted[1] < 0;
if (step_height > 0 && blocked_sideways && (on_ground || landed)) {
const stepped = step_up([...min], [...max], wanted, step_height, is_solid);
if (stepped[0] ** 2 + stepped[2] ** 2 > moved[0] ** 2 + moved[2] ** 2) {
moved = stepped;
}
}
const hit = (axis: number) => moved[axis] !== wanted[axis] ? -Math.sign(wanted[axis]) : 0;
const collisions: Collisions = { x: hit(0), y: hit(1), z: hit(2) };
if (collisions.x !== 0) body.vx = 0;
if (collisions.y !== 0) body.vy = 0;
if (collisions.z !== 0) body.vz = 0;
body.x += moved[0];
body.y += moved[1];
body.z += moved[2];
return collisions;
}
// x then z, or z then x when it's moving more along z
function horizontal_order(wanted: number[]) {
return Math.abs(wanted[0]) < Math.abs(wanted[2]) ? [2, 0] : [0, 2];
}
// moves the box along each axis in turn, as far as it can. returns how far it went on each
function sweep(
min: number[],
max: number[],
wanted: number[],
axes: number[],
is_solid: (x: number, y: number, z: number) => boolean,
) {
const moved = [0, 0, 0];
for (const axis of axes) {
const distance = clip(min, max, axis, wanted[axis], is_solid);
min[axis] += distance;
max[axis] += distance;
moved[axis] = distance;
}
return moved;
}
// up by the step height (or to the ceiling), across, then back down onto whatever it's standing on, as far as it
// would have gone down anyway
function step_up(
min: number[],
max: number[],
wanted: number[],
step_height: number,
is_solid: (x: number, y: number, z: number) => boolean,
) {
const up = sweep(min, max, [0, step_height, 0], [1], is_solid)[1];
const across = sweep(min, max, wanted, horizontal_order(wanted), is_solid);
const down = sweep(min, max, [0, wanted[1] - up, 0], [1], is_solid)[1];
return [across[0], up + down, across[2]];
}
// how far the box can go along an axis before it runs into a block
function clip(
min: number[],
max: number[],
axis: number,
distance: number,
is_solid: (x: number, y: number, z: number) => boolean,
) {
if (distance === 0) {
return 0;
}
const [a, b] = OTHER_AXES[axis];
const from = Math.floor(Math.min(min[axis], min[axis] + distance));
const to = Math.floor(Math.max(max[axis], max[axis] + distance));
const position = [0, 0, 0];
for (let i = from; i <= to; i++) {
for (let j = Math.floor(min[a] + EPSILON); j <= Math.floor(max[a] - EPSILON); j++) {
for (let k = Math.floor(min[b] + EPSILON); k <= Math.floor(max[b] - EPSILON); k++) {
position[axis] = i;
position[a] = j;
position[b] = k;
if (!is_solid(position[0], position[1], position[2])) {
continue;
}
if (distance > 0 && i >= max[axis] - EPSILON) {
distance = Math.min(distance, i - max[axis]);
} else if (distance < 0 && i + 1 <= min[axis] + EPSILON) {
distance = Math.max(distance, i + 1 - min[axis]);
}
}
}
}
return distance;
}
// how far a sneaking body on the ground can move this tick without walking off an edge, like minecraft's
// Player.maybeBackOffFromEdge: each axis is cut back until there's something within `drop` below the moved box
export function back_off_from_edge(
body: Body,
dx: number,
dz: number,
drop: number,
is_solid: (x: number, y: number, z: number) => boolean,
): [number, number] {
const step = 0.05;
const toward_zero = (d: number) => Math.abs(d) <= step ? 0 : d - Math.sign(d) * step;
const falls = (mx: number, mz: number) => {
const half = body.width / 2;
const min = [body.x - half + mx, body.y - drop, body.z - half + mz];
const max = [body.x + half + mx, body.y, body.z + half + mz];
return !overlaps_solid(min, max, is_solid);
};
while (dx !== 0 && falls(dx, 0)) dx = toward_zero(dx);
while (dz !== 0 && falls(0, dz)) dz = toward_zero(dz);
while (dx !== 0 && dz !== 0 && falls(dx, dz)) {
dx = toward_zero(dx);
dz = toward_zero(dz);
}
return [dx, dz];
}
// whether the body's box, moved by dx, dy and dz, would touch nothing blocked, like minecraft's Entity.isFree
export function box_is_free(
body: Body,
dx: number,
dy: number,
dz: number,
is_blocked: (x: number, y: number, z: number) => boolean,
): boolean {
const half = body.width / 2;
const min = [body.x - half + dx, body.y + dy, body.z - half + dz];
const max = [body.x + half + dx, body.y + body.height + dy, body.z + half + dz];
return !overlaps_solid(min, max, is_blocked);
}
function overlaps_solid(min: number[], max: number[], is_solid: (x: number, y: number, z: number) => boolean) {
for (let x = Math.floor(min[0] + EPSILON); x <= Math.floor(max[0] - EPSILON); x++) {
for (let y = Math.floor(min[1] + EPSILON); y <= Math.floor(max[1] - EPSILON); y++) {
for (let z = Math.floor(min[2] + EPSILON); z <= Math.floor(max[2] - EPSILON); z++) {
if (is_solid(x, y, z)) return true;
}
}
}
return false;
}
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in liquid
export function in_water(body: Body, is_liquid: (x: number, y: number, z: number) => boolean): boolean {
const x = Math.floor(body.x);
const z = Math.floor(body.z);
return is_liquid(x, Math.floor(body.y), z) || is_liquid(x, Math.floor(body.y + body.height / 2), z);
}
// its eyes are in liquid, so it can't breathe
export function eyes_in_water(
body: Body,
eye_height: number,
is_liquid: (x: number, y: number, z: number) => boolean,
): boolean {
return is_liquid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
}