Better block models !

This commit is contained in:
2026-10-03 15:57:47 -03:00
parent ecc0295f6d
commit 24fe7cdede
23 changed files with 678 additions and 122 deletions
+41 -16
View File
@@ -1,11 +1,20 @@
// 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";
import { EMPTY_SHAPE, FULL_SHAPE, type Shape } from "./block_shapes.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;
// what a body bumps into at a block: its collision shape (see common/block_shapes.ts), or true for a full block and
// false for nothing
export type BlockCollision = (x: number, y: number, z: number) => boolean | Shape;
function shape_of(collision: boolean | Shape): Shape {
return collision === true ? FULL_SHAPE : collision === false ? EMPTY_SHAPE : collision;
}
// something with a box that moves. position is the middle of its feet, velocity is in blocks per second
export interface Body {
x: number;
@@ -36,7 +45,7 @@ export interface Collisions {
export function move_body(
body: Body,
gravity: number,
is_solid: (x: number, y: number, z: number) => boolean,
is_solid: BlockCollision,
step_height = 0,
on_ground = false,
) {
@@ -82,7 +91,7 @@ function sweep(
max: number[],
wanted: number[],
axes: number[],
is_solid: (x: number, y: number, z: number) => boolean,
is_solid: BlockCollision,
) {
const moved = [0, 0, 0];
for (const axis of axes) {
@@ -101,7 +110,7 @@ function step_up(
max: number[],
wanted: number[],
step_height: number,
is_solid: (x: number, y: number, z: number) => boolean,
is_solid: BlockCollision,
) {
const up = sweep(min, max, [0, step_height, 0], [1], is_solid)[1];
const across = sweep(min, max, wanted, horizontal_order(wanted), is_solid);
@@ -109,13 +118,13 @@ function step_up(
return [across[0], up + down, across[2]];
}
// how far the box can go along an axis before it runs into a block
// how far the box can go along an axis before it runs into a block's shape
function clip(
min: number[],
max: number[],
axis: number,
distance: number,
is_solid: (x: number, y: number, z: number) => boolean,
is_solid: BlockCollision,
) {
if (distance === 0) {
return 0;
@@ -132,13 +141,21 @@ function clip(
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]);
for (const box of shape_of(is_solid(position[0], position[1], position[2]))) {
// only boxes beside the body on the other two axes are in the way
if (
position[a] + box[a] >= max[a] - EPSILON || position[a] + box[a + 3] <= min[a] + EPSILON ||
position[b] + box[b] >= max[b] - EPSILON || position[b] + box[b + 3] <= min[b] + EPSILON
) {
continue;
}
const box_min = i + box[axis];
const box_max = i + box[axis + 3];
if (distance > 0 && box_min >= max[axis] - EPSILON) {
distance = Math.min(distance, box_min - max[axis]);
} else if (distance < 0 && box_max <= min[axis] + EPSILON) {
distance = Math.max(distance, box_max - min[axis]);
}
}
}
}
@@ -153,7 +170,7 @@ export function back_off_from_edge(
dx: number,
dz: number,
drop: number,
is_solid: (x: number, y: number, z: number) => boolean,
is_solid: BlockCollision,
): [number, number] {
const step = 0.05;
const toward_zero = (d: number) => Math.abs(d) <= step ? 0 : d - Math.sign(d) * step;
@@ -178,7 +195,7 @@ export function box_is_free(
dx: number,
dy: number,
dz: number,
is_blocked: (x: number, y: number, z: number) => boolean,
is_blocked: BlockCollision,
): boolean {
const half = body.width / 2;
const min = [body.x - half + dx, body.y + dy, body.z - half + dz];
@@ -186,11 +203,19 @@ export function box_is_free(
return !overlaps_solid(min, max, is_blocked);
}
function overlaps_solid(min: number[], max: number[], is_solid: (x: number, y: number, z: number) => boolean) {
function overlaps_solid(min: number[], max: number[], is_solid: BlockCollision) {
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;
for (const box of shape_of(is_solid(x, y, z))) {
if (
x + box[0] < max[0] - EPSILON && x + box[3] > min[0] + EPSILON &&
y + box[1] < max[1] - EPSILON && y + box[4] > min[1] + EPSILON &&
z + box[2] < max[2] - EPSILON && z + box[5] > min[2] + EPSILON
) {
return true;
}
}
}
}
}