physics/src/tree.rs#
Source: lib/physics/src/tree.rs
tree -- the dynamic AABB tree of a world's bodies: leaves with (fattened) boxes, inserted where the
growth of the surface is smallest, kept balanced by rotations on the way up (the scheme of Box2D's
b2DynamicTree, written anew). A moving body keeps its leaf while its box stays inside the fattened
one. Deterministic: the same sequence of inserts, moves and removes gives the same tree.
struct DynamicTreea dynamic AABB tree over items (u32)
impl DynamicTree
fn new() -> DynamicTreean empty treefn insert(&mut self, aabb: Aabb, item: u32) -> u32a new leaf foritemwith boxaabb-> its idfn remove(&mut self, leaf: u32)remove a leaffn update(&mut self, leaf: u32, aabb: Aabb)give a leaf a new box (it keeps its id)fn aabb(&self, leaf: u32) -> Aabbthe box of a leaffn len(&self) -> usizethe number of leavesfn is_empty(&self) -> boolno leavesfn height(&self) -> u32the height of the root (0 for one leaf or none)fn query(&self, bx: &Aabb, mut f: impl FnMut(u32))callf(item)for every leaf whose box touchesbxfn ray(&self, o: Vec3, d: Vec3, ext: Vec3, mut tmax: f64, mut f: impl FnMut(u32, f64) -> f64) -> f64the leaves whose box, grown byext, the ray o + t d enters at t <= tmax, nearer subtrees first;f(item, tmax)returns the new tmax -> the last tmaxfn validate(&self) -> Result<(), &'static str>(tests) the tree is consistent: parents, heights, boxes holding their children, the leaf count -> an error text