JFLib

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 BodyId a 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) -> u32 the slot (used again after a remove)

  • fn generation(self) -> u32 how often the slot was used before

  • struct Body a body: what [World::add] took, readable through [World::body]

    • shape: Shape -- its shape
    • iso: 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 bit
    • user: u64 -- free for the caller (an entity id)

impl Body

  • fn is_moving(&self) -> bool has the body been moved since it was added (it is in the tree of the moving bodies)

  • struct Filter which bodies a query sees

    • mask: u32 -- bodies with layers & mask != 0
    • exclude: 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 body

  • const fn mask(mask: u32) -> Filter the bodies in one of the layers of mask

  • const fn excluding(self, id: BodyId) -> Filter the same without body id

  • struct World A 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() -> World an empty world; a moving body's box in the tree is 0.05 m larger than the body on every side
  • fn with_margin(margin: f64) -> World an 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) -> BodyId add a body in layer 1
  • fn add_with_layers(&mut self, shape: Shape, iso: Iso, layers: u32) -> BodyId add a body in layers
  • fn remove(&mut self, id: BodyId) -> Option<Body> remove a body -> it, None when the id is dead
  • fn body(&self, id: BodyId) -> Option<&Body> the body, None when the id is dead
  • fn set_iso(&mut self, id: BodyId, iso: Iso) -> bool move 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) -> bool a body's layers -> false when the id is dead
  • fn set_user(&mut self, id: BodyId, user: u64) -> bool a body's value for the caller -> false when the id is dead
  • fn len(&self) -> usize the number of bodies
  • fn is_empty(&self) -> bool no bodies
  • fn bodies(&self) -> impl Iterator<Item = (BodyId, &Body)> + '_ every body with its id, by index
  • fn 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 bodies
  • fn 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_t
  • fn 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 to out sorted by t, body, triangle
  • fn cast(&self, shape: &Shape, iso: &Iso, motion: Vec3, filter: &Filter) -> Option<(BodyId, CastHit)> the first body convex shape, moved from iso by motion, touches (see [query::cast])
  • fn overlap(&self, shape: &Shape, iso: &Iso, filter: &Filter, out: &mut Vec<BodyId>) the bodies convex shape at iso touches or overlaps, appended to out by id
  • fn contacts(&self, shape: &Shape, iso: &Iso, margin: f64, filter: &Filter, out: &mut Vec<(BodyId, Contact)>) the contacts of convex shape at iso with every body (every triangle) within margin, appended to out by body and triangle: what pushes a figure out of the level
  • fn closest_point(&self, point: Vec3, max_dist: f64, filter: &Filter) -> Option<(BodyId, PointHit)> the nearest point of any body to point within max_dist (equal distance: the lower body, triangle)

Source: docs/API.md in the JFLib repository.