use glam::{DQuat, DVec3};
use super::capsules::advance_upright_capsule;
use super::{
OverlapHit, Penetration, ShapeHit, capsule_penetration_vs_aabb, ray_vs_aabb,
sphere_overlap_vs_aabb, swept_capsule_vs_aabb, swept_sphere_vs_aabb,
};
use crate::broadphase::{Ray, Segment};
use crate::collider::{Aabb, BoxCollider, Capsule, Sphere};
use crate::contact::manifold::{closest_on_segment_to_box, gap};
use crate::contact::shape::ContactShape;
#[must_use]
pub fn sphere_overlaps_box(sphere: &Sphere, target: &BoxCollider) -> bool {
sphere_overlap_vs_box(sphere, target).is_some()
}
#[must_use]
pub fn sphere_overlap_vs_box(sphere: &Sphere, target: &BoxCollider) -> Option<OverlapHit> {
if !target.is_turned() {
return sphere_overlap_vs_aabb(sphere, &target.aabb());
}
let local = Sphere::new(target.local_point(sphere.centre), sphere.radius);
let hit = sphere_overlap_vs_aabb(&local, &target.local_aabb())?;
Some(OverlapHit {
point: target.world_point(hit.point),
normal: target.rotation * hit.normal,
..hit
})
}
#[must_use]
pub fn aabb_overlaps_box(aabb: &Aabb, target: &BoxCollider) -> bool {
if !target.is_turned() {
return aabb.intersects(&target.aabb());
}
if aabb.is_empty() {
return false;
}
let query = ContactShape::Box {
centre: aabb.centre(),
rotation: DQuat::IDENTITY,
half: aabb.extents() * 0.5,
};
gap(&query, &contact_box(target)).0 <= 0.0
}
#[must_use]
pub fn ray_vs_box(ray: &Ray, target: &BoxCollider) -> Option<ShapeHit> {
if !target.is_turned() {
return ray_vs_aabb(ray, &target.aabb());
}
let local = Ray {
origin: target.local_point(ray.origin),
dir: target.rotation.inverse() * ray.dir,
..*ray
};
let hit = ray_vs_aabb(&local, &target.local_aabb())?;
Some(ShapeHit {
point: ray.origin + ray.dir * hit.t,
normal: target.rotation * hit.normal,
..hit
})
}
#[must_use]
pub fn swept_sphere_vs_box(
segment: &Segment,
swept_radius: f64,
target: &BoxCollider,
) -> Option<ShapeHit> {
if !target.is_turned() {
return swept_sphere_vs_aabb(segment, swept_radius, &target.aabb());
}
let local = Segment::new(
target.local_point(segment.start),
target.local_point(segment.end),
);
let hit = swept_sphere_vs_aabb(&local, swept_radius, &target.local_aabb())?;
Some(ShapeHit {
point: target.world_point(hit.point),
normal: target.rotation * hit.normal,
..hit
})
}
#[must_use]
pub fn swept_capsule_vs_box(
segment: &Segment,
swept_radius: f64,
swept_half_height: f64,
target: &BoxCollider,
) -> Option<ShapeHit> {
if !target.is_turned() {
return swept_capsule_vs_aabb(segment, swept_radius, swept_half_height, &target.aabb());
}
let (t, normal, started_inside) = advance_upright_capsule(
&contact_box(target),
segment,
swept_radius,
swept_half_height,
)?;
let up = DVec3::Y * swept_half_height;
let centre = segment.start + (segment.end - segment.start) * t;
Some(ShapeHit {
t,
point: nearest_on_box(target, centre - up, centre + up),
normal,
started_inside,
part: 0,
})
}
#[must_use]
pub fn capsule_penetration_vs_box(capsule: &Capsule, target: &BoxCollider) -> Option<Penetration> {
if !target.is_turned() {
return capsule_penetration_vs_aabb(capsule, &target.aabb());
}
let core = ContactShape::Capsule {
a: capsule.bottom(),
b: capsule.top(),
radius: capsule.radius,
};
let (distance, normal) = gap(&contact_box(target), &core);
(distance < 0.0).then_some(Penetration {
normal,
depth: -distance,
})
}
pub(crate) fn contact_box(target: &BoxCollider) -> ContactShape {
ContactShape::Box {
centre: target.centre,
rotation: target.rotation,
half: target.half_extents,
}
}
pub(crate) fn nearest_on_box(target: &BoxCollider, a: DVec3, b: DVec3) -> DVec3 {
let p0 = target.local_point(a);
let along = target.local_point(b) - p0;
let t = closest_on_segment_to_box(p0, along, target.half_extents);
let half = target.half_extents;
target.world_point((p0 + along * t).clamp(-half, half))
}
#[cfg(test)]
mod tests;