SpatialHashGrid

The SpatialHashGrid is is a data structure that stores and sorts items into distinct "2D grids". It allows for the quick access of nearby items without brute force iteration. This data structure can increase performance by ~100x for sufficiently large collision interactions in close proximity. Note that this data structure is not directly iterable.

Important: Only items that implement the HashGridItem interface are compatible with the SpatialHashGrid.

Constructor

new SpatialHashGrid(width, height, xGrids, yGridsopt)

Parameters:
NameTypeAttributesDefaultDescription
widthNumber

width of HashGrid

heightNumber

height of HashGrid

xGridsint

number of grid separations on the x-axis

yGridsint<optional>
null

optional param number of grid separations on the y-axis, defaults to same as xGrids

Methods

add(item)

Adds an item to the HashGrid.

Parameters:
NameTypeDescription
itemHashGridItem

deleteItem(item)

Delete item from HashGrid.

Parameters:
NameTypeDescription
itemHashGridItem

findNear(item, range) → {Array.<HashGridItem>}

Finds the nearby items for a given item, and updates the queryId.

Parameters:
NameTypeDefaultDescription
itemHashGridItem
rangeArray.<Number>null

optional param that overrides the getHashDimensions default surrounding dimensions of the hash item.

Returns:
Type: 
Array.<HashGridItem>

updateItem(item)

Updates the grid positions of the item within the HashGrid. This function MUST be called after any position change.

Parameters:
NameTypeDescription
itemHashGridItem

values() → {Array.<HashGridItem>}

Returns a unique list of all HashGridItems the HashGrid.

Returns:
Type: 
Array.<HashGridItem>