Water phyisics

This commit is contained in:
2026-10-07 19:38:27 -03:00
parent d101fa2ef3
commit 78117f65c9
13 changed files with 338 additions and 13 deletions
+147 -1
View File
@@ -244,6 +244,152 @@ export function eyes_in_water(
return is_liquid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
}
// a liquid in a cell, for liquids pushing things along
export interface LiquidInCell {
// which liquid, its block's numeric id. a liquid's flow only goes by cells of the same one
id: number;
// its surface, 0 to 1 (see liquid_height), not counting more of it above
height: number;
}
// the liquid in a cell, undefined for none
export type LiquidAt = (x: number, y: number, z: number) => LiquidInCell | undefined;
const HORIZONTAL = [[1, 0], [-1, 0], [0, 1], [0, -1]];
// blocks per second added each tick, minecraft's water 0.014 blocks per tick per tick
const LIQUID_PUSH = 0.014 * 20;
// moving slower than this sideways, a push is at least LIQUID_MIN_PUSH, so things still drift off flat-ish water
const LIQUID_STILL_SPEED = 0.003 * 20;
const LIQUID_MIN_PUSH = 0.0045 * 20;
function blocks_motion(collision: boolean | Shape) {
return shape_of(collision).length > 0;
}
function normalized(x: number, y: number, z: number): [number, number, number] {
const length = Math.hypot(x, y, z);
return length < 1e-5 ? [0, 0, 0] : [x / length, y / length, z / length];
}
// which way the liquid at x y z flows, a length 1 vector or nothing, like minecraft's FlowingFluid.getFlow: towards
// lower cells of it beside it, and towards drops where it pours over an edge. falling down a wall it goes down too
export function liquid_flow(
x: number,
y: number,
z: number,
liquid_at: LiquidAt,
is_solid: BlockCollision,
): [number, number, number] {
const here = liquid_at(x, y, z);
if (!here) return [0, 0, 0];
let fx = 0;
let fz = 0;
for (const [dx, dz] of HORIZONTAL) {
const neighbor = liquid_at(x + dx, y, z + dz);
// another liquid doesn't change this one's flow
if (neighbor && neighbor.id !== here.id) continue;
let drop = 0;
if (neighbor) {
drop = here.height - neighbor.height;
} else if (!blocks_motion(is_solid(x + dx, y, z + dz))) {
// an edge it pours over, onto more of itself
const below = liquid_at(x + dx, y - 1, z + dz);
if (below?.id === here.id) drop = here.height - (below.height - 1);
}
fx += dx * drop;
fz += dz * drop;
}
[fx, , fz] = normalized(fx, 0, fz);
// falling: flowing, not a source, with more of it above. a deep pool of sources isn't
let fy = 0;
if (here.height < 1 && liquid_at(x, y + 1, z)?.id === here.id) {
const against_wall = HORIZONTAL.some(([dx, dz]) =>
blocks_motion(is_solid(x + dx, y, z + dz)) || blocks_motion(is_solid(x + dx, y + 1, z + dz))
);
if (against_wall) fy = -6;
}
return normalized(fx, fy, fz);
}
// liquid the body is in pushes it the way it flows, like minecraft's Entity.updateFluidHeightAndDoFluidPushing: the
// flow of every cell of it the box reaches, averaged, weaker where it's shallow. players get that average as it is,
// other things are pushed at full strength whichever way it points. changes the body's velocity
export function push_by_liquid(body: Body, liquid_at: LiquidAt, is_solid: BlockCollision, is_player: boolean) {
const half = body.width / 2 - 0.001;
const min_y = body.y + 0.001;
const max_y = body.y + body.height - 0.001;
let px = 0;
let py = 0;
let pz = 0;
let count = 0;
let depth = 0;
for (let x = Math.floor(body.x - half); x <= Math.floor(body.x + half); x++) {
for (let y = Math.floor(min_y); y <= Math.floor(max_y); y++) {
for (let z = Math.floor(body.z - half); z <= Math.floor(body.z + half); z++) {
const liquid = liquid_at(x, y, z);
if (!liquid) continue;
const surface = y + (liquid_at(x, y + 1, z)?.id === liquid.id ? 1 : liquid.height);
if (surface < min_y) continue;
depth = Math.max(depth, surface - min_y);
let [fx, fy, fz] = liquid_flow(x, y, z, liquid_at, is_solid);
// minecraft's, which goes by the deepest cell so far
if (depth < 0.4) {
fx *= depth;
fy *= depth;
fz *= depth;
}
px += fx;
py += fy;
pz += fz;
count++;
}
}
}
if (Math.hypot(px, py, pz) === 0) return;
[px, py, pz] = is_player ? [px / count, py / count, pz / count] : normalized(px, py, pz);
px *= LIQUID_PUSH;
py *= LIQUID_PUSH;
pz *= LIQUID_PUSH;
if (
Math.abs(body.vx) < LIQUID_STILL_SPEED && Math.abs(body.vz) < LIQUID_STILL_SPEED &&
Math.hypot(px, py, pz) < LIQUID_MIN_PUSH
) {
[px, py, pz] = normalized(px, py, pz).map((n) => n * LIQUID_MIN_PUSH);
}
body.vx += px;
body.vy += py;
body.vz += pz;
}
// how far the body's feet are under the surface of liquid it's in, 0 for none, like minecraft's getFluidHeight
export function liquid_depth(body: Body, liquid_at: LiquidAt): number {
const half = body.width / 2 - 0.001;
const min_y = body.y + 0.001;
let depth = 0;
for (let x = Math.floor(body.x - half); x <= Math.floor(body.x + half); x++) {
for (let y = Math.floor(min_y); y <= Math.floor(body.y + body.height - 0.001); y++) {
for (let z = Math.floor(body.z - half); z <= Math.floor(body.z + half); z++) {
const liquid = liquid_at(x, y, z);
if (!liquid) continue;
const surface = y + (liquid_at(x, y + 1, z)?.id === liquid.id ? 1 : liquid.height);
depth = Math.max(depth, surface - min_y);
}
}
}
return depth;
}
// what the engine calls through, so engine access mods can hook these like methods (module exports can't be
// reassigned). see docs/modding/misc/engine.md
export const physics = { move_body, back_off_from_edge, box_is_free, in_water, eyes_in_water };
export const physics = {
move_body,
back_off_from_edge,
box_is_free,
in_water,
eyes_in_water,
liquid_flow,
push_by_liquid,
liquid_depth,
};
+10 -1
View File
@@ -1,8 +1,9 @@
import { ID_MASK, STATE_SHIFT } from "./constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "./everything_registry.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from "./everything_registry.ts";
import { block_item_texture, cube_face_texture } from "./block_models.ts";
import { FULL_SHAPE, type Shape } from "./block_shapes.ts";
import type { ItemStack } from "./inventory.ts";
import type { LiquidInCell } from "./physics.ts";
import { list_matches } from "./tags.ts";
export function point_inside_rec(
@@ -185,3 +186,11 @@ export function box_overlaps_block(
export function chunk_key(x: number, z: number) {
return (x + 32768) * 65536 + (z + 32768);
}
// the liquid a block value is, for liquids pushing things (see push_by_liquid in physics.ts), or undefined for blocks
// that aren't liquids
export function liquid_in_cell(value: number, block: BlockRegistry | undefined): LiquidInCell | undefined {
if (!block?.liquid) return undefined;
const level = get_state_value(value, block, "level") ?? 0;
return { id: value & ID_MASK, height: liquid_height(level, block.liquid.range) };
}