Skip to main content

crcbl_phys/system/
debug.rs

1//! [`PhysicsSystem::debug_lines`]: the physics debug view as line segments,
2//! for a debug draw layer to show.
3//!
4//! The crate depends on no renderer, and [`DebugCtx`](crcbl_ecs::DebugCtx)
5//! carries nothing to draw into, so the system's
6//! [`SystemTrait::debug_draw`](crcbl_ecs::SystemTrait::debug_draw) still draws
7//! nothing: a caller asks for the segments instead and colours each by the
8//! [`PhysicsDebugLine`] it comes with.
9
10use glam::DVec3;
11
12use super::{BodySet, PhysicsSystem};
13
14/// What a segment from [`PhysicsSystem::debug_lines`] shows, for the caller
15/// to colour.
16#[derive(Clone, Copy, Debug, PartialEq)]
17pub enum PhysicsDebugLine {
18    /// An edge of an entity's collider's bounding box in the world.
19    Bounds {
20        /// The body sleeps.
21        sleeping: bool,
22        /// Its island, as a number to colour by: one for every body the solver
23        /// steps together, and reused once that island is gone. [`None`] for
24        /// an entity in no island — one with no body, a body not yet stepped,
25        /// and every body in a system without contacts.
26        island: Option<u32>,
27    },
28    /// From a contact point, [`CONTACT_NORMAL_LENGTH`] along the contact's
29    /// normal, which points from its lower-ranked shape into the other.
30    Contact {
31        /// The surfaces touch or overlap there, rather than standing near
32        /// enough apart that the solver keeps the point in case they meet.
33        touching: bool,
34        /// The normal impulse the last step left on the point: its last
35        /// substep's, so a body at rest carries its weight over one substep
36        /// between its points.
37        impulse: f64,
38        /// The manifold feature the point belongs to, stable while the shapes
39        /// stay in the same configuration: one that changes every frame on a
40        /// still pile is warm starting lost.
41        feature: u32,
42    },
43}
44
45/// How long a drawn contact normal is, in metres.
46pub const CONTACT_NORMAL_LENGTH: f64 = 0.1;
47
48impl PhysicsSystem {
49    /// Hands `line` every segment of the physics debug view as the last
50    /// [`step`](Self::step) left it, each tagged with the [`PhysicsDebugLine`]
51    /// it is: the twelve edges of each entity's collider's bounding box,
52    /// telling sleeping bodies and islands apart, then each contact point's
53    /// normal, with its impulse and its feature. A plane has no box drawn; a
54    /// contact with one has its normal drawn as any other.
55    pub fn debug_lines(&self, line: &mut impl FnMut(DVec3, DVec3, PhysicsDebugLine)) {
56        for (_, record) in self.records.iter() {
57            let Some(aabb) = record
58                .collider
59                .as_ref()
60                .and_then(|(collider, _)| self.world.aabb_of(*collider))
61            else {
62                continue;
63            };
64            let kind = PhysicsDebugLine::Bounds {
65                sleeping: record.set == BodySet::Sleeping,
66                island: record.island,
67            };
68            let corner = |index: usize| {
69                let pick = |bit: usize, axis: usize| {
70                    if index & bit == 0 {
71                        aabb.min[axis]
72                    } else {
73                        aabb.max[axis]
74                    }
75                };
76                DVec3::new(pick(1, 0), pick(2, 1), pick(4, 2))
77            };
78            for index in 0..8 {
79                for bit in [1, 2, 4] {
80                    if index & bit == 0 {
81                        line(corner(index), corner(index | bit), kind);
82                    }
83                }
84            }
85        }
86        let Some(pipeline) = &self.contacts else {
87            return;
88        };
89        for report in pipeline.reports(&self.records) {
90            let normal = report.manifold.normal * CONTACT_NORMAL_LENGTH;
91            for (point, &impulse) in report.manifold.points().iter().zip(&report.normal_impulses) {
92                let kind = PhysicsDebugLine::Contact {
93                    touching: point.separation <= 0.0,
94                    impulse,
95                    feature: point.id,
96                };
97                line(point.point, point.point + normal, kind);
98            }
99        }
100    }
101}