use std::cmp::Ordering;
use crate::dbvt::{DiscreteVisitor, DynamicBoundingVolumeTree, TreeValue};
use crate::prelude::*;
pub struct DbvtBroadPhase;
impl DbvtBroadPhase {
pub fn new() -> Self {
Self {}
}
pub fn find_collider_pairs<T>(
&self,
tree: &DynamicBoundingVolumeTree<T>,
dirty: &[bool],
) -> Vec<(usize, usize)>
where
T: TreeValue,
T::Bound: Discrete<T::Bound>
+ Clone
+ Contains<T::Bound>
+ SurfaceArea
+ Union<T::Bound, Output = T::Bound>,
{
let mut potentials = Vec::default();
for &(shape_node_index, ref shape) in tree.values() {
let shape_value_index = tree.value_index(shape_node_index).unwrap();
if dirty[shape_value_index] {
for (hit_value_index, _) in
tree.query_for_indices(&mut DiscreteVisitor::<T::Bound, T>::new(shape.bound()))
{
let pair = match shape_value_index.cmp(&hit_value_index) {
Ordering::Equal => continue,
Ordering::Less => (shape_value_index, hit_value_index),
Ordering::Greater => (hit_value_index, shape_value_index),
};
if let Err(pos) = potentials.binary_search(&pair) {
potentials.insert(pos, pair);
}
}
}
}
potentials
}
}