use core::f64::consts::{FRAC_1_SQRT_2, FRAC_PI_4, SQRT_2};
use super::*;
fn unturned() -> BoxCollider {
BoxCollider::new(DVec3::ZERO, DVec3::ONE)
}
fn turned_about(axis: DVec3) -> BoxCollider {
unturned().with_rotation(crate::rotation_from_scaled_axis(axis * FRAC_PI_4))
}
fn close(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-12
}
fn near(a: DVec3, b: DVec3) -> bool {
(a - b).length() < 1e-12
}
fn face_normal() -> DVec3 {
DVec3::new(-1.0, 0.0, 1.0) / SQRT_2
}
#[test]
fn a_turned_boxs_bounds_reach_its_corners() {
let bounds = turned_about(DVec3::Y).aabb();
assert!(
near(bounds.max, DVec3::new(SQRT_2, 1.0, SQRT_2)),
"{bounds:?}"
);
assert!(near(bounds.min, -bounds.max), "{bounds:?}");
}
#[test]
fn a_ray_beside_the_unturned_box_hits_the_turned_face() {
let ray = Ray::new(DVec3::new(-5.0, 0.0, 1.2), DVec3::X);
assert!(ray_vs_box(&ray, &unturned()).is_none());
let hit = ray_vs_box(&ray, &turned_about(DVec3::Y)).expect("the turned face");
let x = 1.2 - SQRT_2;
assert!(close(hit.t, x + 5.0), "{hit:?}");
assert!(near(hit.point, DVec3::new(x, 0.0, 1.2)), "{hit:?}");
assert!(near(hit.normal, face_normal()), "{hit:?}");
assert!(!hit.started_inside);
}
#[test]
fn a_ray_across_the_unturned_corner_misses_the_turned_box() {
let ray = Ray::new(
DVec3::new(-3.0, 0.0, 4.7),
DVec3::new(1.0, 0.0, -1.0).normalize(),
);
assert!(ray_vs_box(&ray, &unturned()).is_some());
assert!(ray_vs_box(&ray, &turned_about(DVec3::Y)).is_none());
}
#[test]
fn a_swept_sphere_meets_the_turned_face() {
let path = Segment::new(DVec3::new(-5.0, 0.0, 1.25), DVec3::new(5.0, 0.0, 1.25));
assert!(swept_sphere_vs_box(&path, 0.1, &unturned()).is_none());
let hit = swept_sphere_vs_box(&path, 0.1, &turned_about(DVec3::Y)).expect("the turned face");
let x = 1.25 - 1.1 * SQRT_2;
assert!(close(hit.t, (x + 5.0) / 10.0), "{hit:?}");
assert!(near(hit.normal, face_normal()), "{hit:?}");
let centre = DVec3::new(x, 0.0, 1.25);
assert!(near(hit.point, centre - face_normal() * 0.1), "{hit:?}");
}
#[test]
fn a_sphere_overlaps_the_turned_box_where_it_reaches() {
let by_the_corner = Sphere::new(DVec3::new(1.2, 0.0, 0.0), 0.1);
assert!(!sphere_overlaps_box(&by_the_corner, &unturned()));
assert!(sphere_overlaps_box(&by_the_corner, &turned_about(DVec3::Y)));
let in_the_old_corner = Sphere::new(DVec3::new(0.85, 0.0, 0.85), 0.05);
assert!(sphere_overlaps_box(&in_the_old_corner, &unturned()));
assert!(!sphere_overlaps_box(
&in_the_old_corner,
&turned_about(DVec3::Y)
));
}
const ADVANCE_SHARE: f64 = 2.0e-4;
fn just_short(t: f64, expected: f64) -> bool {
t <= expected + 1e-12 && expected - t < ADVANCE_SHARE
}
#[test]
fn a_falling_capsule_lands_on_the_turned_ridge() {
let path = Segment::new(DVec3::new(0.0, 5.0, 0.0), DVec3::ZERO);
let flat = swept_capsule_vs_box(&path, 0.1, 0.5, &unturned()).expect("the top");
assert!(close(flat.t, (5.0 - 1.6) / 5.0), "{flat:?}");
let hit = swept_capsule_vs_box(&path, 0.1, 0.5, &turned_about(DVec3::Z)).expect("the ridge");
let expected = (5.0 - (SQRT_2 + 0.6)) / 5.0;
assert!(
just_short(hit.t, expected),
"met at {} of the fall, not just short of {expected}: {hit:?}",
hit.t,
);
assert!((hit.normal - DVec3::Y).length() < 1e-6, "{hit:?}");
assert!(near(hit.point, DVec3::new(0.0, SQRT_2, 0.0)), "{hit:?}");
assert!(!hit.started_inside);
}
#[test]
fn a_sideways_capsule_meets_the_turned_face() {
let path = Segment::new(DVec3::new(-5.0, 0.0, 1.25), DVec3::new(5.0, 0.0, 1.25));
assert!(swept_capsule_vs_box(&path, 0.1, 0.5, &unturned()).is_none());
let hit = swept_capsule_vs_box(&path, 0.1, 0.5, &turned_about(DVec3::Y)).expect("the face");
let expected = (1.25 - 1.1 * SQRT_2 + 5.0) / 10.0;
assert!(
just_short(hit.t, expected),
"met at {}, not just short of {expected}: {hit:?}",
hit.t,
);
assert!((hit.normal - face_normal()).length() < 1e-6, "{hit:?}");
let on_face = hit.point.z - hit.point.x;
assert!((on_face - SQRT_2).abs() < 1e-3, "{hit:?}");
assert!(
(hit.point.z - (1.25 - 0.1 * FRAC_1_SQRT_2)).abs() < 1e-3,
"{hit:?}"
);
}
#[test]
fn a_capsule_sweep_starting_inside_or_standing_still() {
let turned = turned_about(DVec3::Y);
let inside = Segment::new(DVec3::new(1.2, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
let hit = swept_capsule_vs_box(&inside, 0.1, 0.5, &turned).expect("inside");
assert!(hit.started_inside && hit.t == 0.0, "{hit:?}");
let still = Segment::new(DVec3::new(3.0, 0.0, 0.0), DVec3::new(3.0, 0.0, 0.0));
assert!(swept_capsule_vs_box(&still, 0.1, 0.5, &turned).is_none());
}
#[test]
fn a_capsule_is_pushed_out_along_the_turned_face() {
let turned = turned_about(DVec3::Y);
let normal = DVec3::new(1.0, 0.0, 1.0) / SQRT_2;
let into = Capsule::new(normal * 1.25, 0.3, 0.5);
let push = capsule_penetration_vs_box(&into, &turned).expect("overlapping the face");
assert!(close(push.depth, 0.05), "{push:?}");
assert!(near(push.normal, normal), "{push:?}");
assert!(capsule_penetration_vs_box(&into, &unturned()).is_some_and(|old| old.normal != normal));
let clear = Capsule::new(normal * 1.35, 0.3, 0.5);
assert!(capsule_penetration_vs_box(&clear, &turned).is_none());
assert!(capsule_penetration_vs_box(&clear, &unturned()).is_some());
}
#[test]
fn an_unturned_box_answers_exactly_as_its_aabb() {
let target = BoxCollider::new(DVec3::new(0.1, 0.7, -0.3), DVec3::new(0.3, 0.2, 0.9));
let ray = Ray::new(
DVec3::new(-5.0, 0.75, 0.1),
DVec3::new(1.0, 0.01, 0.02).normalize(),
);
assert_eq!(ray_vs_box(&ray, &target), ray_vs_aabb(&ray, &target.aabb()));
let path = Segment::new(DVec3::new(-5.0, 0.73, 0.1), DVec3::new(5.0, 0.71, 0.0));
assert_eq!(
swept_sphere_vs_box(&path, 0.07, &target),
swept_sphere_vs_aabb(&path, 0.07, &target.aabb()),
);
assert_eq!(
swept_capsule_vs_box(&path, 0.07, 0.3, &target),
swept_capsule_vs_aabb(&path, 0.07, 0.3, &target.aabb()),
);
}
#[test]
fn a_short_sweep_keeps_its_contact_time_against_a_turned_box() {
for scale in [1e100, 1.0, 1e-6, 1e-10, 1e-100] {
let target = BoxCollider::new(DVec3::ZERO, DVec3::splat(scale))
.with_rotation(crate::rotation_from_scaled_axis(DVec3::Y * FRAC_PI_4));
let path = Segment::new(
DVec3::new(-4.0 * scale, 0.0, 0.0),
DVec3::new(-2.0 * scale, 0.0, 0.0),
);
let hit = swept_sphere_vs_box(&path, scale, &target)
.unwrap_or_else(|| panic!("a short sweep missed at scale {scale}"));
let expected = (4.0 - 2.0 * SQRT_2) / 2.0;
assert!(
(hit.t - expected).abs() < 1e-12,
"met at {} of the way, not {expected}, at scale {scale}",
hit.t,
);
assert!(!hit.started_inside);
}
}
#[test]
fn an_aabb_overlaps_the_turned_box_by_its_faces() {
let corner = Aabb::new(DVec3::new(1.0, -0.5, 1.0), DVec3::new(1.5, 0.5, 1.5));
assert!(aabb_overlaps_box(&corner, &unturned()));
assert!(!aabb_overlaps_box(&corner, &turned_about(DVec3::Y)));
let point = Aabb::new(DVec3::new(1.3, -0.5, -0.05), DVec3::new(1.6, 0.5, 0.05));
assert!(!aabb_overlaps_box(&point, &unturned()));
assert!(aabb_overlaps_box(&point, &turned_about(DVec3::Y)));
let empty = Aabb::new(DVec3::new(0.1, -1.0, -1.0), DVec3::new(0.0, 1.0, 1.0));
assert!(!aabb_overlaps_box(&empty, &turned_about(DVec3::Y)));
let half_turn = unturned().with_rotation(DQuat::from_xyzw(0.0, 1.0, 0.0, 0.0));
let resting = Aabb::new(DVec3::new(1.0, -0.5, -0.5), DVec3::new(2.0, 0.5, 0.5));
assert!(aabb_overlaps_box(&resting, &half_turn));
}
#[test]
fn the_overlap_agrees_with_a_separating_axis_test() {
let mut state = 0x9E37_79B9_7F4A_7C15_u64;
let mut unit = || {
state ^= state >> 12;
state ^= state << 25;
state ^= state >> 27;
(state.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 11) as f64 / (1u64 << 53) as f64
};
let (mut apart, mut met) = (0, 0);
for _ in 0..4000 {
let mut vector = |reach: f64| {
DVec3::new(
(unit() * 2.0 - 1.0) * reach,
(unit() * 2.0 - 1.0) * reach,
(unit() * 2.0 - 1.0) * reach,
)
};
let axis = vector(1.0);
let turn =
crate::rotation_from_scaled_axis(axis.normalize_or(DVec3::X) * 3.0 * axis.x.abs());
let target = BoxCollider::new(vector(1.0), vector(1.0).abs() + 0.1).with_rotation(turn);
let centre = vector(2.0);
let half = vector(1.0).abs() + 0.05;
let query = Aabb::new(centre - half, centre + half);
let separation = widest_separation(&query, &target);
if separation.abs() < 1e-9 {
continue;
}
let expected = separation <= 0.0;
assert_eq!(
aabb_overlaps_box(&query, &target),
expected,
"{query:?} against {target:?}: separation {separation}"
);
if expected {
met += 1;
} else {
apart += 1;
}
}
assert!(apart > 500 && met > 500, "apart {apart}, met {met}");
}
fn widest_separation(query: &Aabb, target: &BoxCollider) -> f64 {
let a_axes = [DVec3::X, DVec3::Y, DVec3::Z];
let b_axes = [
target.rotation * DVec3::X,
target.rotation * DVec3::Y,
target.rotation * DVec3::Z,
];
let a_half = query.extents() * 0.5;
let between = target.centre - query.centre();
let radius = |axis: DVec3, axes: &[DVec3; 3], half: DVec3| {
axes[0].dot(axis).abs() * half.x
+ axes[1].dot(axis).abs() * half.y
+ axes[2].dot(axis).abs() * half.z
};
let mut candidates: Vec<DVec3> = a_axes.iter().chain(&b_axes).copied().collect();
for a in a_axes {
for b in b_axes {
let cross = a.cross(b);
if cross.length() > 1e-9 {
candidates.push(cross.normalize());
}
}
}
candidates
.into_iter()
.map(|axis| {
between.dot(axis).abs()
- radius(axis, &a_axes, a_half)
- radius(axis, &b_axes, target.half_extents)
})
.fold(f64::NEG_INFINITY, f64::max)
}