pub struct SpatialIndex2D<T: Copy>(/* private fields */);Expand description
Owned 2D collider snapshot with a balanced AABB hierarchy.
Rebuild explicitly after moving colliders. Queries see the last successful
snapshot. IDs need only be Copy; the index does not read or mutate ECS
components. Duplicate IDs are accepted and reported as supplied. Rebuild
validates all boxes before committing, reuses staging capacity, and needs
temporary room for both the old and new snapshots.
Implementations§
Source§impl<T: Copy> SpatialIndex2D<T>
impl<T: Copy> SpatialIndex2D<T>
Sourcepub fn rebuild(
&mut self,
source: impl IntoIterator<Item = (T, Aabb2)>,
) -> Result<usize, SpatialError>
pub fn rebuild( &mut self, source: impl IntoIterator<Item = (T, Aabb2)>, ) -> Result<usize, SpatialError>
Copies (id, bounds) pairs and atomically replaces the snapshot.
Invalid input returns its original position and preserves old data.
Build cost is O(n log n); query cost depends on overlap distribution.
Sourcepub fn nearest(
&self,
ray: Ray2,
max_distance: f32,
) -> Result<Option<SpatialHit2<T>>, SpatialError>
pub fn nearest( &self, ray: Ray2, max_distance: f32, ) -> Result<Option<SpatialHit2<T>>, SpatialError>
Nearest ray contact within an inclusive world-unit distance. Exact-distance ties use original input order. Infinity is allowed.
Sourcepub fn visit_overlapping(
&self,
area: Aabb2,
emit: impl FnMut(T),
) -> Result<(), SpatialError>
pub fn visit_overlapping( &self, area: Aabb2, emit: impl FnMut(T), ) -> Result<(), SpatialError>
Visits boxes with positive-volume overlap. Touching alone is excluded. Visitor order follows the hierarchy, not input order. No allocation.
Sourcepub fn overlapping(&self, area: Aabb2) -> Result<Vec<T>, SpatialError>
pub fn overlapping(&self, area: Aabb2) -> Result<Vec<T>, SpatialError>
Collects positive-volume overlaps for convenience.
Sourcepub fn visit_visible(&self, view: &Frustum2D, emit: impl FnMut(T))
pub fn visit_visible(&self, view: &Frustum2D, emit: impl FnMut(T))
Visits boxes that may be visible in this camera. No allocation.