use crcbl_ecs::Entity;
use crcbl_phys::{
CONTACT_NORMAL_LENGTH, ColliderComponent, ContactSettings, GravityForce, MassProperties,
PhysicsDebugLine, PhysicsSystem, RigidBody, SurfaceMaterial, Transform,
};
use glam::DVec3;
const DT: f64 = 1.0 / 60.0;
const MASS: f64 = 10.0;
const HALF: DVec3 = DVec3::splat(0.25);
const NEAR: f64 = 1e-3;
const EDGES: usize = 12;
const SETTLE_TICKS: usize = 300;
fn entity(index: u32) -> Entity {
Entity::from_bits((1u64 << 32) | u64::from(index)).expect("generation 1 is never zero")
}
fn system() -> PhysicsSystem {
let mut physics = PhysicsSystem::with_contacts(ContactSettings::DEFAULT);
physics.add_force_provider(Box::new(GravityForce::EARTH));
physics.add_plane(DVec3::Y, 0.0, SurfaceMaterial::DEFAULT);
physics
}
fn place(physics: &mut PhysicsSystem, index: u32, at: DVec3, body: Option<RigidBody>) {
let e = entity(index);
if let Some(body) = body {
physics.set_body(e, body);
}
let transform = Transform::from_position(at);
physics.set_transform(e, transform);
physics.set_collider(
e,
&ColliderComponent::Box {
offset: DVec3::ZERO,
half_extents: HALF,
is_trigger: false,
},
&transform,
);
}
fn crate_(physics: &mut PhysicsSystem, index: u32, at: DVec3) {
let inertia = MassProperties::cuboid(MASS, HALF, DVec3::ZERO).inertia;
let body = RigidBody::new_dynamic(MASS).with_inertia(inertia);
place(physics, index, at, Some(body));
}
fn lines(physics: &PhysicsSystem) -> Vec<(DVec3, DVec3, PhysicsDebugLine)> {
let mut lines = Vec::new();
physics.debug_lines(&mut |from, to, kind| lines.push((from, to, kind)));
lines
}
fn boxed_at(drawn: &[(DVec3, DVec3, PhysicsDebugLine)], centre: DVec3) -> Vec<PhysicsDebugLine> {
let corner = |point: DVec3| (point - centre).abs().distance(HALF) < NEAR;
drawn
.iter()
.filter(|(from, to, kind)| {
let edge = (from.distance(*to) - 2.0 * HALF.x).abs() < NEAR;
matches!(kind, PhysicsDebugLine::Bounds { .. }) && corner(*from) && corner(*to) && edge
})
.map(|&(_, _, kind)| kind)
.collect()
}
#[test]
fn each_collider_is_boxed_telling_islands_and_sleep_apart() {
let mut physics = system();
let bottom = DVec3::new(0.0, HALF.y, 0.0);
let top = DVec3::new(HALF.x / 2.0, 3.0 * HALF.y, 0.0);
let apart = DVec3::new(3.0, HALF.y, 0.0);
let slab = DVec3::new(-3.0, HALF.y, 0.0);
crate_(&mut physics, 0, bottom);
crate_(&mut physics, 1, top);
crate_(&mut physics, 2, apart);
place(&mut physics, 3, slab, None);
physics.step(DT);
let drawn = lines(&physics);
let island = |centre| {
let kinds = boxed_at(&drawn, centre);
assert_eq!(kinds.len(), EDGES, "the box at {centre} drew {kinds:?}");
assert!(kinds.windows(2).all(|pair| pair[0] == pair[1]));
match kinds[0] {
PhysicsDebugLine::Bounds { sleeping, island } => {
assert!(!sleeping, "the box at {centre} sleeps after one tick");
island
}
PhysicsDebugLine::Contact { .. } => unreachable!("filtered to bounds"),
}
};
let stack = island(bottom);
assert!(stack.is_some(), "a stepped crate in no island");
assert_eq!(island(top), stack, "the stack in two islands");
assert!(island(apart).is_some_and(|own| Some(own) != stack));
assert_eq!(island(slab), None, "a static slab in an island");
let settled = (0..SETTLE_TICKS).any(|_| {
physics.step(DT);
physics.contact_counters().bodies == 0
});
assert!(settled, "the crates never slept");
let drawn = lines(&physics);
for centre in [bottom, top, apart] {
let kinds = boxed_at(&drawn, centre);
assert_eq!(kinds.len(), EDGES, "the box at {centre} drew {kinds:?}");
assert!(
kinds
.iter()
.all(|kind| matches!(kind, PhysicsDebugLine::Bounds { sleeping: true, .. })),
"the crate at {centre} not drawn asleep"
);
}
assert!(
boxed_at(&drawn, slab).iter().all(|kind| matches!(
kind,
PhysicsDebugLine::Bounds {
sleeping: false,
..
}
)),
"the slab drawn asleep"
);
}
#[derive(Debug)]
struct Drawn {
foot: DVec3,
normal: DVec3,
touching: bool,
impulse: f64,
feature: u32,
}
fn contacts(physics: &PhysicsSystem) -> Vec<Drawn> {
lines(physics)
.into_iter()
.filter_map(|(from, to, kind)| match kind {
PhysicsDebugLine::Contact {
touching,
impulse,
feature,
} => Some(Drawn {
foot: from,
normal: to - from,
touching,
impulse,
feature,
}),
PhysicsDebugLine::Bounds { .. } => None,
})
.collect()
}
#[test]
fn each_contact_point_draws_its_normal_impulse_and_feature() {
let mut physics = system();
crate_(&mut physics, 0, DVec3::new(0.0, HALF.y, 0.0));
physics.step(DT);
let first = contacts(&physics);
assert_eq!(first.len(), 4, "{first:?}");
for contact in &first {
assert!(contact.touching, "a resting corner not touching");
let foot = contact.foot;
assert!(foot.y.abs() < NEAR, "a contact off the floor at {foot}");
assert!(
(foot.x.abs() - HALF.x).abs() < NEAR && (foot.z.abs() - HALF.z).abs() < NEAR,
"a contact off the corners at {foot}"
);
let normal = contact.normal;
assert!(
(normal.length() - CONTACT_NORMAL_LENGTH).abs() < 1e-12
&& normal.normalize().abs().distance(DVec3::Y) < 1e-9,
"the normal drawn as {normal}"
);
let [report] = physics.contacts()[..] else {
panic!("the crate meets more than the floor");
};
assert!(
normal.normalize().distance(report.manifold.normal) < 1e-9,
"drawn along {normal}, not the manifold's {}",
report.manifold.normal
);
}
let substeps = f64::from(ContactSettings::DEFAULT.substeps);
let weight = MASS * -GravityForce::EARTH.acceleration.y * DT / substeps;
let held: f64 = first.iter().map(|contact| contact.impulse).sum();
assert!(
(held - weight).abs() < weight * 0.05,
"the corners hold {held}, not the substep's weight {weight}"
);
let features = |contacts: &[Drawn]| {
let mut features: Vec<u32> = contacts.iter().map(|contact| contact.feature).collect();
features.sort_unstable();
features
};
physics.step(DT);
let second = contacts(&physics);
assert_eq!(features(&second), features(&first), "the features moved");
assert!(
features(&first).windows(2).all(|pair| pair[0] != pair[1]),
"two corners share a feature"
);
}
#[test]
fn a_speculative_point_is_drawn_not_touching() {
let mut physics = system();
let gap = ContactSettings::DEFAULT.speculative_distance / 2.0;
crate_(&mut physics, 0, DVec3::new(0.0, HALF.y + gap, 0.0));
physics.step(DT);
let drawn = contacts(&physics);
assert!(
!drawn.is_empty(),
"no contact inside the speculative distance"
);
assert!(drawn.iter().all(|contact| !contact.touching), "{drawn:?}");
}
#[test]
fn without_contacts_only_boxes_are_drawn() {
let mut physics = PhysicsSystem::new();
let at = DVec3::new(0.0, 2.0, 0.0);
crate_(&mut physics, 0, at);
physics.step(DT);
let drawn = lines(&physics);
let kinds = boxed_at(&drawn, at);
assert_eq!(kinds.len(), EDGES);
assert_eq!(drawn.len(), EDGES, "more than the box drawn");
let alone = PhysicsDebugLine::Bounds {
sleeping: false,
island: None,
};
assert!(kinds.iter().all(|kind| *kind == alone), "{kinds:?}");
}