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 RayHitwhere a ray hitst: f64-- the ray parameter: point = origin + dir * t (metres when dir is a unit vector)point: Vec3-- origin + dir * tnormal: 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 CastHitwhere a moving shape first touches anotherfraction: 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 surfacenormal: Vec3-- the unit normal of that surface towards the moving shapetriangle: 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 Contacthow two shapes touch or nearly touchpoint: 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 Separationthe distance between two shapesdistance: f64-- the gap; negative: the penetration depthpoint_a: Vec3-- the nearest (or deepest) points on the query shape and on the other onepoint_b: Vec3-- on the other shapenormal: Vec3-- the unit normal from the other shape towards the query shapetriangle: u32-- the nearest triangle of a mesh, or [NO_TRIANGLE]
struct PointHitthe point of a shape nearest to a given pointpoint: Vec3-- the nearest point of the surface, or the point itself when it is inside a convex shapedistance: f64-- 0 insideinside: bool-- the point lies inside a convex shapetriangle: 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) ontargetplaced bytarget_isofn ray_all(target: &Shape, target_iso: &Iso, origin: Vec3, dir: Vec3, max_t: f64, out: &mut Vec<RayHit>)every hit of the ray ontarget(one for a convex shape, one per triangle for a mesh), appended tooutsorted by t and then by trianglefn cast(shape: &Shape, iso: &Iso, motion: Vec3, target: &Shape, target_iso: &Iso) -> Option<CastHit>the first contact of convexshape, moved fromisobymotion, withtarget. 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 convexshapeandtargetif it is at mostmax_dist; for a mesh the nearest trianglefn intersects(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso) -> booldo convexshapeandtargettouch or overlapfn contacts(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso, margin: f64, out: &mut Vec<Contact>)the contacts of convexshapewithtargetwhose gap is at mostmargin(0: only touching and overlapping), appended toout(a mesh: one per triangle, by triangle)fn closest_point(target: &Shape, target_iso: &Iso, point: Vec3, max_dist: f64) -> Option<PointHit>the point oftargetnearest topoint, if withinmax_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 testedfn 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 testedfn cast(shape: &Shape, iso: &Iso, motion: Vec3, target: &Shape, target_iso: &Iso) -> Option<CastHit>the first contact, every triangle of a mesh testedfn separation(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso, max_dist: f64) -> Option<Separation>the nearest triangle of a mesh without its treefn intersects(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso) -> booltouch or overlap, every triangle of a mesh testedfn contacts(shape: &Shape, iso: &Iso, target: &Shape, target_iso: &Iso, margin: f64, out: &mut Vec<Contact>)the contacts, every triangle of a mesh testedfn closest_point(target: &Shape, target_iso: &Iso, point: Vec3, max_dist: f64) -> Option<PointHit>the nearest point, every triangle of a mesh tested