use core::f64::consts::{FRAC_PI_2, FRAC_PI_4, SQRT_2};
use glam::DVec3;
use super::*;
use crate::collider::BoxCollider;
use crate::integrator::rotation_from_scaled_axis;
use crate::query::{TurnedCapsule, sphere_overlap_vs_box, sphere_overlap_vs_turned_capsule};
const EXACT: f64 = 1e-12;
fn assert_hit(hit: Option<OverlapHit>, point: DVec3, normal: DVec3, depth: f64) {
let hit = hit.expect("an overlap");
assert!(
(hit.point - point).length() < EXACT,
"point: {hit:?}, wanted {point}"
);
assert!(
(hit.normal - normal).length() < EXACT,
"normal: {hit:?}, wanted {normal}"
);
assert!(
(hit.depth - depth).abs() < EXACT,
"depth: {hit:?}, wanted {depth}"
);
assert!(
(hit.normal.length() - 1.0).abs() < EXACT,
"not unit: {hit:?}"
);
assert!(hit.depth > 0.0, "a hit with no depth: {hit:?}");
assert_eq!(hit.part, 0);
}
fn assert_pushed_out_touches(sphere: &Sphere, hit: OverlapHit) {
let moved = sphere.centre + hit.normal * hit.depth;
assert!(
((moved - hit.point).length() - sphere.radius).abs() < EXACT,
"the pushed-out sphere does not touch the point: {hit:?}",
);
}
#[test]
fn a_sphere_inside_a_sphere_is_pushed_out_along_the_centres() {
let query = Sphere::new(DVec3::new(3.0, 0.0, 0.0), 1.5);
let target = Sphere::new(DVec3::ZERO, 2.0);
let hit = sphere_overlap_vs_sphere(&query, &target);
assert_hit(hit, DVec3::new(2.0, 0.0, 0.0), DVec3::X, 0.5);
assert_pushed_out_touches(&query, hit.unwrap());
}
#[test]
fn coincident_spheres_leave_along_plus_y() {
let query = Sphere::new(DVec3::new(1.0, 2.0, 3.0), 1.5);
let target = Sphere::new(DVec3::new(1.0, 2.0, 3.0), 2.0);
assert_hit(
sphere_overlap_vs_sphere(&query, &target),
DVec3::new(1.0, 4.0, 3.0),
DVec3::Y,
3.5,
);
}
#[test]
fn spheres_that_only_touch_or_are_apart_do_not_overlap() {
let target = Sphere::new(DVec3::ZERO, 2.0);
let touching = Sphere::new(DVec3::new(3.5, 0.0, 0.0), 1.5);
assert_eq!(sphere_overlap_vs_sphere(&touching, &target), None);
let apart = Sphere::new(DVec3::new(0.0, 0.0, -4.0), 1.5);
assert_eq!(sphere_overlap_vs_sphere(&apart, &target), None);
}
fn capsule() -> Capsule {
Capsule::new(DVec3::ZERO, 0.5, 1.0)
}
#[test]
fn a_sphere_beside_a_capsule_is_pushed_out_sideways() {
let query = Sphere::new(DVec3::new(1.0, 0.5, 0.0), 0.75);
let hit = sphere_overlap_vs_capsule(&query, &capsule());
assert_hit(hit, DVec3::new(0.5, 0.5, 0.0), DVec3::X, 0.25);
assert_pushed_out_touches(&query, hit.unwrap());
}
#[test]
fn a_sphere_over_a_capsule_cap_is_pushed_out_through_it() {
let query = Sphere::new(DVec3::new(0.0, 2.0, 0.0), 1.25);
assert_hit(
sphere_overlap_vs_capsule(&query, &capsule()),
DVec3::new(0.0, 1.5, 0.0),
DVec3::Y,
0.75,
);
}
#[test]
fn a_centre_on_a_capsule_core_leaves_along_plus_y() {
let query = Sphere::new(DVec3::new(0.0, 0.5, 0.0), 0.75);
assert_hit(
sphere_overlap_vs_capsule(&query, &capsule()),
DVec3::new(0.0, 1.0, 0.0),
DVec3::Y,
1.25,
);
}
#[test]
fn a_sphere_that_only_touches_a_capsule_does_not_overlap_it() {
let query = Sphere::new(DVec3::new(1.25, 0.0, 0.0), 0.75);
assert_eq!(sphere_overlap_vs_capsule(&query, &capsule()), None);
}
#[test]
fn a_turned_capsule_is_measured_in_its_own_frame() {
let turn = rotation_from_scaled_axis(DVec3::Z * FRAC_PI_2);
let lying = TurnedCapsule::new(capsule(), turn);
let above = Sphere::new(DVec3::new(0.5, 1.0, 0.0), 0.75);
assert_hit(
sphere_overlap_vs_turned_capsule(&above, &lying),
DVec3::new(0.5, 0.5, 0.0),
DVec3::Y,
0.25,
);
let on_core = Sphere::new(DVec3::new(0.5, 0.0, 0.0), 0.75);
let along = turn * DVec3::Y;
assert_hit(
sphere_overlap_vs_turned_capsule(&on_core, &lying),
on_core.centre + along * 0.5,
along,
1.25,
);
}
fn boxed() -> BoxCollider {
BoxCollider::new(DVec3::ZERO, DVec3::new(1.0, 2.0, 3.0))
}
#[test]
fn a_sphere_against_a_box_face_is_pushed_out_through_the_face() {
let query = Sphere::new(DVec3::new(1.5, 0.5, -1.0), 1.0);
let hit = sphere_overlap_vs_box(&query, &boxed());
assert_hit(hit, DVec3::new(1.0, 0.5, -1.0), DVec3::X, 0.5);
assert_pushed_out_touches(&query, hit.unwrap());
}
#[test]
fn a_sphere_against_a_box_edge_is_pushed_out_from_the_edge() {
let query = Sphere::new(DVec3::new(2.0, 3.0, 0.0), 1.5);
let hit = sphere_overlap_vs_box(&query, &boxed());
assert_hit(
hit,
DVec3::new(1.0, 2.0, 0.0),
DVec3::new(1.0, 1.0, 0.0) / SQRT_2,
1.5 - SQRT_2,
);
assert_pushed_out_touches(&query, hit.unwrap());
}
#[test]
fn a_sphere_against_a_box_corner_is_pushed_out_from_the_corner() {
let query = Sphere::new(DVec3::new(2.0, 3.0, 4.0), 2.0);
let root_three = 3.0_f64.sqrt();
assert_hit(
sphere_overlap_vs_box(&query, &boxed()),
DVec3::new(1.0, 2.0, 3.0),
DVec3::ONE / root_three,
2.0 - root_three,
);
}
#[test]
fn a_centre_inside_a_box_leaves_through_the_nearest_face() {
let query = Sphere::new(DVec3::new(0.7, -0.5, 1.0), 0.1);
assert_hit(
sphere_overlap_vs_box(&query, &boxed()),
DVec3::new(1.0, -0.5, 1.0),
DVec3::X,
0.4,
);
}
#[test]
fn the_centre_of_a_cube_leaves_through_minus_x() {
let cube = BoxCollider::new(DVec3::new(5.0, 0.0, 0.0), DVec3::ONE);
let query = Sphere::new(DVec3::new(5.0, 0.0, 0.0), 0.25);
assert_hit(
sphere_overlap_vs_box(&query, &cube),
DVec3::new(4.0, 0.0, 0.0),
DVec3::NEG_X,
1.25,
);
}
#[test]
fn a_sphere_that_only_touches_a_box_does_not_overlap_it() {
let face = Sphere::new(DVec3::new(2.0, 0.0, 0.0), 1.0);
assert_eq!(sphere_overlap_vs_box(&face, &boxed()), None);
let edge = Sphere::new(DVec3::new(4.0, 6.0, 0.0), 5.0);
assert_eq!(sphere_overlap_vs_box(&edge, &boxed()), None);
}
#[test]
fn a_turned_box_is_measured_in_its_own_frame() {
let cube = BoxCollider::new(DVec3::ZERO, DVec3::ONE)
.with_rotation(rotation_from_scaled_axis(DVec3::Y * FRAC_PI_4));
let query = Sphere::new(DVec3::new(2.0, 0.0, 0.0), 1.0);
let hit = sphere_overlap_vs_box(&query, &cube);
assert_hit(hit, DVec3::new(SQRT_2, 0.0, 0.0), DVec3::X, SQRT_2 - 1.0);
assert_pushed_out_touches(&query, hit.unwrap());
}
#[test]
fn an_aabb_answers_as_the_box_with_its_bounds() {
let query = Sphere::new(DVec3::new(2.0, 3.0, 0.0), 1.5);
assert_eq!(
sphere_overlap_vs_aabb(&query, &boxed().aabb()),
sphere_overlap_vs_box(&query, &boxed()),
);
}