Better block models !
This commit is contained in:
@@ -20,6 +20,7 @@ import { crosses_planes } from "../workers/translucent_sort.ts";
|
||||
import { Camera } from "../camera.ts";
|
||||
import type { Entity } from "../entity/entity.ts";
|
||||
import type { ModelJson } from "$/common/block_models.ts";
|
||||
import { clip_shape, collision_shape_at, FULL_SHAPE, type Shape, shape_at } from "$/common/block_shapes.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
|
||||
export interface Block {
|
||||
@@ -309,6 +310,16 @@ export class ClientLevel {
|
||||
return block === VOID || (block !== AIR && (this.#blocks[block]?.has_collision ?? true));
|
||||
}
|
||||
|
||||
// what entities bump into there, see common/physics.ts. unloaded chunks are full blocks, like has_collision
|
||||
collision_shape(x: number, y: number, z: number): boolean | Shape {
|
||||
const value = this.get_block_value(x, y, z);
|
||||
const nid = value & ID_MASK;
|
||||
if (nid === VOID) return true;
|
||||
if (nid === AIR) return false;
|
||||
const block = this.#blocks[nid];
|
||||
return block ? collision_shape_at(block, value) : true;
|
||||
}
|
||||
|
||||
// water, see is_liquid_block. unloaded chunks aren't
|
||||
is_liquid(x: number, y: number, z: number) {
|
||||
const block = this.get_block(x, y, z);
|
||||
@@ -640,7 +651,8 @@ export class ClientLevel {
|
||||
return closest;
|
||||
}
|
||||
|
||||
// the first block along the view of something at x/y/z looking at yaw/pitch, like minecraft's pick
|
||||
// the first block along the view of something at x/y/z looking at yaw/pitch, like minecraft's pick. it has to go
|
||||
// into one of the block's boxes (see common/block_shapes.ts), so it looks past the empty half of a slab
|
||||
pick(
|
||||
x: number,
|
||||
y: number,
|
||||
@@ -651,55 +663,51 @@ export class ClientLevel {
|
||||
step = 0.05,
|
||||
): BlockHitResult | undefined {
|
||||
const cos_pitch = Math.cos(pitch);
|
||||
const dx = -Math.sin(yaw) * cos_pitch;
|
||||
const dy = Math.sin(pitch);
|
||||
const dz = -Math.cos(yaw) * cos_pitch;
|
||||
const direction = [-Math.sin(yaw) * cos_pitch, Math.sin(pitch), -Math.cos(yaw) * cos_pitch];
|
||||
const origin = [x, y, z];
|
||||
|
||||
let prev_bx = Math.floor(x);
|
||||
let prev_by = Math.floor(y);
|
||||
let prev_bz = Math.floor(z);
|
||||
|
||||
let dist = 0;
|
||||
|
||||
while (dist <= max_distance) {
|
||||
x += dx * step;
|
||||
y += dy * step;
|
||||
z += dz * step;
|
||||
dist += step;
|
||||
|
||||
const bx = Math.floor(x);
|
||||
const by = Math.floor(y);
|
||||
const bz = Math.floor(z);
|
||||
// a little past the end, so a block whose cell the steps reach after max_distance is still tried
|
||||
for (let dist = step; dist <= max_distance + step; dist += step) {
|
||||
const bx = Math.floor(origin[0] + direction[0] * dist);
|
||||
const by = Math.floor(origin[1] + direction[1] * dist);
|
||||
const bz = Math.floor(origin[2] + direction[2] * dist);
|
||||
|
||||
if (bx === prev_bx && by === prev_by && bz === prev_bz) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const block = this.get_block(bx, by, bz);
|
||||
|
||||
if (block && block !== AIR && block !== VOID) {
|
||||
let face: Faces;
|
||||
|
||||
if (bx > prev_bx) {
|
||||
face = "west";
|
||||
} else if (bx < prev_bx) {
|
||||
face = "east";
|
||||
} else if (by > prev_by) {
|
||||
face = "bottom";
|
||||
} else if (by < prev_by) {
|
||||
face = "top";
|
||||
} else if (bz > prev_bz) {
|
||||
face = "north";
|
||||
} else {
|
||||
face = "south";
|
||||
}
|
||||
|
||||
return { x: bx, y: by, z: bz, face, block };
|
||||
}
|
||||
|
||||
prev_bx = bx;
|
||||
prev_by = by;
|
||||
prev_bz = bz;
|
||||
|
||||
const value = this.get_block_value(bx, by, bz);
|
||||
const block = value & ID_MASK;
|
||||
if (block === AIR || block === VOID) {
|
||||
continue;
|
||||
}
|
||||
const info = this.#blocks[block];
|
||||
const hit = clip_shape(
|
||||
info ? shape_at(info, value) : FULL_SHAPE,
|
||||
bx,
|
||||
by,
|
||||
bz,
|
||||
origin,
|
||||
direction,
|
||||
max_distance,
|
||||
);
|
||||
if (!hit) {
|
||||
continue;
|
||||
}
|
||||
// the side of the box it went in by
|
||||
const face: Faces = hit.axis === 0
|
||||
? (hit.sign > 0 ? "west" : "east")
|
||||
: hit.axis === 1
|
||||
? (hit.sign > 0 ? "bottom" : "top")
|
||||
: (hit.sign > 0 ? "north" : "south");
|
||||
return { x: bx, y: by, z: bz, face, block };
|
||||
}
|
||||
|
||||
return undefined;
|
||||
|
||||
Reference in New Issue
Block a user