Skip to main content

SpatialHashGrid2D

Struct SpatialHashGrid2D 

Source
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.

Source

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.
Source

pub fn with_default_size() -> SpatialHashGrid2D

Creates a new 2D spatial hash grid with the default cell size.

§Returns
  • SpatialHashGrid2D - The new grid.
Source

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.
Source

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.
Source

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.

Source

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

Source

pub fn get_cell_size(&self) -> f64

Source

pub fn get_inverse_cell_size(&self) -> f64

Source

pub fn get_cells(&self) -> &SpatialCellMap2D

Source

pub fn set_cells(&mut self, val: SpatialCellMap2D) -> &mut Self

Source§

impl SpatialHashGrid2D

Source

pub fn new(cell_size: f64) -> Self

Trait Implementations§

Source§

impl Clone for SpatialHashGrid2D

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SpatialHashGrid2D

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SpatialHashGrid2D

Default-construction for SpatialHashGrid2D.

Source§

fn default() -> SpatialHashGrid2D

Constructs a default SpatialHashGrid2D value.

§Returns
  • SpatialHashGrid2D - A default-constructed instance with the documented initial state.
Source§

impl PartialEq for SpatialHashGrid2D

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SpatialHashGrid2D

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more