Skip to main content

SpatialHashGrid3D

Struct SpatialHashGrid3D 

Source
pub struct SpatialHashGrid3D { /* private fields */ }
Expand description

A uniform-grid spatial hash for broad-phase collision culling in 3D.

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 SpatialHashGrid3D

Implements construction, insertion, and querying for SpatialHashGrid3D.

Source

pub fn create(cell_size: f64) -> SpatialHashGrid3D

Creates a new 3D spatial hash grid with the given cell size.

A cell size that is not finite and strictly positive is rejected in favour of [SPATIAL_DEFAULT_CELL_SIZE_3D], for the same reason as SpatialHashGrid2D::create: a near-zero cell size makes the column range of an ordinary body span millions of cells.

§Arguments
  • f64 - The world-space size of each grid cell.
§Returns
  • SpatialHashGrid3D - The new grid.
Source

pub fn with_default_size() -> SpatialHashGrid3D

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

§Returns
  • SpatialHashGrid3D - The new grid.
Source

pub fn insert(&mut self, index: usize, min: Vector3D, max: Vector3D)

Inserts a body index into all cells overlapping the given 3D bounding box.

§Arguments
  • usize - The body index to insert.
  • Vector3D - The minimum corner of the bounding box.
  • Vector3D - The maximum corner of the bounding box.
Source

pub fn query(&self, min: Vector3D, max: Vector3D) -> Vec<usize>

Returns all candidate body indices whose cells overlap the given 3D bounding box.

Deduplicates indices so each candidate appears at most once.

§Arguments
  • Vector3D - The minimum corner of the query box.
  • Vector3D - 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: Vector3D, max: Vector3D, 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
  • Vector3D - The minimum corner of the query box.
  • Vector3D - 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 SpatialHashGrid3D

Source

pub fn get_cell_size(&self) -> f64

Source

pub fn get_inverse_cell_size(&self) -> f64

Source

pub fn get_cells(&self) -> &SpatialCellMap3D

Source

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

Source§

impl SpatialHashGrid3D

Source

pub fn new(cell_size: f64) -> Self

Trait Implementations§

Source§

impl Clone for SpatialHashGrid3D

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 SpatialHashGrid3D

Source§

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

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

impl Default for SpatialHashGrid3D

Implements Default for SpatialHashGrid3D with the default cell size.

Source§

fn default() -> SpatialHashGrid3D

Constructs a default SpatialHashGrid3D value.

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

impl PartialEq for SpatialHashGrid3D

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 SpatialHashGrid3D

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