/// <summary> /// adds the object to the SpatialHash /// </summary> /// <param name="collider">Object.</param> public void Add(IAABBF collider) { RectangleF bounds = collider.AABB; Point p1 = CellCoords(bounds.x, bounds.y); Point p2 = CellCoords(bounds.Right, bounds.Bottom); // update our bounds to keep track of our grid size if (!GridBounds.Contains(p1)) { GridBounds.Union(p1); } if (!GridBounds.Contains(p2)) { GridBounds.Union(p2); } for (var x = p1.X; x <= p2.X; x++) { for (var y = p1.Y; y <= p2.Y; y++) { // we need to create the cell if there is none var c = CellAtPosition(x, y, true); c.Add(collider); } } }
public void Remove(IAABBF obj) { foreach (var list in _store.Values) { if (list.Contains(obj)) { list.Remove(obj); } } }
/// <summary> /// removes the object from the SpatialHash /// </summary> /// <param name="collider">Collider.</param> public void Remove(IAABBF collider) { RectangleF bounds = collider.AABB; var p1 = CellCoords(bounds.x, bounds.y); var p2 = CellCoords(bounds.Right, bounds.Bottom); for (var x = p1.X; x <= p2.X; x++) { for (var y = p1.Y; y <= p2.Y; y++) { // the cell should always exist since this collider should be in all queryed cells var cell = CellAtPosition(x, y); Assert.AssertNotNull(cell, string.Format("removing Collider [{0}] from a cell that it is not present in", collider), Logger.LogLevel.Error); if (cell != null) { cell.Remove(collider); } } } }
/// <summary> /// removes the object from the SpatialHash using a brute force approach /// </summary> /// <param name="obj">Object.</param> public void ForceRemove(IAABBF obj) { cellDict.Remove(obj); }