physics/src/world.rs#
Source: lib/physics/src/world.rs
world -- bodies (a shape, a placement, layers) in three dynamic AABB trees, and the queries over all of them
A body starts in a tree of the bodies that stand still, with its exact box: meshes in one, the other
shapes in another; it goes to the tree of the moving bodies when it is first moved (with a box 0.05 m
larger, so small moves keep their leaf). So the large box of a map never widens the nodes of the props
and figures: rays and casts look at the map's mesh first, and its hit prunes the other trees
(docs/PHYSICS.md, measurements).
Results never depend on the shape of the tree: a first hit is the smallest (t, body, triangle), lists
are sorted by body and triangle (rays by t first). So a client that holds only some of the server's
bodies gets the same answer from them, as long as the bodies have the same ids.
struct BodyIda body of a [World]: an index and a generation (an id stays dead after remove, also when the index is used again). Ordered by index, then generation: the tie-break of the queries.
impl BodyId
fn index(self) -> u32the slot (used again after a remove)fn generation(self) -> u32how often the slot was used beforestruct Bodya body: what [World::add] took, readable through [World::body]shape: Shape-- its shapeiso: Iso-- where it is (set with [World::set_iso])layers: u32-- the layers the body is in (bit mask); a query sees it when its filter's mask shares a bituser: u64-- free for the caller (an entity id)
impl Body
fn is_moving(&self) -> boolhas the body been moved since it was added (it is in the tree of the moving bodies)struct Filterwhich bodies a query seesmask: u32-- bodies withlayers & mask != 0exclude: Option<BodyId>-- one body to leave out (the query's own, e.g. a figure's capsule)
impl Filter
const ALL: Filter = Filter { mask: u32::MAX, exclude: None }every bodyconst fn mask(mask: u32) -> Filterthe bodies in one of the layers ofmaskconst fn excluding(self, id: BodyId) -> Filterthe same without bodyidstruct WorldA set of bodies and the queries over them. Bodies move with [World::set_iso]; nothing moves by itself here (a [crate::Simulation] moves the bodies of its World). A clone answers every query as the original.
impl World
fn new() -> Worldan empty world; a moving body's box in the tree is 0.05 m larger than the body on every sidefn with_margin(margin: f64) -> Worldan empty world with this margin around a moving body's box (a body that has never moved has none)fn add(&mut self, shape: Shape, iso: Iso) -> BodyIdadd a body in layer 1fn add_with_layers(&mut self, shape: Shape, iso: Iso, layers: u32) -> BodyIdadd a body inlayersfn remove(&mut self, id: BodyId) -> Option<Body>remove a body -> it, None when the id is deadfn body(&self, id: BodyId) -> Option<&Body>the body, None when the id is deadfn set_iso(&mut self, id: BodyId, iso: Iso) -> boolmove a body -> false when the id is dead. The first move takes it to the tree of the moving bodies.fn set_layers(&mut self, id: BodyId, layers: u32) -> boola body's layers -> false when the id is deadfn set_user(&mut self, id: BodyId, user: u64) -> boola body's value for the caller -> false when the id is deadfn len(&self) -> usizethe number of bodiesfn is_empty(&self) -> boolno bodiesfn bodies(&self) -> impl Iterator<Item = (BodyId, &Body)> + '_every body with its id, by indexfn trees(&self) -> [&DynamicTree; 3]the broad phase (for tests and statistics): the trees of the meshes and of the other bodies that never moved, and the one of the moving bodiesfn ray_cast(&self, origin: Vec3, dir: Vec3, max_t: f64, filter: &Filter) -> Option<(BodyId, RayHit)>the first hit of the ray origin + dir * t, 0 <= t <= max_tfn ray_cast_all(&self, origin: Vec3, dir: Vec3, max_t: f64, filter: &Filter, out: &mut Vec<(BodyId, RayHit)>)every hit of the ray (one per convex body, one per triangle of a mesh), appended tooutsorted by t, body, trianglefn cast(&self, shape: &Shape, iso: &Iso, motion: Vec3, filter: &Filter) -> Option<(BodyId, CastHit)>the first body convexshape, moved fromisobymotion, touches (see [query::cast])fn overlap(&self, shape: &Shape, iso: &Iso, filter: &Filter, out: &mut Vec<BodyId>)the bodies convexshapeatisotouches or overlaps, appended tooutby idfn contacts(&self, shape: &Shape, iso: &Iso, margin: f64, filter: &Filter, out: &mut Vec<(BodyId, Contact)>)the contacts of convexshapeatisowith every body (every triangle) withinmargin, appended tooutby body and triangle: what pushes a figure out of the levelfn closest_point(&self, point: Vec3, max_dist: f64, filter: &Filter) -> Option<(BodyId, PointHit)>the nearest point of any body topointwithinmax_dist(equal distance: the lower body, triangle)