dcsext.containers.SpatialHash

SpatialHash.

Contents

  1. Description
  2. Functions
    1. SpatialHashGrid:__init(tablesize, cellsize)
    2. SpatialHashGrid:_hash(x, y)
    3. SpatialHashGrid:newObject(position, radius)
    4. SpatialHashGrid:update(object)
    5. SpatialHashGrid:insert(object)
    6. SpatialHashGrid:remove(object)
    7. SpatialHashGrid:findNear(position, radius)
  3. Fields
    1. SpatialHashGrid.Object

Description

Provides a basic spatial hashing container for 2d objects to facilitate fast neighbor query.

Functions

SpatialHashGrid:__init(tablesize, cellsize)

Constructor.

Parameters

tablesize
number of hash buckets in the grid
cellsize
edge length of one grid cell in world units

SpatialHashGrid:_hash(x, y)

Maps a 2d position(x, y) into our cell grid.

Parameters

x
x coordinate of the position to hash.
y
y coordinate of the position to hash.

SpatialHashGrid:newObject(position, radius)

Returns a new Object already inserted into the grid.

Parameters

position
center of the object as a Vec2
radius
radius of the object, the object occupies every cell its bounding circle overlaps

Returns

  • the inserted Object instance

SpatialHashGrid:update(object)

Update the position of object in the grid. Call after changing an object’s position or radius so it is re-registered under the cells it now overlaps, no-op when the occupied cells did not change.

Parameters

object
the Object to move within the grid

SpatialHashGrid:insert(object)

Add a new object into the grid.

Parameters

object
the Object to register, every cell overlapped by its bounding circle will reference it

SpatialHashGrid:remove(object)

Remove object from the grid.

Parameters

object
the Object to unregister from all of its cells

SpatialHashGrid:findNear(position, radius)

Find all objects in the grid within radius of position. The search visits every cell overlapped by the query circle, so results may include objects whose distance is up to their own radius beyond it, callers can filter further if exact distances are required.

Parameters

position
center of the query circle as a Vec2
radius
radius of the query circle

Returns

  • table keyed by the found Object instances

Fields

SpatialHashGrid.Object

Object class stored on the grid, instances track the cells they occupy so update() can move them efficiently.