Skip to content

Commit

Permalink
feat(spatial,bvh): add get_collisions (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka authored Dec 15, 2024
1 parent e785f91 commit eec261b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
14 changes: 7 additions & 7 deletions crates/bvh-region/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use glam::Vec3;
const ELEMENTS_TO_ACTIVATE_LEAF: usize = 16;
const VOLUME_TO_ACTIVATE_LEAF: f32 = 5.0;

pub trait GetAabb<T>: Sync + Fn(&T) -> Aabb {}
pub trait GetAabb<T>: Fn(&T) -> Aabb {}

impl<T, F> GetAabb<T> for F where F: Fn(&T) -> Aabb + Sync {}
impl<T, F> GetAabb<T> for F where F: Fn(&T) -> Aabb {}

#[cfg(feature = "plot")]
pub mod plot;
Expand Down Expand Up @@ -121,7 +121,7 @@ fn thread_count_pow2() -> usize {

impl<T: Send + Copy + Sync + Debug> Bvh<T> {
#[tracing::instrument(skip_all, fields(elements_len = elements.len()))]
pub fn build<H: Heuristic>(mut elements: Vec<T>, get_aabb: &impl GetAabb<T>) -> Self {
pub fn build<H: Heuristic>(mut elements: Vec<T>, get_aabb: &(impl GetAabb<T> + Sync)) -> Self {
let max_threads = thread_count_pow2();

let len = elements.len();
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<T: Send + Copy + Sync + Debug> Bvh<T> {
pub fn get_collisions<'a>(
&'a self,
target: Aabb,
get_aabb: &'a impl GetAabb<T>,
get_aabb: impl GetAabb<T> + 'a,
) -> impl Iterator<Item = &'a T> + 'a {
BvhIter::consume(self, target, get_aabb)
}
Expand Down Expand Up @@ -411,7 +411,7 @@ impl BvhNode {
max_threads: usize,
nodes_idx: usize,
nodes: &mut [Self],
get_aabb: &impl GetAabb<T>,
get_aabb: &(impl GetAabb<T> + Sync),
) -> (i32, usize)
where
T: Send + Copy + Sync + Debug,
Expand Down Expand Up @@ -519,7 +519,7 @@ impl<'a, T> BvhIter<'a, T> {
fn consume(
bvh: &'a Bvh<T>,
target: Aabb,
get_aabb: &'a impl GetAabb<T>,
get_aabb: impl GetAabb<T> + 'a,
) -> Box<dyn Iterator<Item = &'a T> + 'a> {
let root = bvh.root();

Expand Down Expand Up @@ -549,7 +549,7 @@ impl<'a, T> BvhIter<'a, T> {
pub fn process(
self,
on: &'a BvhNode,
get_aabb: &'a impl GetAabb<T>,
get_aabb: impl GetAabb<T>,
) -> impl Iterator<Item = &'a T> {
gen move {
let mut stack: ArrayVec<&'a BvhNode, 64> = ArrayVec::new();
Expand Down
18 changes: 9 additions & 9 deletions crates/spatial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ impl SpatialIndex {
self.query = bvh_region::Bvh::build::<TrivialHeuristic>(all_entities, &get_aabb);
}

// pub fn get_collisions<'a>(
// &'a self,
// target: Aabb,
// world: &'a World,
// ) -> impl Iterator<Item = Entity> + 'a {
// let get_aabb = get_aabb_func(world);
// self.query.get_collisions(target, get_aabb).copied()
// }
pub fn get_collisions<'a>(
&'a self,
target: Aabb,
world: &'a World,
) -> impl Iterator<Item = Entity> + 'a {
let get_aabb = get_aabb_func(world);
self.query.get_collisions(target, get_aabb).copied()
}

/// Get the closest player to the given position.
#[must_use]
Expand All @@ -63,7 +63,7 @@ impl SpatialIndex {
Some(world.entity_from_id(*target))
}
}
//

/// If we want the entity to be spatially indexed, we need to add this component.
#[derive(Component)]
pub struct Spatial;
Expand Down

0 comments on commit eec261b

Please sign in to comment.