pub struct SpatialHashGrid2D { /* private fields */ }Expand description
A uniform-grid spatial hash for broad-phase collision culling in 2D.
Bodies are inserted by their world-space axis-aligned bounding box. A query returns all candidate indices whose AABBs overlap the query region, dramatically reducing narrow-phase collision checks from O(n²) to near O(n).
Implementations§
Source§impl SpatialHashGrid2D
Implements construction, insertion, and querying for SpatialHashGrid2D.
impl SpatialHashGrid2D
Implements construction, insertion, and querying for SpatialHashGrid2D.
Sourcepub fn create(cell_size: f64) -> SpatialHashGrid2D
pub fn create(cell_size: f64) -> SpatialHashGrid2D
Creates a new 2D spatial hash grid with the given cell size.
A cell size that is not finite and strictly positive cannot produce a
usable grid: clamping it up to [EPSILON] leaves an inverse cell size
of 1e6, so a body of ordinary size maps to a column range in the
millions and a single insert or query degenerates into a loop over
billions of empty cells. Such a request is treated as “no cell size
given” and falls back to [SPATIAL_DEFAULT_CELL_SIZE_2D], which keeps
both the grid and the per-operation work bounded.
§Arguments
f64- The world-space size of each grid cell.
§Returns
SpatialHashGrid2D- The new grid.
Sourcepub fn with_default_size() -> SpatialHashGrid2D
pub fn with_default_size() -> SpatialHashGrid2D
Creates a new 2D spatial hash grid with the default cell size.
§Returns
SpatialHashGrid2D- The new grid.
Sourcepub fn insert(&mut self, index: usize, min: Vector2D, max: Vector2D)
pub fn insert(&mut self, index: usize, min: Vector2D, max: Vector2D)
Inserts a body index into all cells overlapping the given bounding box.
§Arguments
usize- The body index to insert.Vector2D- The minimum corner of the bounding box.Vector2D- The maximum corner of the bounding box.
Sourcepub fn query(&self, min: Vector2D, max: Vector2D) -> Vec<usize>
pub fn query(&self, min: Vector2D, max: Vector2D) -> Vec<usize>
Returns all candidate body indices whose cells overlap the given bounding box.
Deduplicates indices so each candidate appears at most once.
§Arguments
Vector2D- The minimum corner of the query box.Vector2D- The maximum corner of the query box.
§Returns
Vec<usize>- The list of candidate body indices.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all entries from the grid, preparing it for a fresh insertion pass.
Cells that held bodies during the just-finished pass keep their key and
their underlying Vec buffer, so a body that stays put pays no
allocation on the next tick. Cells that ended the pass empty are dropped
outright: without that, a scene whose bodies roam across a large world
accumulates one permanent map entry per cell ever visited, and both the
map and the per-tick clear below it grow without bound for the lifetime
of the process. Dropping on empty bounds the map to the cells the
current pass actually populates, at the cost of one small Vec
allocation for a cell a body leaves and then re-enters.
Sourcepub fn query_into(
&self,
min: Vector2D,
max: Vector2D,
out: &mut Vec<usize>,
seen: &mut HashSet<usize>,
)
pub fn query_into( &self, min: Vector2D, max: Vector2D, out: &mut Vec<usize>, seen: &mut HashSet<usize>, )
Appends all candidate body indices overlapping the query box into out,
deduplicating via the caller-provided seen set.
Both out and seen are cleared first, so the caller can reuse the same
buffers across all queries in a step without any per-query allocation.
§Arguments
Vector2D- The minimum corner of the query box.Vector2D- The maximum corner of the query box.&mut Vec<usize>- The output buffer, cleared then filled with candidates.&mut HashSet<usize>- The dedup scratch set, cleared then reused.
Source§impl SpatialHashGrid2D
impl SpatialHashGrid2D
pub fn get_cell_size(&self) -> f64
pub fn get_inverse_cell_size(&self) -> f64
pub fn get_cells(&self) -> &SpatialCellMap2D
pub fn set_cells(&mut self, val: SpatialCellMap2D) -> &mut Self
Trait Implementations§
Source§impl Clone for SpatialHashGrid2D
impl Clone for SpatialHashGrid2D
Source§impl Debug for SpatialHashGrid2D
impl Debug for SpatialHashGrid2D
Source§impl Default for SpatialHashGrid2D
Default-construction for SpatialHashGrid2D.
impl Default for SpatialHashGrid2D
Default-construction for SpatialHashGrid2D.
Source§fn default() -> SpatialHashGrid2D
fn default() -> SpatialHashGrid2D
Constructs a default SpatialHashGrid2D value.
§Returns
SpatialHashGrid2D- A default-constructed instance with the documented initial state.