bonk-docs/content/api/SpatialHash.md

2.7 KiB

title date weight
SpatialHash<T> 2019-09-15T11:28:40-07:00 10

SpatialHash is used for broad-phase collision detection. It can quickly return a set of potential collisions of a transformed shape. SpatialHash takes an ID type that is used to avoid comparing certain shape-transforms.

Methods

public SpatialHash(int cellSize)

Constructor method. Takes an integer representing the width of a cell in the spatial hash.

The cell width must not be too large or too small. If the cell width is too small, then shape-transforms will occupy many cells, and the hash check will have to check all of those cells for potential collisions. If the cell width is too large, then many shape-transforms will be contained in each cell and many expensive collision tests will have to be made.

A good rule of thumb is picking a cell width that is roughly twice the size of the most common objects in your game.

Example:

var hash = new SpatialHash<int>(16);

public void Insert(T id, IHasAABB2D shape, Transform2D Transform2D)

Given an ID, an object that has an AABB, and corresponding transform, inserts into the spatial hash. Uses the transformed AABB to insert into appropriate hash cells.

Example:

var hash = new SpatialHash<int>(16);

var circle = new MoonTools.Bonk.Circle(8);
var circleTransform = new Transform2D(new Vector2(16, 16));

var rect = new Rectangle(-2, -2, 2, 2);
var rectTransform = new Transform2D(new Vector2(8, 8));

hash.Insert(0, circle, circleTransform);
hash.Insert(1, rect, rectTransform);

public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(T id, IHasAABB2D shape, Transform2D Transform2D)

Given an ID, an object with an AABB, and corresponding transform, retrieves a set of potential collisions from the spatial hash.

Example:

var hash = new SpatialHash<int>(16);

var circle = new MoonTools.Bonk.Circle(8);
var circleTransform = new Transform2D(new Vector2(16, 16));

var rect = new Rectangle(-2, -2, 2, 2);
var rectTransform = new Transform2D(new Vector2(8, 8));

hash.Insert(0, circle, circleTransform);
hash.Insert(1, rect, rectTransform);

hash.Retrieve(1, rect, rectTransform);

In this example, the circle we inserted will be in the IEnumerable returned by the Retrieve call because they both exist in the same hash cell.

If a potential colliding shape-transform has the same ID as the one passed into the method, it will not be returned in the IEnumerable.


public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB aabb)

Retrieves objects from the AABB based on a pre-transformed AABB.

public void Clear()

Empties the spatial hash. Useful when the spatial hash contains dynamic data that needs to be updated.