use glam::DVec3;
use super::{BodySet, PhysicsSystem};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PhysicsDebugLine {
Bounds {
sleeping: bool,
island: Option<u32>,
},
Contact {
touching: bool,
impulse: f64,
feature: u32,
},
}
pub const CONTACT_NORMAL_LENGTH: f64 = 0.1;
impl PhysicsSystem {
pub fn debug_lines(&self, line: &mut impl FnMut(DVec3, DVec3, PhysicsDebugLine)) {
for (_, record) in self.records.iter() {
let Some(aabb) = record
.collider
.as_ref()
.and_then(|(collider, _)| self.world.aabb_of(*collider))
else {
continue;
};
let kind = PhysicsDebugLine::Bounds {
sleeping: record.set == BodySet::Sleeping,
island: record.island,
};
let corner = |index: usize| {
let pick = |bit: usize, axis: usize| {
if index & bit == 0 {
aabb.min[axis]
} else {
aabb.max[axis]
}
};
DVec3::new(pick(1, 0), pick(2, 1), pick(4, 2))
};
for index in 0..8 {
for bit in [1, 2, 4] {
if index & bit == 0 {
line(corner(index), corner(index | bit), kind);
}
}
}
}
let Some(pipeline) = &self.contacts else {
return;
};
for report in pipeline.reports(&self.records) {
let normal = report.manifold.normal * CONTACT_NORMAL_LENGTH;
for (point, &impulse) in report.manifold.points().iter().zip(&report.normal_impulses) {
let kind = PhysicsDebugLine::Contact {
touching: point.separation <= 0.0,
impulse,
feature: point.id,
};
line(point.point, point.point + normal, kind);
}
}
}
}