Skip to main content

crcbl_phys/query/
sphere_overlap.rs

1//! A sphere's overlap with one shape, measured: how deep, which way out, and
2//! where.
3//!
4//! [`OverlapHit`] is what the overlap queries answer per collider. Every
5//! function here is the push-out the capsule queries already compute
6//! ([`Penetration`]), asked for a capsule of no height — which is a sphere —
7//! with the contact point added.
8//!
9//! A turned box and a turned capsule are measured in their own frames, as
10//! [`super::sphere_overlaps_box`] tests one, by the forms beside it in
11//! [`super::boxes`] and [`super::capsules`]: a sphere is the same in any frame
12//! and a rotation keeps lengths, so only the point and the normal are turned
13//! back out. A mesh is measured by its own capsule push-out, against the
14//! triangle the sphere is deepest in.
15//!
16//! # A hit is a positive depth, exactly
17//!
18//! A sphere that only touches a shape is not inside it, and is not a hit: the
19//! depth has to be strictly positive in `f64`, with no tolerance either way,
20//! the rule [`Penetration`] keeps. Two shapes apart by less than rounding can
21//! therefore read either side of the line; nothing here pads it.
22//!
23//! # The normal where there is no direction to escape along
24//!
25//! A centre exactly on a sphere's centre, or on a capsule's core, is equally
26//! far from every point of the surface. The normal there is `+Y` — the shape's
27//! own `+Y` for a turned capsule, its core — which is the fallback the sweeps
28//! and the capsule push-out already take for the same case. A centre inside a
29//! box leaves through the nearest face, and of faces equally near through the
30//! first of `-X`, `+X`, `-Y`, `+Y`, `-Z`, `+Z` in the box's own frame, which is
31//! the minimum-penetration rule the capsule push-out uses.
32//!
33//! "Exactly" is as the arithmetic has it. A centre that is on a capsule's core
34//! in exact numbers can land a rounding off it in `f64` — the nearest core
35//! point is worked out along the core, and need not come back to the bit —
36//! and then leaves along that rounding's direction: deterministic, the same
37//! on every run and target, but no more meaningful than which way it rounded.
38//! A centre on a mesh's triangle leaves along the triangle's own normal, the
39//! side its winding faces.
40
41use glam::DVec3;
42
43use super::{
44    Penetration, capsule_penetration_vs_aabb, capsule_penetration_vs_capsule,
45    capsule_penetration_vs_sphere,
46};
47use crate::collider::{Aabb, Capsule, Sphere};
48
49/// Where, how deep and which way a query sphere overlaps one collider.
50///
51/// The overlap queries' answer per collider —
52/// [`PhysicsWorld::overlap_sphere`](crate::PhysicsWorld::overlap_sphere) and
53/// every form of it. Not a [`ShapeHit`](super::ShapeHit): an overlap has no
54/// time of impact, so a ray parameter would be a field with no meaning, and
55/// the depth a sweep cannot give is the measurement an overlap exists for.
56#[derive(Debug, Clone, Copy, PartialEq)]
57pub struct OverlapHit {
58    /// The point of the collider's surface the query sphere is pushed out
59    /// from: moved `depth` along `normal`, the sphere touches the collider
60    /// here.
61    pub point: DVec3,
62    /// Unit direction to move the query sphere along to leave the collider,
63    /// pointing out of the collider.
64    pub normal: DVec3,
65    /// How far along `normal` the sphere has to move. Always positive — see
66    /// the module docs.
67    pub depth: f64,
68    /// Which part of the collider the sphere is deepest in: for a compound
69    /// ([`crate::PhysicsWorld::add_compound`]) the index of the part in its
70    /// [`CompoundShape::parts`](crate::CompoundShape::parts), and `0` for
71    /// every other collider and from every shape-level function here.
72    pub part: usize,
73}
74
75impl OverlapHit {
76    /// The hit for `sphere` pushed out by `penetration`: the surface point is
77    /// where the sphere's deepest point lands once it has moved out, which for
78    /// a centre outside the shape is the shape's point nearest the centre.
79    pub(crate) fn from_penetration(sphere: &Sphere, penetration: Penetration) -> Self {
80        let Penetration { normal, depth } = penetration;
81        Self {
82            point: sphere.centre - normal * (sphere.radius - depth),
83            normal,
84            depth,
85            part: 0,
86        }
87    }
88}
89
90/// `sphere` as the capsule queries take one: a capsule of no height.
91fn as_capsule(sphere: &Sphere) -> Capsule {
92    Capsule::new(sphere.centre, sphere.radius, 0.0)
93}
94
95/// How `sphere` overlaps another sphere, or `None` if they are apart or only
96/// touch.
97#[must_use]
98pub fn sphere_overlap_vs_sphere(sphere: &Sphere, target: &Sphere) -> Option<OverlapHit> {
99    let penetration = capsule_penetration_vs_sphere(&as_capsule(sphere), target)?;
100    Some(OverlapHit::from_penetration(sphere, penetration))
101}
102
103/// How `sphere` overlaps an AABB, or `None` if they are apart or only touch.
104#[must_use]
105pub fn sphere_overlap_vs_aabb(sphere: &Sphere, target: &Aabb) -> Option<OverlapHit> {
106    let penetration = capsule_penetration_vs_aabb(&as_capsule(sphere), target)?;
107    Some(OverlapHit::from_penetration(sphere, penetration))
108}
109
110/// How `sphere` overlaps a Y-aligned capsule, or `None` if they are apart or
111/// only touch.
112#[must_use]
113pub fn sphere_overlap_vs_capsule(sphere: &Sphere, target: &Capsule) -> Option<OverlapHit> {
114    let penetration = capsule_penetration_vs_capsule(&as_capsule(sphere), target)?;
115    Some(OverlapHit::from_penetration(sphere, penetration))
116}
117
118#[cfg(test)]
119mod tests;