Improve moviment !
This commit is contained in:
+1
-1
@@ -26,7 +26,7 @@ export const SEA_LEVEL = 64;
|
||||
export const CHUNK_AREA = CHUNK_SIZE * CHUNK_SIZE;
|
||||
|
||||
// the player's collision box, position is the middle of its feet
|
||||
export const PLAYER_WIDTH = 0.55;
|
||||
export const PLAYER_WIDTH = 0.6;
|
||||
export const PLAYER_HEIGHT = 1.79;
|
||||
export const PLAYER_EYE_HEIGHT = 1.69;
|
||||
|
||||
|
||||
+98
-10
@@ -26,10 +26,20 @@ export interface Collisions {
|
||||
z: number;
|
||||
}
|
||||
|
||||
// falls and moves a body by its velocity for one tick. like minecraft it moves along y, then x, then z, 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
|
||||
export function move_body(body: Body, gravity: number, is_solid: (x: number, y: number, z: number) => boolean) {
|
||||
// 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;
|
||||
@@ -38,12 +48,15 @@ export function move_body(body: Body, gravity: number, is_solid: (x: number, y:
|
||||
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];
|
||||
const moved = [0, 0, 0];
|
||||
for (const axis of [1, 0, 2]) {
|
||||
const distance = clip(min, max, axis, wanted[axis], is_solid);
|
||||
min[axis] += distance;
|
||||
max[axis] += distance;
|
||||
moved[axis] = distance;
|
||||
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;
|
||||
@@ -58,6 +71,44 @@ export function move_body(body: Body, gravity: number, is_solid: (x: number, y:
|
||||
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[],
|
||||
@@ -94,3 +145,40 @@ function clip(
|
||||
}
|
||||
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];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user