JFLib

physics/src/query.rs#

Source: lib/physics/src/query.rs

query -- one shape against one placed shape, without a world: rays, casts, distance and contacts, the
nearest point.  The work happens in the frame of the target shape (positions far from the origin cancel
once, in the relative placement); results come back in world coordinates.  The query shape of a cast,
a distance or a contact must be convex: with a mesh as the query shape these give nothing.
/// `triangle` of a hit on anything but a mesh
NO_TRIANGLE: u32 = u32::MAX
  • struct RayHit where a ray hits

    • t: f64 -- the ray parameter: point = origin + dir * t (metres when dir is a unit vector)
    • point: Vec3 -- origin + dir * t
    • normal: Vec3 -- the unit normal, against the ray (a ray starting inside a convex shape: t = 0, normal = -dir)
    • triangle: u32 -- the triangle's index in the mesh's index list, or [NO_TRIANGLE]
  • struct CastHit where a moving shape first touches another

    • fraction: f64 -- how far along the motion: the shape stops at iso.pos + motion * fraction (0..=1). With a radius (sphere, capsule) on either side the gap there is at most 1e-9 m, between two shapes without one at most [crate::CAST_SKIN] + 1e-9 m; never a penetration that was not there at the start.
    • point: Vec3 -- the contact point on the other shape's surface
    • normal: Vec3 -- the unit normal of that surface towards the moving shape
    • triangle: u32 -- the triangle's index in the mesh's index list, or [NO_TRIANGLE]
    • depth: f64 -- > 0: the shapes overlapped at the start by this much (then fraction = 0)
  • struct Contact how two shapes touch or nearly touch

    • point: Vec3 -- on the other shape's surface (with overlap: its deepest point)
    • normal: Vec3 -- the unit normal from the other shape towards the query shape: moving the query shape by normal * depth separates them (for a triangle of a mesh along the triangle's normal, unless the contact is on an active edge)
    • depth: f64 -- the overlap; negative: the gap (contacts within a margin)
    • triangle: u32 -- the triangle's index in the mesh's index list, or [NO_TRIANGLE]
  • struct Separation the distance between two shapes

    • distance: f64 -- the gap; negative: the penetration depth
    • point_a: Vec3 -- the nearest (or deepest) points on the query shape and on the other one
    • point_b: Vec3 -- on the other shape
    • normal: Vec3 -- the unit normal from the other shape towards the query shape
    • triangle: u32 -- the nearest triangle of a mesh, or [NO_TRIANGLE]
  • struct PointHit the point of a shape nearest to a given point

    • point: Vec3 -- the nearest point of the surface, or the point itself when it is inside a convex shape
    • distance: f64 -- 0 inside
    • inside: bool -- the point lies inside a convex shape
    • triangle: u32 -- the nearest triangle of a mesh, or [NO_TRIANGLE]
  • fn ray(target: &Shape, target_iso: &Iso, origin: Vec3, dir: Vec3, max_t: f64) -> Option<RayHit> the first hit of the ray origin + dir * t (0 <= t <= max_t) on target placed by target_iso

  • fn ray_all(target: &Shape, target_iso: &Iso, origin: Vec3, dir: Vec3, max_t: f64, out: &mut Vec<RayHit>) every hit of the ray on target (one for a convex shape, one per triangle for a mesh), appended to out sorted by t and then by triangle

  • fn cast(shape: &Shape, iso: &Iso, motion: Vec3, target: &Shape, target_iso: &Iso) -> Option<CastHit> the first contact of convex shape, moved from iso by motion, with target. Shapes that touch or overlap at the start give a hit at fraction 0 only when the motion goes into the other shape (normal . motion < 0); a motion along or away from it is free, so sliding along a surface works.

  • fn separation(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso, max_dist: f64) -> Option<Separation> the gap (negative: depth) between convex shape and target if it is at most max_dist; for a mesh the nearest triangle

  • fn intersects(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso) -> bool do convex shape and target touch or overlap

  • fn contacts(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso, margin: f64, out: &mut Vec<Contact>) the contacts of convex shape with target whose gap is at most margin (0: only touching and overlapping), appended to out (a mesh: one per triangle, by triangle)

  • fn closest_point(target: &Shape, target_iso: &Iso, point: Vec3, max_dist: f64) -> Option<PointHit> the point of target nearest to point, if within max_dist

mod brute

The same queries without a mesh's tree: every triangle in turn, through the same per-triangle code and tie-breaks, so the results must equal those of the tree bit for bit. For tests (apps/physicstest) and for checking a mesh that behaves oddly; for convex shapes they are the queries above.

  • fn ray(target: &Shape, target_iso: &Iso, origin: Vec3, dir: Vec3, max_t: f64) -> Option<RayHit> the first hit, every triangle of a mesh tested

  • fn ray_all(target: &Shape, target_iso: &Iso, origin: Vec3, dir: Vec3, max_t: f64, out: &mut Vec<RayHit>) every hit, every triangle of a mesh tested

  • fn cast(shape: &Shape, iso: &Iso, motion: Vec3, target: &Shape, target_iso: &Iso) -> Option<CastHit> the first contact, every triangle of a mesh tested

  • fn separation(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso, max_dist: f64) -> Option<Separation> the nearest triangle of a mesh without its tree

  • fn intersects(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso) -> bool touch or overlap, every triangle of a mesh tested

  • fn contacts(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso, margin: f64, out: &mut Vec<Contact>) the contacts, every triangle of a mesh tested

  • fn closest_point(target: &Shape, target_iso: &Iso, point: Vec3, max_dist: f64) -> Option<PointHit> the nearest point, every triangle of a mesh tested

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