use core::f64::consts::FRAC_PI_2;
use glam::{DQuat, DVec3};
use crate::broadphase::{Ray, Segment};
use crate::collider::{Capsule, LyingCapsule};
use crate::components::{ColliderComponent, RigidBody, Transform};
use crate::system::PhysicsSystem;
use super::{PhysicsWorld, QueryFilter};
const CLOSE: f64 = 1e-12;
const SWEEP_TOLERANCE: f64 = 5e-3;
const RADIUS: f64 = 0.5;
const HALF_HEIGHT: f64 = 1.0;
fn lay_down() -> DQuat {
crate::rotation_from_scaled_axis(DVec3::Z * FRAC_PI_2)
}
fn capsule() -> Capsule {
Capsule::new(DVec3::ZERO, RADIUS, HALF_HEIGHT)
}
fn worlds() -> (PhysicsWorld, PhysicsWorld) {
let mut lying = PhysicsWorld::new();
lying.add_turned_capsule(capsule(), lay_down());
let mut upright = PhysicsWorld::new();
upright.add_capsule(capsule());
(lying, upright)
}
fn close(a: DVec3, b: DVec3) -> bool {
(a - b).length() < CLOSE
}
#[test]
fn a_ray_meets_the_turned_core() {
let (mut lying, mut upright) = worlds();
let down = Ray::new(DVec3::new(1.2, 5.0, 0.0), DVec3::NEG_Y);
let (_, hit) = lying.cast_ray(&down).expect("the lying capsule");
let y = (RADIUS * RADIUS - 0.2 * 0.2).sqrt();
assert!((hit.t - (5.0 - y)).abs() < CLOSE, "{hit:?}");
assert!(
close(hit.normal, DVec3::new(0.2, y, 0.0) / RADIUS),
"{hit:?}"
);
assert_eq!(upright.cast_ray(&down), None);
let along = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X);
let (_, hit) = lying.cast_ray(&along).expect("the lying capsule");
assert!((hit.t - 3.5).abs() < CLOSE, "the cap at x = -1.5: {hit:?}");
assert!(close(hit.normal, DVec3::NEG_X), "{hit:?}");
let (_, hit) = upright.cast_ray(&along).expect("the upright capsule");
assert!((hit.t - 4.5).abs() < CLOSE, "its side at x = -0.5: {hit:?}");
}
#[test]
fn a_sphere_sweep_and_overlap_meet_the_turned_core() {
let (mut lying, mut upright) = worlds();
let down = Segment::new(DVec3::new(0.9, 5.0, 0.0), DVec3::new(0.9, -5.0, 0.0));
let (_, hit) = lying.sweep_sphere(&down, 0.25).expect("the lying capsule");
assert!((hit.t - (5.0 - 0.75) / 10.0).abs() < CLOSE, "{hit:?}");
assert!(close(hit.normal, DVec3::Y), "{hit:?}");
assert!(close(hit.point, DVec3::new(0.9, 0.5, 0.0)), "{hit:?}");
assert_eq!(upright.sweep_sphere(&down, 0.25), None);
let near_the_end = DVec3::new(1.3, 0.6, 0.0);
assert_eq!(lying.overlap_sphere(near_the_end, 0.2).len(), 1);
assert!(upright.overlap_sphere(near_the_end, 0.2).is_empty());
}
#[test]
fn an_upright_capsule_is_pushed_out_and_stopped_by_the_turned_core() {
let (mut lying, mut upright) = worlds();
let character = Capsule::new(DVec3::new(1.2, 0.9, 0.0), 0.5, 0.5);
let mut out = Vec::new();
lying.capsule_penetrations_into(&character, None, &mut out);
let [(_, push)] = out[..] else {
panic!("one push-out, not {out:?}");
};
let between = DVec3::new(0.2, 0.4, 0.0);
assert!(
(push.depth - (1.0 - between.length())).abs() < CLOSE,
"{push:?}"
);
assert!(close(push.normal, between.normalize()), "{push:?}");
upright.capsule_penetrations_into(&character, None, &mut out);
assert!(out.is_empty(), "{out:?}");
let down = Segment::new(DVec3::new(0.9, 5.0, 0.0), DVec3::new(0.9, -5.0, 0.0));
let (_, hit) = lying
.sweep_capsule(&down, 0.25, 0.25)
.expect("the lying capsule");
let exact = (5.0 - 1.0) / 10.0;
assert!(
hit.t <= exact && hit.t >= exact - SWEEP_TOLERANCE / 10.0,
"{hit:?}"
);
assert!((hit.normal - DVec3::Y).length() < 1e-6, "{hit:?}");
assert!(
(hit.point - DVec3::new(0.9, 0.5, 0.0)).length() < 1e-6,
"{hit:?}"
);
assert_eq!(upright.sweep_capsule(&down, 0.25, 0.25), None);
}
#[test]
fn a_lying_body_is_blocked_by_the_turned_core() {
let (mut lying, mut upright) = worlds();
let body = LyingCapsule::new(DVec3::new(0.9, 0.3, -1.0), 0.0, 0.25, 2.0);
assert!(
lying
.lying_capsule_blocker(&body, QueryFilter::ALL)
.is_some()
);
assert_eq!(upright.lying_capsule_blocker(&body, QueryFilter::ALL), None);
}
#[test]
fn a_lying_body_lowered_onto_the_turned_core_stops_on_it() {
let (mut lying, mut upright) = worlds();
let body = LyingCapsule::new(DVec3::new(0.9, 1.5, -1.0), 0.0, 0.25, 2.0);
let down = DVec3::new(0.0, -2.0, 0.0);
let (_, hit) = lying
.sweep_lying_capsule(&body, down, QueryFilter::ALL)
.expect("the lying capsule");
let exact = (1.5 - 0.75) / 2.0;
assert!(
hit.t <= exact && hit.t >= exact - SWEEP_TOLERANCE / 2.0,
"{hit:?}"
);
assert!((hit.normal - DVec3::Y).length() < 1e-6, "{hit:?}");
assert_eq!(
upright.sweep_lying_capsule(&body, down, QueryFilter::ALL),
None
);
}
#[test]
fn the_bounds_are_the_turned_cores() {
let along_z = crate::rotation_from_scaled_axis(DVec3::X * FRAC_PI_2);
for (turn, half) in [
(lay_down(), DVec3::new(1.5, 0.5, 0.5)),
(along_z, DVec3::new(0.5, 0.5, 1.5)),
] {
let mut world = PhysicsWorld::new();
let id = world.add_turned_capsule(capsule(), turn);
let bounds = world.aabb_of(id).expect("in the world");
assert!(close(bounds.min, -half), "{bounds:?}");
assert!(close(bounds.max, half), "{bounds:?}");
}
}
#[test]
fn a_capsule_turned_about_y_answers_as_the_upright_one_to_the_bit() {
let facing = crate::rotation_from_scaled_axis(DVec3::Y * 0.7);
assert_ne!(facing, DQuat::IDENTITY);
let centre = DVec3::new(0.3, 0.2, -0.1);
let mut turned = PhysicsWorld::new();
let turned_id = turned.add_turned_capsule(Capsule::new(centre, RADIUS, HALF_HEIGHT), facing);
let mut upright = PhysicsWorld::new();
let upright_id = upright.add_capsule(Capsule::new(centre, RADIUS, HALF_HEIGHT));
assert_eq!(turned.aabb_of(turned_id), upright.aabb_of(upright_id));
let aimed = |from: DVec3, at: DVec3| Ray::new(from, at - from);
let rays = [
aimed(DVec3::new(-5.0, 0.7, 0.1), DVec3::new(0.35, 0.5, -0.05)),
aimed(DVec3::new(0.6, 5.0, -0.3), DVec3::new(0.4, 0.3, -0.1)),
];
for ray in rays {
let hit = turned.cast_ray(&ray).map(|(_, hit)| hit);
assert!(hit.is_some(), "the ray is aimed at the capsule");
assert_eq!(hit, upright.cast_ray(&ray).map(|(_, hit)| hit));
}
let path = Segment::new(DVec3::new(-4.0, 1.4, 0.2), DVec3::new(4.0, 0.9, -0.3));
let sphere = turned.sweep_sphere(&path, 0.3).map(|(_, hit)| hit);
assert!(sphere.is_some(), "the sweep is aimed at the capsule");
assert_eq!(sphere, upright.sweep_sphere(&path, 0.3).map(|(_, hit)| hit));
let swept = turned.sweep_capsule(&path, 0.3, 0.4).map(|(_, hit)| hit);
assert!(swept.is_some(), "the sweep is aimed at the capsule");
assert_eq!(
swept,
upright.sweep_capsule(&path, 0.3, 0.4).map(|(_, hit)| hit)
);
let character = Capsule::new(DVec3::new(0.9, 0.6, 0.1), 0.5, 0.5);
let (mut a, mut b) = (Vec::new(), Vec::new());
turned.capsule_penetrations_into(&character, None, &mut a);
upright.capsule_penetrations_into(&character, None, &mut b);
assert_eq!(a.len(), 1, "the character is inside the capsule");
assert_eq!(a[0].1, b[0].1);
}
#[test]
fn a_bodys_capsule_turns_with_the_body() {
let entity =
crcbl_ecs::Entity::from_bits((1u64 << 32) | 7).expect("generation 1 is never zero");
let transform = Transform::new(DVec3::ZERO, lay_down());
let mut phys = PhysicsSystem::new();
phys.set_body(entity, RigidBody::new_kinematic());
phys.set_transform(entity, transform);
phys.set_collider(
entity,
&ColliderComponent::Capsule {
offset: DVec3::ZERO,
radius: RADIUS,
half_height: HALF_HEIGHT,
is_trigger: false,
},
&transform,
);
let down = Ray::new(DVec3::new(1.2, 5.0, 0.0), DVec3::NEG_Y);
assert_eq!(phys.cast_ray(&down).map(|(hit, _)| hit), Some(entity));
phys.set_transform(entity, Transform::IDENTITY);
assert_eq!(phys.cast_ray(&down), None, "stood up again");
phys.set_transform(entity, transform);
assert_eq!(phys.cast_ray(&down).map(|(hit, _)| hit), Some(entity));
}