Grid

A broadphase grid that can handle bodies and points

Constructor

new Grid(size)

Creates an empty grid

Parameters:
NameTypeDefaultDescription
sizenumber2000

Size of each grid cell

Members

grid :Object

Grid cells. You can access grid cells with grid.grid[id], with id being the grid cell id corresponding to the x/y position of the cell. You can find the grid cell id with grid.pair(cellPosition)

Type:
  • Object

gridIds :Set

Set of all created grid ids

Type:
  • Set

gridSize :number

The grid size

Type:
  • number

Methods

pair(pos) → {Number}

Takes a cell position and returns the corresponding grid cell id

Parameters:
NameTypeDescription
posvec
Returns:

grid cell id

Type: 
Number

unpair(n) → {vec}

Takes a grid cell id and returns the corresponding cell position

Parameters:
NameTypeDescription
nNumber

Grid cell id

Returns:

Cell position

Type: 
vec

getBounds(body) → {Bounds}

Takes a body with a bounding box and returns the range of grid cells that the body is in

Parameters:
NameTypeDescription
bodyRigidBody | Bounds | vec

the RigidBody, global space vec, or global space bounds to convert to grid space Bounds

Returns:

Grid space bounds

Type: 
Bounds

getBucketIds(bounds) → {Array.<Number>}

Takes grid-space bounds and returns an array of all bucket ids within those bounds

Parameters:
NameTypeDescription
boundsBounds

Bounding box in form of { min: vec, max: vec } to get IDs of grid cells within

Returns:

Array of grid cell ids

Type: 
Array.<Number>

getBuckets(bounds)

Takes global-space bounds and returns an array of all buckets in those bounds

Parameters:
NameTypeDescription
bounds*

addBody(body)

Adds the body to the grid

Parameters:
NameTypeDescription
bodyRigidBody

Body added to the grid

removeBody(body)

Removes the body from the grid

Parameters:
NameTypeDescription
bodyRigidBody

Body removed from the grid

addPoint(point)

Adds a vector point to the grid

Parameters:
NameTypeDescription
pointvec

Point added

removePoint(point)

Remove a vector point from the grid

Parameters:
NameTypeDescription
pointvec

Point removed

updateBody(body)

Updates the body's position in the grid

Parameters:
NameTypeDescription
bodyRigidBody | vec

Body in the grid