Skip to content

HashGrid3D

Defined in: src/modules/spatial/hash-grid-3d.ts:36

Spatial hash over continuous {x, y, z} positions. Implements SpatialStructure with TPos = {x, y, z}, so it plugs into world.enableSpatial(Position3DDef, new HashGrid3D({ cellSize })).

Each position is bucketed into the cubic cell floor(p / cellSize) on every axis. The grid keeps a copy of each entity’s position, so:

  • queryNear and queryRect are exact — Euclidean distance and inclusive bounds against the stored position, not just “shares a cell”;
  • remove and move find the entity’s current cell from the stored copy.

An entity holds one position: add on an entity already present moves it. queryAt(pos) returns the entities in the cell containing pos.

Pick cellSize near the typical query radius: much smaller and a query walks many empty cells, much larger and each cell holds many candidates. A query whose box spans more cells than the grid holds entities scans the stored positions instead.

new HashGrid3D(options?): HashGrid3D

Defined in: src/modules/spatial/hash-grid-3d.ts:41

HashGrid3DOptions = {}

HashGrid3D

readonly cellSize: number

Defined in: src/modules/spatial/hash-grid-3d.ts:38

get size(): number

Defined in: src/modules/spatial/hash-grid-3d.ts:177

Number of entities in the grid.

number

add(id, pos): void

Defined in: src/modules/spatial/hash-grid-3d.ts:48

Register an entity at a position. No-op if already present at that position.

EntityId

Pos3

void

SpatialStructure.add


clear(): void

Defined in: src/modules/spatial/hash-grid-3d.ts:97

Remove all entries.

void

SpatialStructure.clear


has(id): boolean

Defined in: src/modules/spatial/hash-grid-3d.ts:103

Whether id is in the grid.

EntityId

boolean


move(id, _from, to): void

Defined in: src/modules/spatial/hash-grid-3d.ts:130

Move id to to. The stored position, not from, locates its current cell. Adds id if absent.

EntityId

Pos3

Pos3

void

SpatialStructure.move


positionOf(id): Pos3 | undefined

Defined in: src/modules/spatial/hash-grid-3d.ts:135

The position id was last added or moved to, or undefined if absent.

EntityId

Pos3 | undefined


queryAt(pos): Iterable<EntityId>

Defined in: src/modules/spatial/hash-grid-3d.ts:139

All entity IDs at pos. Empty iterable if none.

Pos3

Iterable<EntityId>

SpatialStructure.queryAt


queryNear(pos, radius): Iterable<EntityId>

Defined in: src/modules/spatial/hash-grid-3d.ts:145

All entity IDs within radius of pos (Euclidean, inclusive). radius is in the same units as the position coordinates.

Pos3

number

Iterable<EntityId>

SpatialStructure.queryNear


queryRect(min, max): Iterable<EntityId>

Defined in: src/modules/spatial/hash-grid-3d.ts:159

All entity IDs within an axis-aligned region (inclusive bounds). min / max are the region corners.

Pos3

Pos3

Iterable<EntityId>

SpatialStructure.queryRect


remove(id, _pos?): void

Defined in: src/modules/spatial/hash-grid-3d.ts:168

Remove id. The stored position, not pos, locates its cell. No-op if absent.

EntityId

Pos3

void

SpatialStructure.remove