use core::f64::consts::FRAC_PI_2;
use glam::{DQuat, DVec3};
use crate::broadphase::{Ray, Segment};
use crate::collider::{Aabb, Capsule, LyingCapsule};
use crate::components::{ColliderComponent, RigidBody, Transform};
use crate::compound_shape::{CompoundPart, CompoundShape};
use crate::query::ShapeHit;
use crate::system::PhysicsSystem;
use super::{ColliderId, PhysicsWorld, QueryFilter};
const EXACT: f64 = 1e-12;
const LEFT: usize = 0;
const RIGHT: usize = 1;
const BASE: usize = 2;
fn u_shape() -> CompoundShape {
let part = |centre: DVec3, half: DVec3| CompoundPart::new(centre, DQuat::IDENTITY, half);
CompoundShape::new(vec![
part(DVec3::new(-1.5, 0.0, 0.0), DVec3::new(0.5, 1.0, 1.0)),
part(DVec3::new(1.5, 0.0, 0.0), DVec3::new(0.5, 1.0, 1.0)),
part(DVec3::new(0.0, -1.5, 0.0), DVec3::new(2.0, 0.5, 1.0)),
])
.expect("a valid shape")
}
fn world() -> (PhysicsWorld, ColliderId) {
let mut world = PhysicsWorld::new();
let id = world.add_compound(&u_shape(), DVec3::ZERO, &Transform::IDENTITY);
(world, id)
}
fn close(a: DVec3, b: DVec3) -> bool {
(a - b).length() < EXACT
}
#[test]
fn a_ray_down_the_gap_hits_the_base_below_it() {
let (mut world, id) = world();
let (hit, at) = world
.cast_ray(&Ray::new(DVec3::new(0.0, 5.0, 0.0), DVec3::NEG_Y))
.expect("the base");
assert_eq!((hit, at.part), (id, BASE));
assert!((at.t - 6.0).abs() < EXACT, "{at:?}");
assert!(close(at.point, DVec3::new(0.0, -1.0, 0.0)), "{at:?}");
assert_eq!(at.normal, DVec3::Y);
}
#[test]
fn rays_and_sweeps_through_the_gap_miss() {
let (mut world, _) = world();
let along = Segment::new(DVec3::new(0.0, 0.0, -5.0), DVec3::new(0.0, 0.0, 5.0));
assert_eq!(
world.cast_ray(&Ray::new(along.start, along.end - along.start)),
None
);
assert_eq!(world.sweep_sphere(&along, 0.5), None);
assert_eq!(world.sweep_capsule(&along, 0.3, 0.4), None);
let mut hits = Vec::new();
world.sweep_sphere_all(&along, 0.5, QueryFilter::ALL, &mut hits);
assert!(hits.is_empty(), "{hits:?}");
}
#[test]
fn overlaps_and_push_outs_in_the_gap_find_nothing() {
let (mut world, id) = world();
assert!(world.overlap_sphere(DVec3::ZERO, 0.5).is_empty());
let small = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(0.25));
assert!(world.overlap_aabb(&small).is_empty());
let mut out = Vec::new();
world.capsule_penetrations_into(&Capsule::new(DVec3::ZERO, 0.3, 0.4), None, &mut out);
assert!(out.is_empty(), "{out:?}");
let lying = LyingCapsule::new(DVec3::new(0.0, 0.0, -0.5), 0.0, 0.3, 1.0);
assert_eq!(world.lying_capsule_blocker(&lying, QueryFilter::ALL), None);
world.capsule_penetrations_into(
&Capsule::new(DVec3::new(0.8, 0.0, 0.0), 0.3, 0.4),
None,
&mut out,
);
let [(hit, push)] = out[..] else {
panic!("one push-out, not {out:?}");
};
assert_eq!(hit, id);
assert!((push.depth - 0.1).abs() < EXACT, "{push:?}");
assert!(close(push.normal, DVec3::NEG_X), "{push:?}");
}
#[test]
fn the_candidate_sweeps_report_each_part() {
let (mut world, id) = world();
let path = Segment::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
let radius = 0.25;
for capsule in [false, true] {
let mut hits = Vec::new();
let closest = if capsule {
world.sweep_capsule_all(&path, radius, 0.5, QueryFilter::ALL, &mut hits);
world.sweep_capsule(&path, radius, 0.5)
} else {
world.sweep_sphere_all(&path, radius, QueryFilter::ALL, &mut hits);
world.sweep_sphere(&path, radius)
};
let [(first, left), (second, right)] = hits[..] else {
panic!("both columns, not {hits:?} (capsule {capsule})");
};
assert_eq!((first, left.part), (id, LEFT));
assert_eq!((second, right.part), (id, RIGHT));
assert!((left.t - (3.0 - radius) / 10.0).abs() < EXACT, "{left:?}");
assert!((right.t - (6.0 - radius) / 10.0).abs() < EXACT, "{right:?}");
assert_eq!(left.normal, DVec3::NEG_X);
assert_eq!(right.normal, DVec3::NEG_X);
assert_eq!(closest, Some(hits[0]), "capsule {capsule}");
}
}
#[test]
fn parts_met_together_come_in_part_order() {
let mut world = PhysicsWorld::new();
let part = |y: f64| CompoundPart::new(DVec3::new(4.0, y, 0.0), DQuat::IDENTITY, DVec3::ONE);
let shape = CompoundShape::new(vec![part(1.0), part(-1.0)]).expect("a valid shape");
let id = world.add_compound(&shape, DVec3::ZERO, &Transform::IDENTITY);
let path = Segment::new(DVec3::ZERO, DVec3::new(5.0, 0.0, 0.0));
let mut hits: Vec<(ColliderId, ShapeHit)> = Vec::new();
world.sweep_sphere_all(&path, 0.5, QueryFilter::ALL, &mut hits);
let parts: Vec<_> = hits.iter().map(|&(hit, at)| (hit, at.part)).collect();
assert_eq!(parts, [(id, 0), (id, 1)]);
assert_eq!(hits[0].1.t, hits[1].1.t, "a tie");
assert_eq!(world.sweep_sphere(&path, 0.5), Some(hits[0]));
let ray = Ray::new(DVec3::ZERO, DVec3::X);
assert_eq!(world.cast_ray(&ray).map(|(_, at)| at.part), Some(0));
}
#[test]
fn a_bodys_compound_turns_with_the_body_part_by_part() {
let entity =
crcbl_ecs::Entity::from_bits((1u64 << 32) | 5).expect("generation 1 is never zero");
let transform = Transform::new(
DVec3::ZERO,
crate::rotation_from_scaled_axis(DVec3::Y * FRAC_PI_2),
);
let mut phys = PhysicsSystem::new();
phys.set_body(entity, RigidBody::new_kinematic());
phys.set_transform(entity, transform);
phys.set_collider(
entity,
&ColliderComponent::Compound {
offset: DVec3::ZERO,
shape: u_shape(),
is_trigger: false,
},
&transform,
);
let id = phys.collider_of(entity).expect("a collider");
let onto = Ray::new(DVec3::new(0.0, 5.0, -1.5), DVec3::NEG_Y);
let (hit, at) = phys.world_mut().cast_ray(&onto).expect("the column");
assert_eq!((hit, at.part), (id, RIGHT));
assert!((at.t - 4.0).abs() < 1e-9, "{at:?}");
let unturned = Ray::new(DVec3::new(1.5, 5.0, 0.0), DVec3::NEG_Y);
assert_eq!(phys.cast_ray(&unturned), None);
phys.step(1.0 / 60.0);
assert_eq!(phys.cast_ray(&onto).map(|(hit, _)| hit), Some(entity));
phys.set_transform(entity, Transform::from_position(DVec3::new(10.0, 0.0, 0.0)));
let down = |x: f64| Ray::new(DVec3::new(x, 5.0, 0.0), DVec3::NEG_Y);
let (hit, at) = phys.world_mut().cast_ray(&down(11.5)).expect("the column");
assert_eq!((hit, at.part), (id, RIGHT));
let (hit, at) = phys.world_mut().cast_ray(&down(10.0)).expect("the base");
assert_eq!((hit, at.part), (id, BASE));
assert_eq!(phys.cast_ray(&onto), None, "nothing left where it stood");
}
#[test]
fn placing_a_compound_again_reuses_its_parts_buffer() {
let (mut world, id) = world();
let parts = |world: &PhysicsWorld| {
let slot = world.slot_of(id).expect("a live collider");
match &world.colliders[slot].as_ref().expect("a filled slot").entry {
super::ColliderEntry::Compound(placed) => placed.parts().as_ptr(),
other => panic!("not a compound: {other:?}"),
}
};
let before = parts(&world);
let moved = Transform::from_position(DVec3::new(10.0, 0.0, 0.0));
assert!(world.set_compound(id, &u_shape(), DVec3::ZERO, &moved));
assert_eq!(parts(&world), before, "a new buffer");
let down = |x: f64| Ray::new(DVec3::new(x, 5.0, 0.0), DVec3::NEG_Y);
assert_eq!(world.cast_ray(&down(1.5)), None, "the U has moved away");
let (hit, at) = world.cast_ray(&down(11.5)).expect("the right column");
assert_eq!((hit, at.part), (id, RIGHT));
}
#[test]
fn a_push_out_is_the_deepest_part_and_ties_go_to_the_lower() {
let column = |x: f64| {
CompoundPart::new(
DVec3::new(x, 0.0, 0.0),
DQuat::IDENTITY,
DVec3::new(0.5, 1.0, 1.0),
)
};
let shape = CompoundShape::new(vec![column(-1.5), column(1.5)]).expect("a valid shape");
let mut world = PhysicsWorld::new();
let id = world.add_compound(&shape, DVec3::ZERO, &Transform::IDENTITY);
let mut out = Vec::new();
world.capsule_penetrations_into(&Capsule::new(DVec3::ZERO, 1.1, 0.0), None, &mut out);
let [(hit, push)] = out[..] else {
panic!("one push-out, not {out:?}");
};
assert_eq!(hit, id);
assert!((push.depth - 0.1).abs() < EXACT, "{push:?}");
assert!(
close(push.normal, DVec3::X),
"out of the lower part: {push:?}"
);
world.capsule_penetrations_into(
&Capsule::new(DVec3::new(0.05, 0.0, 0.0), 1.1, 0.0),
None,
&mut out,
);
let [(_, push)] = out[..] else {
panic!("one push-out, not {out:?}");
};
assert!((push.depth - 0.15).abs() < EXACT, "{push:?}");
assert!(
close(push.normal, DVec3::NEG_X),
"out of the deeper part: {push:?}"
);
}
#[test]
fn many_tied_parts_are_sorted_into_part_order() {
let parts: Vec<_> = (0..CompoundShape::MAX_PARTS)
.map(|i| {
let face = 5.0 - (i % 3) as f64;
CompoundPart::new(
DVec3::new(face + 0.5, 0.0, 0.0),
DQuat::IDENTITY,
DVec3::splat(0.5),
)
})
.collect();
let shape = CompoundShape::new(parts).expect("a valid shape");
let mut world = PhysicsWorld::new();
for _ in 0..3 {
world.add_compound(&shape, DVec3::ZERO, &Transform::IDENTITY);
}
let path = Segment::new(DVec3::ZERO, DVec3::new(10.0, 0.0, 0.0));
for capsule in [false, true] {
let mut hits: Vec<(ColliderId, ShapeHit)> = Vec::new();
if capsule {
world.sweep_capsule_all(&path, 0.25, 0.1, QueryFilter::ALL, &mut hits);
} else {
world.sweep_sphere_all(&path, 0.25, QueryFilter::ALL, &mut hits);
}
assert_eq!(
hits.len(),
3 * CompoundShape::MAX_PARTS,
"capsule {capsule}"
);
let order: Vec<_> = hits
.iter()
.map(|&(id, at)| (at.t.to_bits(), id.index(), at.part))
.collect();
let mut sorted = order.clone();
sorted.sort_unstable();
assert_eq!(order, sorted, "capsule {capsule}");
}
}