Skip to main content

SpatialHashGrid

Struct SpatialHashGrid 

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

Dense spatial grid for O(1) entity lookup by position. Grid cells are cell_size x cell_size world units. Uses u64 entity handles — zero-copy, no per-entity String allocation.

Implementations§

Source§

impl SpatialHashGrid

Source

pub fn new(cell_size: i32) -> Self

Create a new spatial hash grid with the given cell size.

Source

pub fn cell_key(&self, x: i32, y: i32) -> (i32, i32)

Compute the cell key for a world-space position.

Source

pub fn insert(&mut self, entity_id: u64, x: i32, y: i32)

Insert an entity at world position (x, y).

Source

pub fn remove(&mut self, entity_id: u64, x: i32, y: i32)

Remove an entity from its cell at world position (x, y). Removes the first occurrence. No-ops if not present.

Source

pub fn query_radius(&self, cx: i32, cy: i32, radius: i32) -> Vec<u64>

Query all entities within radius world units of (cx, cy). Scans O(radius^2 / cell_size^2) cells.

Source

pub fn query_cell(&self, x: i32, y: i32) -> &[u64]

Return all entity IDs in the cell containing world position (x, y).

Source

pub fn clear(&mut self)

Clear all cells. Retains allocated capacity.

Source

pub fn entity_count(&self) -> usize

Return the total number of entity references across all cells.

Source

pub fn cell_count(&self) -> usize

Return the number of occupied cells.

Source

pub fn cell_size(&self) -> i32

Return the cell size.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.