This commit is contained in:
2026-10-03 16:28:09 -03:00
parent 24fe7cdede
commit 91655c28c5
53 changed files with 1752 additions and 114 deletions
+13 -7
View File
@@ -1,6 +1,6 @@
// 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";
import { COLLISION_TOP, 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;
@@ -15,6 +15,12 @@ function shape_of(collision: boolean | Shape): Shape {
return collision === true ? FULL_SHAPE : collision === false ? EMPTY_SHAPE : collision;
}
// the lowest block whose shape can reach up to a box starting at min along an axis: on y that's one more below,
// since a collision shape can be taller than its block, like a fence's
function lowest(axis: number, min: number) {
return Math.floor(min + EPSILON) - (axis === 1 ? Math.ceil(COLLISION_TOP) - 1 : 0);
}
// something with a box that moves. position is the middle of its feet, velocity is in blocks per second
export interface Body {
x: number;
@@ -131,13 +137,13 @@ function clip(
}
const [a, b] = OTHER_AXES[axis];
const from = Math.floor(Math.min(min[axis], min[axis] + distance));
const from = lowest(axis, Math.min(min[axis], min[axis] + distance) - EPSILON);
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++) {
for (let j = lowest(a, min[a]); j <= Math.floor(max[a] - EPSILON); j++) {
for (let k = lowest(b, min[b]); k <= Math.floor(max[b] - EPSILON); k++) {
position[axis] = i;
position[a] = j;
position[b] = k;
@@ -204,9 +210,9 @@ export function box_is_free(
}
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++) {
for (let x = lowest(0, min[0]); x <= Math.floor(max[0] - EPSILON); x++) {
for (let y = lowest(1, min[1]); y <= Math.floor(max[1] - EPSILON); y++) {
for (let z = lowest(2, min[2]); z <= Math.floor(max[2] - EPSILON); z++) {
for (const box of shape_of(is_solid(x, y, z))) {
if (
x + box[0] < max[0] - EPSILON && x + box[3] > min[0] + EPSILON &&