mod arc;
mod arc_selection;
mod boxes;
mod capsules;
mod sphere_overlap;
use crate::broadphase::Ray;
use crate::collider::{Aabb, Capsule, Sphere};
use glam::DVec3;
pub use self::arc::{ARC_MAX_ITERATIONS, ARC_TIME_TOLERANCE, AcceleratedPath, ArcHit};
pub(crate) use self::arc::{
arc_capsule_vs_box, arc_capsule_vs_sphere, arc_capsule_vs_turned_capsule, support,
};
pub(crate) use self::arc_selection::contact_where;
pub use self::boxes::{
aabb_overlaps_box, capsule_penetration_vs_box, ray_vs_box, sphere_overlap_vs_box,
sphere_overlaps_box, swept_capsule_vs_box, swept_sphere_vs_box,
};
pub(crate) use self::boxes::{contact_box, nearest_on_box};
pub(crate) use self::capsules::{
TurnedCapsule, capsule_penetration_vs_turned_capsule, ray_vs_turned_capsule,
sphere_overlap_vs_turned_capsule, swept_capsule_vs_turned_capsule,
swept_sphere_vs_turned_capsule,
};
pub use self::sphere_overlap::{
OverlapHit, sphere_overlap_vs_aabb, sphere_overlap_vs_capsule, sphere_overlap_vs_sphere,
};
#[must_use]
pub fn sphere_overlaps_sphere(a: &Sphere, b: &Sphere) -> bool {
sphere_overlap_vs_sphere(a, b).is_some()
}
#[must_use]
pub fn sphere_overlaps_aabb(sphere: &Sphere, aabb: &Aabb) -> bool {
sphere_overlap_vs_aabb(sphere, aabb).is_some()
}
#[must_use]
pub fn sphere_overlaps_capsule(sphere: &Sphere, capsule: &Capsule) -> bool {
sphere_overlap_vs_capsule(sphere, capsule).is_some()
}
#[inline]
#[must_use]
fn closest_point_on_capsule_axis(capsule: &Capsule, point: DVec3) -> DVec3 {
let bot = capsule.bottom();
let seg = capsule.top() - bot;
let len_sq = seg.length_squared();
if len_sq <= 0.0 {
return capsule.centre;
}
bot + seg * ((point - bot).dot(seg) / len_sq).clamp(0.0, 1.0)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ShapeHit {
pub t: f64,
pub point: DVec3,
pub normal: DVec3,
pub started_inside: bool,
pub part: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum InsideRule {
Exit,
Contact,
}
#[inline]
fn solve_quadratic(a: f64, b: f64, c: f64) -> Option<(f64, f64)> {
if a <= f64::EPSILON {
return None;
}
let disc = b * b - 4.0 * a * c;
if disc < 0.0 {
return None;
}
let sqrt_disc = disc.sqrt();
let inv_2a = 0.5 / a;
Some(((-b - sqrt_disc) * inv_2a, (-b + sqrt_disc) * inv_2a))
}
#[inline]
fn select_hit(near: f64, far: f64, lo: f64, hi: f64, rule: InsideRule) -> Option<(f64, bool)> {
if near > far || far < lo || near > hi {
return None;
}
if near >= lo {
return Some((near, false));
}
match rule {
InsideRule::Exit if far <= hi => Some((far, true)),
InsideRule::Exit => None,
InsideRule::Contact => Some((lo, true)),
}
}
#[must_use]
fn aabb_escape(aabb: &Aabb, point: DVec3) -> (DVec3, f64) {
let faces = [
((point.x - aabb.min.x).abs(), DVec3::NEG_X),
((aabb.max.x - point.x).abs(), DVec3::X),
((point.y - aabb.min.y).abs(), DVec3::NEG_Y),
((aabb.max.y - point.y).abs(), DVec3::Y),
((point.z - aabb.min.z).abs(), DVec3::NEG_Z),
((aabb.max.z - point.z).abs(), DVec3::Z),
];
let mut best = faces[0];
for &face in &faces[1..] {
if face.0 < best.0 {
best = face;
}
}
(best.1, best.0)
}
#[must_use]
pub fn ray_vs_sphere(ray: &Ray, sphere: &Sphere) -> Option<ShapeHit> {
let oc = ray.origin - sphere.centre;
let a = ray.dir.dot(ray.dir);
let b = 2.0 * oc.dot(ray.dir);
let c = oc.dot(oc) - sphere.radius * sphere.radius;
let (near, far) = solve_quadratic(a, b, c)?;
let (t, started_inside) = select_hit(near, far, ray.t_min, ray.t_max, InsideRule::Exit)?;
let point = ray.origin + ray.dir * t;
let to_centre = point - sphere.centre;
let normal = if to_centre.length_squared() > 0.0 {
to_centre.normalize()
} else {
DVec3::Y
};
Some(ShapeHit {
t,
point,
normal,
started_inside,
part: 0,
})
}
#[must_use]
pub fn ray_vs_aabb(ray: &Ray, aabb: &Aabb) -> Option<ShapeHit> {
let inv_dir = ray.dir.recip();
let dir_is_neg = [ray.dir.x < 0.0, ray.dir.y < 0.0, ray.dir.z < 0.0];
let (near, far) = aabb.ray_slab(ray.origin, inv_dir, dir_is_neg)?;
let (t, started_inside) = select_hit(near, far, ray.t_min, ray.t_max, InsideRule::Exit)?;
let point = ray.origin + ray.dir * t;
let normal = aabb_escape(aabb, point).0;
Some(ShapeHit {
t,
point,
normal,
started_inside,
part: 0,
})
}
#[must_use]
pub fn ray_vs_capsule(ray: &Ray, capsule: &Capsule) -> Option<ShapeHit> {
let bot = capsule.bottom();
let top = capsule.top();
let radius_sq = capsule.radius * capsule.radius;
let mut best = f64::INFINITY;
let mut consider = |t: f64| {
if t >= ray.t_min && t <= ray.t_max && t < best {
best = t;
}
};
let oc_xz = DVec3::new(
ray.origin.x - capsule.centre.x,
0.0,
ray.origin.z - capsule.centre.z,
);
let dir_xz = DVec3::new(ray.dir.x, 0.0, ray.dir.z);
if let Some(roots) = solve_quadratic(
dir_xz.dot(dir_xz),
2.0 * oc_xz.dot(dir_xz),
oc_xz.dot(oc_xz) - radius_sq,
) {
for t in [roots.0, roots.1] {
let y = ray.origin.y + ray.dir.y * t;
if y >= bot.y && y <= top.y {
consider(t);
}
}
}
let a = ray.dir.dot(ray.dir);
for (cap, is_top) in [(top, true), (bot, false)] {
let oc = ray.origin - cap;
if let Some(roots) = solve_quadratic(a, 2.0 * oc.dot(ray.dir), oc.dot(oc) - radius_sq) {
for t in [roots.0, roots.1] {
let y = ray.origin.y + ray.dir.y * t;
if (is_top && y >= top.y) || (!is_top && y <= bot.y) {
consider(t);
}
}
}
}
if !best.is_finite() {
return None;
}
let point = ray.origin + ray.dir * best;
let axis_point = closest_point_on_capsule_axis(capsule, point);
let radial = point - axis_point;
let normal = if radial.length_squared() > 0.0 {
radial.normalize()
} else {
DVec3::Y
};
let entry = ray.origin + ray.dir * ray.t_min;
let started_inside =
(entry - closest_point_on_capsule_axis(capsule, entry)).length_squared() <= radius_sq;
Some(ShapeHit {
t: best,
point,
normal,
started_inside,
part: 0,
})
}
#[must_use]
pub fn swept_sphere_vs_sphere(
segment: &crate::broadphase::Segment,
swept_radius: f64,
target: &Sphere,
) -> Option<ShapeHit> {
let dir = segment.end - segment.start;
let combined_r = swept_radius + target.radius;
let oc = segment.start - target.centre;
if dir.length_squared() <= f64::EPSILON {
let dist = oc.length();
if dist <= combined_r {
let overlap = combined_r - dist;
let normal = if dist > 0.0 { oc / dist } else { DVec3::Y };
return Some(ShapeHit {
t: 0.0,
point: segment.start - normal * (swept_radius - overlap * 0.5),
normal,
started_inside: true,
part: 0,
});
}
return None;
}
let (near, far) = solve_quadratic(
dir.dot(dir),
2.0 * oc.dot(dir),
oc.dot(oc) - combined_r * combined_r,
)?;
let (t, started_inside) = select_hit(near, far, 0.0, 1.0, InsideRule::Contact)?;
let point_on_swept = segment.start + dir * t;
let to_target = point_on_swept - target.centre;
let normal = if to_target.length_squared() > 0.0 {
to_target.normalize()
} else {
DVec3::Y
};
let contact_point = target.centre + normal * target.radius;
Some(ShapeHit {
t,
point: contact_point,
normal,
started_inside,
part: 0,
})
}
pub use aabb_sweep::swept_sphere_vs_aabb;
#[path = "query/aabb_sweep.rs"]
mod aabb_sweep;
#[must_use]
pub fn swept_sphere_vs_capsule(
segment: &crate::broadphase::Segment,
swept_radius: f64,
capsule: &Capsule,
) -> Option<ShapeHit> {
let combined = swept_radius + capsule.radius;
let axis_point = closest_point_on_capsule_axis(capsule, segment.start);
let radial = segment.start - axis_point;
let dist_sq = radial.length_squared();
if dist_sq <= combined * combined {
let dist = dist_sq.sqrt();
let normal = if dist > 0.0 { radial / dist } else { DVec3::Y };
return Some(ShapeHit {
t: 0.0,
point: axis_point + normal * capsule.radius,
normal,
started_inside: true,
part: 0,
});
}
let dir = segment.end - segment.start;
if dir.length_squared() <= 0.0 {
return None; }
let inflated = Capsule::new(capsule.centre, combined, capsule.half_height);
let ray = Ray::new(segment.start, dir).with_bounds(0.0, 1.0);
ray_vs_capsule(&ray, &inflated).map(|hit| {
let contact_point = hit.point - hit.normal * swept_radius;
ShapeHit {
t: hit.t, point: contact_point,
normal: hit.normal,
started_inside: hit.started_inside,
part: 0,
}
})
}
#[must_use]
pub fn swept_capsule_vs_sphere(
segment: &crate::broadphase::Segment,
swept_radius: f64,
swept_half_height: f64,
target: &Sphere,
) -> Option<ShapeHit> {
let proxy = Capsule::new(target.centre, target.radius, swept_half_height);
let hit = swept_sphere_vs_capsule(segment, swept_radius, &proxy)?;
Some(ShapeHit {
point: target.centre + hit.normal * target.radius,
..hit
})
}
#[must_use]
pub fn swept_capsule_vs_aabb(
segment: &crate::broadphase::Segment,
swept_radius: f64,
swept_half_height: f64,
target: &Aabb,
) -> Option<ShapeHit> {
if target.is_empty() {
return None;
}
let grown = Aabb::new(
target.min - DVec3::Y * swept_half_height,
target.max + DVec3::Y * swept_half_height,
);
let hit = aabb_sweep::swept_sphere_vs_rounded_aabb(segment, swept_radius, &grown)?;
Some(ShapeHit {
point: hit.point.clamp(target.min, target.max),
..hit
})
}
#[must_use]
pub fn swept_capsule_vs_capsule(
segment: &crate::broadphase::Segment,
swept_radius: f64,
swept_half_height: f64,
target: &Capsule,
) -> Option<ShapeHit> {
let proxy = Capsule::new(
target.centre,
target.radius,
target.half_height + swept_half_height,
);
let hit = swept_sphere_vs_capsule(segment, swept_radius, &proxy)?;
let centre_at_hit = segment.start + (segment.end - segment.start) * hit.t;
let axis_point = closest_point_on_capsule_axis(target, centre_at_hit);
Some(ShapeHit {
point: axis_point + hit.normal * target.radius,
..hit
})
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Penetration {
pub normal: DVec3,
pub depth: f64,
}
#[must_use]
pub fn capsule_penetration_vs_sphere(capsule: &Capsule, target: &Sphere) -> Option<Penetration> {
let grown = Capsule::new(target.centre, target.radius, capsule.half_height);
penetration_in_capsule(capsule.centre, capsule.radius, &grown)
}
#[must_use]
pub fn capsule_penetration_vs_capsule(capsule: &Capsule, target: &Capsule) -> Option<Penetration> {
let grown = Capsule::new(
target.centre,
target.radius,
target.half_height + capsule.half_height,
);
penetration_in_capsule(capsule.centre, capsule.radius, &grown)
}
#[must_use]
pub fn capsule_penetration_vs_aabb(capsule: &Capsule, target: &Aabb) -> Option<Penetration> {
if target.is_empty() {
return None;
}
let grown = Aabb::new(
target.min - DVec3::Y * capsule.half_height,
target.max + DVec3::Y * capsule.half_height,
);
let closest = capsule.centre.clamp(grown.min, grown.max);
let out = capsule.centre - closest;
let dist_sq = out.length_squared();
if dist_sq > capsule.radius * capsule.radius {
return None;
}
let (normal, depth) = if dist_sq > 0.0 {
let dist = dist_sq.sqrt();
(out / dist, capsule.radius - dist)
} else {
let (normal, to_face) = aabb_escape(&grown, capsule.centre);
(normal, capsule.radius + to_face)
};
(depth > 0.0).then_some(Penetration { normal, depth })
}
fn penetration_in_capsule(point: DVec3, radius: f64, grown: &Capsule) -> Option<Penetration> {
let combined = radius + grown.radius;
let out = point - closest_point_on_capsule_axis(grown, point);
let dist_sq = out.length_squared();
if dist_sq > combined * combined {
return None;
}
let dist = dist_sq.sqrt();
let depth = combined - dist;
if depth <= 0.0 {
return None;
}
Some(Penetration {
normal: if dist > 0.0 { out / dist } else { DVec3::Y },
depth,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::broadphase::Ray;
#[test]
fn ray_hits_sphere_dead_centre() {
let ray = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X);
let sphere = Sphere::new(DVec3::ZERO, 1.0);
let hit = ray_vs_sphere(&ray, &sphere).unwrap();
assert!((hit.t - 4.0).abs() < 0.001);
assert_eq!(hit.normal, DVec3::NEG_X);
}
#[test]
fn a_ray_passing_beside_a_sphere_reports_no_hit() {
let ray = Ray::new(DVec3::new(-5.0, 3.0, 0.0), DVec3::X);
let sphere = Sphere::new(DVec3::ZERO, 1.0);
assert!(ray_vs_sphere(&ray, &sphere).is_none());
}
#[test]
fn ray_starts_inside_sphere() {
let ray = Ray::new(DVec3::ZERO, DVec3::X);
let sphere = Sphere::new(DVec3::ZERO, 2.0);
let hit = ray_vs_sphere(&ray, &sphere).unwrap();
assert!(hit.started_inside);
assert!((hit.t - 2.0).abs() < 0.001);
}
#[test]
fn ray_sphere_behind_origin() {
let ray = Ray::new(DVec3::new(5.0, 0.0, 0.0), DVec3::NEG_X);
let sphere = Sphere::new(DVec3::ZERO, 1.0);
let hit = ray_vs_sphere(&ray, &sphere).unwrap();
assert!((hit.t - 4.0).abs() < 0.001);
}
#[test]
fn zero_radius_sphere_normal_is_finite_when_hit_at_centre() {
let sphere = Sphere {
centre: DVec3::ZERO,
radius: 0.0,
};
let ray = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X);
let hit =
ray_vs_sphere(&ray, &sphere).expect("ray through centre must hit zero-radius sphere");
assert!(
!hit.normal.x.is_nan() && !hit.normal.y.is_nan() && !hit.normal.z.is_nan(),
"normal must be finite for zero-radius sphere hit at centre; got {:?}",
hit.normal
);
}
#[test]
fn ray_hits_sphere_it_already_entered_before_t_min() {
let ray = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X).with_bounds(4.5, 100.0);
let sphere = Sphere::new(DVec3::ZERO, 1.0);
let hit = ray_vs_sphere(&ray, &sphere).expect("far root is within bounds");
assert!((hit.t - 6.0).abs() < 1e-9, "t = {}", hit.t);
assert!(hit.started_inside);
}
#[test]
fn ray_misses_sphere_when_both_roots_are_out_of_bounds() {
let ray = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X).with_bounds(6.5, 100.0);
let sphere = Sphere::new(DVec3::ZERO, 1.0);
assert!(ray_vs_sphere(&ray, &sphere).is_none());
}
#[test]
fn ray_hits_aabb_centre() {
let ray = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = ray_vs_aabb(&ray, &aabb).unwrap();
assert!((hit.t - 4.0).abs() < 0.001);
assert_eq!(hit.normal, DVec3::NEG_X);
}
#[test]
fn a_ray_passing_above_a_box_reports_no_hit() {
let ray = Ray::new(DVec3::new(-5.0, 5.0, 0.0), DVec3::X);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
assert!(ray_vs_aabb(&ray, &aabb).is_none());
}
#[test]
fn ray_hits_aabb_from_inside() {
let ray = Ray::new(DVec3::ZERO, DVec3::X);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = ray_vs_aabb(&ray, &aabb).unwrap();
assert!(hit.started_inside);
assert!((hit.t - 1.0).abs() < 0.001);
}
#[test]
fn ray_hits_aabb_y_face() {
let ray = Ray::new(DVec3::new(0.0, -5.0, 0.0), DVec3::Y);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = ray_vs_aabb(&ray, &aabb).unwrap();
assert_eq!(hit.normal, DVec3::NEG_Y);
assert!((hit.t - 4.0).abs() < 0.001);
}
#[test]
fn ray_hits_aabb_z_face() {
let ray = Ray::new(DVec3::new(0.0, 0.0, -5.0), DVec3::Z);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = ray_vs_aabb(&ray, &aabb).unwrap();
assert_eq!(hit.normal, DVec3::NEG_Z);
}
#[test]
fn ray_on_aabb_face_zero_dir_component_hits() {
let ray = Ray::new(DVec3::new(0.0, 1.0, 0.0), DVec3::new(1.0, 0.0, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = ray_vs_aabb(&ray, &aabb).unwrap();
assert!(hit.started_inside);
assert!((hit.t - 1.0).abs() < 0.001);
assert_eq!(hit.normal, DVec3::X);
}
#[test]
fn ray_parallel_to_axis_outside_misses() {
let ray = Ray::new(DVec3::new(0.0, 5.0, 0.0), DVec3::new(1.0, 0.0, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
assert!(ray_vs_aabb(&ray, &aabb).is_none());
}
#[test]
fn ray_hits_aabb_it_already_entered_before_t_min() {
let ray = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X).with_bounds(4.5, 100.0);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = ray_vs_aabb(&ray, &aabb).expect("far face is within bounds");
assert!((hit.t - 6.0).abs() < 1e-9, "t = {}", hit.t);
assert!(hit.started_inside);
assert_eq!(hit.normal, DVec3::X);
}
#[test]
fn ray_hits_capsule_cylinder() {
let capsule = Capsule::new(DVec3::ZERO, 0.5, 1.0);
let ray = Ray::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::X);
let hit = ray_vs_capsule(&ray, &capsule).unwrap();
assert!((hit.t - 4.5).abs() < 0.001);
}
#[test]
fn ray_hits_capsule_top_cap() {
let capsule = Capsule::new(DVec3::ZERO, 0.5, 1.0);
let ray = Ray::new(DVec3::new(0.0, 5.0, 0.0), DVec3::NEG_Y);
let hit = ray_vs_capsule(&ray, &capsule).unwrap();
assert!((hit.t - 3.5).abs() < 0.001);
}
#[test]
fn a_ray_passing_beside_a_capsule_reports_no_hit() {
let capsule = Capsule::new(DVec3::ZERO, 0.5, 1.0);
let ray = Ray::new(DVec3::new(-5.0, 3.0, 0.0), DVec3::X);
assert!(ray_vs_capsule(&ray, &capsule).is_none());
}
#[test]
fn ray_inside_capsule_cylinder_radius_hits_the_cap_not_the_far_side() {
let capsule = Capsule::new(DVec3::ZERO, 1.0, 1.0);
let ray = Ray::new(DVec3::new(0.9, 5.0, 0.0), DVec3::new(0.0001, -1.0, 0.0));
let hit = ray_vs_capsule(&ray, &capsule).expect("ray must hit the capsule");
assert!(
hit.t < 4.5,
"expected the top cap at t ≈ 4, got t = {} at {:?}",
hit.t,
hit.point
);
assert!(
hit.point.y > 1.0,
"hit must be on the top cap: {:?}",
hit.point
);
assert!(
hit.normal.y > 0.0,
"top-cap normal must point up: {:?}",
hit.normal
);
assert!(!hit.started_inside);
}
#[test]
fn ray_started_inside_capsule_is_reported() {
let capsule = Capsule::new(DVec3::ZERO, 1.0, 2.0);
let ray = Ray::new(DVec3::ZERO, DVec3::X);
let hit = ray_vs_capsule(&ray, &capsule).expect("ray from the axis must exit the capsule");
assert!(hit.started_inside);
assert!((hit.t - 1.0).abs() < 1e-9, "t = {}", hit.t);
}
#[test]
fn swept_sphere_hits_static_sphere() {
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
let target = Sphere::new(DVec3::ZERO, 1.0);
let hit = swept_sphere_vs_sphere(&seg, 0.5, &target).unwrap();
assert!((hit.t - 0.35).abs() < 0.001, "expected 0.35, got {}", hit.t);
}
#[test]
fn swept_sphere_misses_static_sphere() {
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 5.0, 0.0), DVec3::new(5.0, 5.0, 0.0));
let target = Sphere::new(DVec3::ZERO, 1.0);
assert!(swept_sphere_vs_sphere(&seg, 0.5, &target).is_none());
}
#[test]
fn swept_sphere_starts_overlapping() {
let seg =
crate::broadphase::Segment::new(DVec3::new(0.5, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
let target = Sphere::new(DVec3::ZERO, 1.0);
let hit = swept_sphere_vs_sphere(&seg, 0.5, &target).expect("overlap at t=0 is a contact");
assert_eq!(hit.t, 0.0);
assert!(hit.started_inside);
assert!((hit.normal - DVec3::X).length() < 1e-9, "{:?}", hit.normal);
}
#[test]
fn swept_sphere_overlapping_for_the_whole_sweep_still_contacts() {
let seg = crate::broadphase::Segment::new(DVec3::ZERO, DVec3::new(0.1, 0.0, 0.0));
let target = Sphere::new(DVec3::ZERO, 5.0);
let hit = swept_sphere_vs_sphere(&seg, 0.5, &target).expect("resting contact");
assert_eq!(hit.t, 0.0);
assert!(hit.started_inside);
}
#[test]
fn swept_sphere_hits_aabb() {
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = swept_sphere_vs_aabb(&seg, 0.5, &aabb).unwrap();
assert!((hit.t - 0.35).abs() < 0.001, "expected 0.35, got {}", hit.t);
assert_eq!(hit.normal, DVec3::NEG_X);
}
#[test]
fn swept_sphere_misses_aabb() {
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 5.0, 0.0), DVec3::new(5.0, 5.0, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
assert!(swept_sphere_vs_aabb(&seg, 0.5, &aabb).is_none());
}
#[test]
fn swept_sphere_vs_aabb_starts_overlapping() {
let seg =
crate::broadphase::Segment::new(DVec3::new(0.5, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = swept_sphere_vs_aabb(&seg, 0.5, &aabb).expect("overlap at t=0 is a contact");
assert_eq!(hit.t, 0.0);
assert!(hit.started_inside);
assert_eq!(hit.normal, DVec3::X);
}
#[test]
fn tiny_sweep_starting_inside_sphere_is_a_contact() {
let seg = crate::broadphase::Segment::new(DVec3::ZERO, DVec3::new(1e-10, 0.0, 0.0));
let target = Sphere::new(DVec3::ZERO, 1.0);
let hit = swept_sphere_vs_sphere(&seg, 0.5, &target)
.expect("a sweep inside the target is a contact, not a miss");
assert_eq!(hit.t, 0.0);
assert!(hit.started_inside);
}
#[test]
fn tiny_sweep_starting_inside_aabb_is_a_contact() {
let seg = crate::broadphase::Segment::new(DVec3::ZERO, DVec3::new(1e-10, 0.0, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = swept_sphere_vs_aabb(&seg, 0.5, &aabb)
.expect("a sweep inside the box is a contact, not a miss");
assert_eq!(hit.t, 0.0);
assert!(hit.started_inside);
}
#[test]
fn empty_aabb_overlaps_nothing() {
let sphere = Sphere::new(DVec3::ZERO, 1.0);
assert!(!sphere_overlaps_aabb(&sphere, &Aabb::EMPTY));
}
#[test]
fn swept_sphere_vs_empty_aabb_is_a_miss() {
let seg = crate::broadphase::Segment::new(DVec3::ZERO, DVec3::ZERO);
assert!(swept_sphere_vs_aabb(&seg, 1.0, &Aabb::EMPTY).is_none());
}
#[test]
fn swept_sphere_vs_aabb_touches_y_face() {
let seg =
crate::broadphase::Segment::new(DVec3::new(0.0, -5.0, 0.0), DVec3::new(0.0, 5.0, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = swept_sphere_vs_aabb(&seg, 0.5, &aabb).unwrap();
assert_eq!(hit.normal, DVec3::NEG_Y);
}
#[test]
fn swept_sphere_zero_dir_on_face_hits() {
let seg =
crate::broadphase::Segment::new(DVec3::new(-2.0, 1.5, 0.0), DVec3::new(2.0, 1.5, 0.0));
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
let hit = swept_sphere_vs_aabb(&seg, 0.5, &aabb).unwrap();
assert!(hit.t >= 0.0 && hit.t <= 1.0);
}
#[test]
fn spheres_overlap_when_close() {
let a = Sphere::new(DVec3::ZERO, 1.0);
let b = Sphere::new(DVec3::new(1.5, 0.0, 0.0), 1.0);
assert!(sphere_overlaps_sphere(&a, &b));
}
#[test]
fn the_yes_or_no_checks_do_not_count_a_touch() {
let unit = Sphere::new(DVec3::ZERO, 1.0);
assert!(!sphere_overlaps_sphere(
&unit,
&Sphere::new(DVec3::new(2.0, 0.0, 0.0), 1.0)
));
let cube = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
assert!(!sphere_overlaps_aabb(
&Sphere::new(DVec3::new(2.0, 0.0, 0.0), 1.0),
&cube
));
let capsule = Capsule::new(DVec3::ZERO, 0.5, 2.0);
assert!(!sphere_overlaps_capsule(
&Sphere::new(DVec3::new(1.5, 1.0, 0.0), 1.0),
&capsule
));
}
#[test]
fn spheres_do_not_overlap_when_far() {
let a = Sphere::new(DVec3::ZERO, 1.0);
let b = Sphere::new(DVec3::new(5.0, 0.0, 0.0), 1.0);
assert!(!sphere_overlaps_sphere(&a, &b));
}
#[test]
fn sphere_overlaps_aabb_when_intersecting() {
let sphere = Sphere::new(DVec3::new(2.0, 0.0, 0.0), 1.5);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
assert!(sphere_overlaps_aabb(&sphere, &aabb));
}
#[test]
fn sphere_does_not_overlap_distant_aabb() {
let sphere = Sphere::new(DVec3::new(5.0, 0.0, 0.0), 1.0);
let aabb = Aabb::from_centre_half(DVec3::ZERO, DVec3::splat(1.0));
assert!(!sphere_overlaps_aabb(&sphere, &aabb));
}
#[test]
fn sphere_overlaps_capsule_body() {
let sphere = Sphere::new(DVec3::new(1.0, 0.0, 0.0), 0.6);
let capsule = Capsule::new(DVec3::ZERO, 0.5, 2.0);
assert!(sphere_overlaps_capsule(&sphere, &capsule));
}
#[test]
fn sphere_overlaps_capsule_cap() {
let sphere = Sphere::new(DVec3::new(0.0, 3.0, 0.0), 0.6);
let capsule = Capsule::new(DVec3::ZERO, 0.5, 2.0);
assert!(sphere_overlaps_capsule(&sphere, &capsule));
}
#[test]
fn sphere_does_not_overlap_distant_capsule() {
let sphere = Sphere::new(DVec3::new(10.0, 0.0, 0.0), 1.0);
let capsule = Capsule::new(DVec3::ZERO, 0.5, 2.0);
assert!(!sphere_overlaps_capsule(&sphere, &capsule));
}
#[test]
fn swept_sphere_hits_capsule_cylinder() {
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
let capsule = Capsule::new(DVec3::ZERO, 0.5, 2.0);
let hit = swept_sphere_vs_capsule(&seg, 0.5, &capsule).unwrap();
assert!(hit.t > 0.0 && hit.t < 1.0);
assert!((hit.normal.x + 1.0).abs() < 0.1); }
#[test]
fn swept_sphere_misses_capsule() {
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 5.0, 0.0), DVec3::new(5.0, 5.0, 0.0));
let capsule = Capsule::new(DVec3::ZERO, 0.5, 2.0);
assert!(swept_sphere_vs_capsule(&seg, 0.5, &capsule).is_none());
}
#[test]
fn swept_sphere_overlapping_tall_capsule_pushes_out_sideways() {
let capsule = Capsule::new(DVec3::ZERO, 1.0, 5.0);
let seg =
crate::broadphase::Segment::new(DVec3::new(1.2, 5.0, 0.0), DVec3::new(1.2, 5.0, 0.0));
let hit = swept_sphere_vs_capsule(&seg, 0.5, &capsule).expect("resting contact");
assert_eq!(hit.t, 0.0);
assert!(hit.started_inside);
assert!(
(hit.normal - DVec3::X).length() < 1e-9,
"expected a lateral normal, got {:?}",
hit.normal
);
}
#[test]
fn swept_sphere_vs_capsule_touches_top_cap() {
let seg =
crate::broadphase::Segment::new(DVec3::new(0.0, 5.0, 0.0), DVec3::new(0.0, 0.0, 0.0));
let capsule = Capsule::new(DVec3::ZERO, 0.5, 2.0);
let hit = swept_sphere_vs_capsule(&seg, 0.5, &capsule).unwrap();
assert!((hit.t - 0.4).abs() < 0.01); assert!(hit.normal.y > 0.5); }
#[test]
fn a_capsule_sweep_strikes_a_curb_the_sphere_at_its_waist_rides_over() {
let (radius, half_height) = (0.4, 0.5);
let centre_y = radius + half_height;
let seg = crate::broadphase::Segment::new(
DVec3::new(-3.0, centre_y, 0.0),
DVec3::new(3.0, centre_y, 0.0),
);
let curb = Aabb::new(DVec3::new(1.0, 0.0, -2.0), DVec3::new(3.0, 0.3, 2.0));
assert!(
swept_sphere_vs_aabb(&seg, radius, &curb).is_none(),
"the sphere must miss the curb, or the capsule hitting it proves nothing",
);
let hit = swept_capsule_vs_aabb(&seg, radius, half_height, &curb)
.expect("the capsule reaches down to the curb");
let rise = 0.1_f64;
let expected = DVec3::new(-(radius * radius - rise * rise).sqrt(), rise, 0.0) / radius;
assert!(
(hit.normal - expected).length() < 1e-12,
"struck the curb's top edge: {:?}",
hit.normal
);
}
#[test]
fn a_capsule_landing_reports_the_contact_on_the_floor_not_the_grown_box() {
let (radius, half_height) = (0.4, 0.5);
let floor = Aabb::new(DVec3::new(-5.0, -1.0, -5.0), DVec3::new(5.0, 0.0, 5.0));
let seg =
crate::broadphase::Segment::new(DVec3::new(0.0, 3.0, 0.0), DVec3::new(0.0, 0.5, 0.0));
let hit = swept_capsule_vs_aabb(&seg, radius, half_height, &floor).expect("lands");
assert_eq!(hit.normal, DVec3::Y);
let touch_y = radius + half_height;
assert!(
(hit.t - (3.0 - touch_y) / 2.5).abs() < 1e-12,
"expected the TOI where the bottom cap meets the floor, got t = {}",
hit.t,
);
assert!(
(hit.point.y - floor.max.y).abs() < 1e-12,
"contact reported at y = {}, and the floor's top is y = {}",
hit.point.y,
floor.max.y,
);
}
#[test]
fn a_capsule_sweep_misses_a_box_that_is_empty_only_on_y() {
let inverted = Aabb::new(DVec3::new(-1.0, 1.0, -1.0), DVec3::new(1.0, -1.0, 1.0));
assert!(inverted.is_empty());
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 0.0, 0.0), DVec3::new(5.0, 0.0, 0.0));
assert!(swept_capsule_vs_aabb(&seg, 0.5, 2.0, &inverted).is_none());
}
#[test]
fn a_capsule_flank_hits_a_sphere_above_its_centre_line() {
let (radius, half_height) = (0.5, 2.0);
let target = Sphere::new(DVec3::ZERO, 1.0);
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 1.6, 0.0), DVec3::new(5.0, 1.6, 0.0));
assert!(
swept_sphere_vs_sphere(&seg, radius, &target).is_none(),
"the sphere must miss, or the capsule hitting it proves nothing",
);
let hit = swept_capsule_vs_sphere(&seg, radius, half_height, &target).expect("flank hit");
assert!(
(hit.t - (5.0 - 1.5) / 10.0).abs() < 1e-12,
"expected the TOI where the flank meets the sphere, got t = {}",
hit.t,
);
assert!(
(hit.normal - DVec3::NEG_X).length() < 1e-12,
"got {:?}",
hit.normal
);
assert!(
(hit.point - DVec3::new(-1.0, 0.0, 0.0)).length() < 1e-12,
"the contact belongs on the target sphere's surface, and came back at {:?}",
hit.point,
);
}
#[test]
fn a_capsule_sweep_re_anchors_its_contact_on_the_static_capsules_axis() {
let (radius, half_height) = (0.5, 3.0);
let target = Capsule::new(DVec3::ZERO, 0.5, 1.0);
let seg =
crate::broadphase::Segment::new(DVec3::new(-5.0, 2.0, 0.0), DVec3::new(5.0, 2.0, 0.0));
let hit =
swept_capsule_vs_capsule(&seg, radius, half_height, &target).expect("flanks meet");
assert!(
(hit.t - (5.0 - 1.0) / 10.0).abs() < 1e-12,
"expected the TOI where the two flanks touch, got t = {}",
hit.t,
);
assert!(
(hit.normal - DVec3::NEG_X).length() < 1e-12,
"got {:?}",
hit.normal
);
let off_axis = hit.point - closest_point_on_capsule_axis(&target, hit.point);
assert!(
(off_axis.length() - target.radius).abs() < 1e-12,
"the contact is {} from the target's axis and its surface is {} out — \
the proxy's axis is taller than the target's, so this is what an \
un-re-anchored point fails",
off_axis.length(),
target.radius,
);
}
#[test]
fn a_capsule_sweep_that_starts_overlapping_is_a_contact_at_zero() {
let (radius, half_height) = (0.4, 0.9);
let wall = Aabb::new(DVec3::new(0.0, -5.0, -5.0), DVec3::new(5.0, 5.0, 5.0));
let seg =
crate::broadphase::Segment::new(DVec3::new(0.2, 0.0, 0.0), DVec3::new(0.2, 0.0, 1.0));
let hit = swept_capsule_vs_aabb(&seg, radius, half_height, &wall).expect("resting contact");
assert_eq!(hit.t, 0.0);
assert!(hit.started_inside);
assert_eq!(
hit.normal,
DVec3::NEG_X,
"the shortest way out is back through the face it entered",
);
}
}