Skip to content

1681-engine / index / SpatialHash

Class: SpatialHash ​

Defined in: src/net/SpatialHash.ts:16

Points sorted into square cells, for asking who is near a spot without looking at everyone: a room's interest management, a server's hit test. Moving a point within its cell costs a write; across cells, a set delete and add. Pure and transport-free, so the same class serves the client.

Constructors ​

Constructor ​

ts
new SpatialHash(cellSize): SpatialHash;

Defined in: src/net/SpatialHash.ts:21

cellSize: world px per cell. About the radius queries ask for keeps a query to a 3×3 block.

Parameters ​

ParameterType
cellSizenumber

Returns ​

SpatialHash

Properties ​

cellSize ​

ts
readonly cellSize: number;

Defined in: src/net/SpatialHash.ts:21

Accessors ​

size ​

Get Signature ​

ts
get size(): number;

Defined in: src/net/SpatialHash.ts:25

Returns ​

number

Methods ​

clear() ​

ts
clear(): void;

Defined in: src/net/SpatialHash.ts:56

Returns ​

void


delete() ​

ts
delete(id): void;

Defined in: src/net/SpatialHash.ts:49

Parameters ​

ParameterType
idnumber

Returns ​

void


has() ​

ts
has(id): boolean;

Defined in: src/net/SpatialHash.ts:29

Parameters ​

ParameterType
idnumber

Returns ​

boolean


inRect() ​

ts
inRect(
   x0, 
   y0, 
   x1, 
   y1, 
   out?
): number[];

Defined in: src/net/SpatialHash.ts:73

The ids inside the box from (x0, y0) to (x1, y1), edges included.

Parameters ​

ParameterTypeDefault value
x0numberundefined
y0numberundefined
x1numberundefined
y1numberundefined
outnumber[][]

Returns ​

number[]


near() ​

ts
near(
   x, 
   y, 
   radius, 
   out?
): number[];

Defined in: src/net/SpatialHash.ts:62

The ids within radius of (x, y), edge included, pushed onto out in no set order.

Parameters ​

ParameterTypeDefault value
xnumberundefined
ynumberundefined
radiusnumberundefined
outnumber[][]

Returns ​

number[]


set() ​

ts
set(
   id, 
   x, 
   y
): void;

Defined in: src/net/SpatialHash.ts:34

Puts id at (x, y), or moves it there.

Parameters ​

ParameterType
idnumber
xnumber
ynumber

Returns ​

void

Released under the MIT License.