gizgame/voxel
Orthogonal voxel grid with stable coordinate-hashed solid collider IDs. Removing a block never shifts the collider IDs of remaining blocks.
import * as Voxel from "gizgame/voxel";Types
VoxelBlock
type VoxelBlock = { gx: i64; gy: i64; gz: i64; x: f64; y: f64; z: f64; mat: i64; solidId: i64; }A placed voxel block with grid coordinates, world position, material index and stable physics solid ID.
gx:i64gy:i64gz:i64x:f64y:f64z:f64mat:i64solidId:i64
VoxelGrid
class VoxelGrid { blocks: Vec<VoxelBlock> = []; step: f64; maxHeight: f64; limit: i64; constructor(step: f64 = 2.0, maxHeight: f64 = 15.0, limit: i64 = 180) { this.step = step; this.maxHeight = maxHeight; this.limit = limit; } at(gx: i64, gy: i64, gz: i64): VoxelBlock | undefined { for (const b of this.blocks) { if (b.gx === gx && b.gy === gy && b.gz === gz) { return b; } } return undefined; } place(px: f64, pz: f64, fx: f64, fz: f64, mat: i64, groundY: (x: f64, z: f64) => f64): VoxelBlock | undefined { if (this.blocks.length> = this.limit) { return undefined; } const gx = i64(Math.round((px + fx * 3.8) / this.step)); const gz = i64(Math.round((pz + fz * 3.8) / this.step)); const wx = f64(gx) * this.step; const wz = f64(gz) * this.step; let gy: i64 = 0; for (const b of this.blocks) { if (b.gx === gx && b.gz === gz && b.gy> = gy) { gy = b.gy + 1; } } const topY = f64(gy) * this.step + this.step * 0.5; if (topY> this.maxHeight) { return undefined; } const wy = groundY(wx, wz) + topY; const block: VoxelBlock = { gx, gy, gz, x: wx, y: wy, z: wz, mat, solidId: voxelSolidId(gx, gy, gz) }; this.blocks.push(block); return block; } mineNearest(tx: f64, tz: f64, maxDist2: f64 = 64.0): VoxelBlock | undefined { let best: i64 = -1; let bestD = maxDist2; for (const i of range(0, this.blocks.length)) { const b = this.blocks[i]; const d = (b.x - tx) * (b.x - tx) + (b.z - tz) * (b.z - tz); if (d <bestD) { bestD = d; best = i; } } if (best <0) { return undefined; } const dead = this.blocks[best]; let kept: Vec<VoxelBlock> = []; for (const i of range(0, this.blocks.length)) { if (i !== best) { kept.push(this.blocks[i]); } } this.blocks = kept; return dead; } clear(): Vec<i64> { let ids: Vec<i64> = []; for (const b of this.blocks) { ids.push(b.solidId); } this.blocks = []; return ids; } }Mutable voxel grid tracking placed blocks with coordinate-stable collider IDs.
blocks:Vec<M263.VoxelBlock>step:f64maxHeight:f64limit:i64
Functions
voxelSolidId
function voxelSolidId(gx: i64, gy: i64, gz: i64): i64Deterministic negative collider ID derived strictly from integer cell coordinates (gx, gy, gz).
Effects: fail.
constructor
constructor(step: f64 = 2.0, maxHeight: f64 = 15.0, limit: i64 = 180)An empty grid of the given cell size, height and block limit.
Effects: fail, read, write, alloc.
Methods
VoxelGrid.at
at(gx: i64, gy: i64, gz: i64): VoxelBlock | undefinedReturns the block at integer cell (gx, gy, gz), or undefined if empty.
Effects: fail, read.
VoxelGrid.place
place(px: f64, pz: f64, fx: f64, fz: f64, mat: i64, groundY: (x: f64, z: f64) => f64): VoxelBlock | undefinedPlaces a block at the grid cell in front of (px, pz) along (fx, fz), stacked on existing blocks.
Effects: fail, read, write, console, io, alloc.
VoxelGrid.mineNearest
mineNearest(tx: f64, tz: f64, maxDist2: f64 = 64.0): VoxelBlock | undefinedRemoves the nearest block to (tx, tz) within maxDist2 and returns it with its original solidId.
Effects: fail, read, write, alloc.
VoxelGrid.clear
clear(): Vec<i64>Clears all blocks and returns every collider ID that should be dropped from physics.
Effects: fail, read, write, alloc.