use glam::{DQuat, DVec3};
use super::{
OverlapHit, Penetration, ShapeHit, capsule_penetration_vs_capsule, ray_vs_capsule,
sphere_overlap_vs_capsule, swept_capsule_vs_capsule, swept_sphere_vs_capsule,
};
use crate::broadphase::{Ray, Segment};
use crate::collider::{Aabb, Capsule, Sphere};
use crate::contact::manifold::{closest_between_segments, gap};
use crate::contact::shape::ContactShape;
use crate::contact::sweep::time_of_contact;
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct TurnedCapsule {
pub(crate) capsule: Capsule,
pub(crate) rotation: DQuat,
}
impl TurnedCapsule {
pub(crate) const fn upright(capsule: Capsule) -> Self {
Self {
capsule,
rotation: DQuat::IDENTITY,
}
}
pub(crate) const fn new(capsule: Capsule, rotation: DQuat) -> Self {
Self { capsule, rotation }
}
fn stands(&self) -> bool {
let axis = self.rotation * DVec3::Y;
axis.x == 0.0 && axis.z == 0.0
}
pub(crate) fn core(&self) -> (DVec3, DVec3) {
if self.stands() {
return (self.capsule.bottom(), self.capsule.top());
}
let axis = self.rotation * DVec3::new(0.0, self.capsule.half_height, 0.0);
(self.capsule.centre - axis, self.capsule.centre + axis)
}
pub(crate) fn aabb(&self) -> Aabb {
if self.stands() {
return self.capsule.aabb();
}
let (a, b) = self.core();
let radius = DVec3::splat(self.capsule.radius);
Aabb::new(a.min(b) - radius, a.max(b) + radius)
}
pub(crate) fn contact_shape(&self) -> ContactShape {
let (a, b) = self.core();
ContactShape::Capsule {
a,
b,
radius: self.capsule.radius,
}
}
fn local_point(&self, point: DVec3) -> DVec3 {
self.capsule.centre + self.rotation.inverse() * (point - self.capsule.centre)
}
fn world_point(&self, point: DVec3) -> DVec3 {
self.capsule.centre + self.rotation * (point - self.capsule.centre)
}
}
pub(crate) fn sphere_overlap_vs_turned_capsule(
sphere: &Sphere,
target: &TurnedCapsule,
) -> Option<OverlapHit> {
if target.stands() {
return sphere_overlap_vs_capsule(sphere, &target.capsule);
}
let local = Sphere::new(target.local_point(sphere.centre), sphere.radius);
let hit = sphere_overlap_vs_capsule(&local, &target.capsule)?;
Some(OverlapHit {
point: target.world_point(hit.point),
normal: target.rotation * hit.normal,
..hit
})
}
pub(crate) fn ray_vs_turned_capsule(ray: &Ray, target: &TurnedCapsule) -> Option<ShapeHit> {
if target.stands() {
return ray_vs_capsule(ray, &target.capsule);
}
let local = Ray {
origin: target.local_point(ray.origin),
dir: target.rotation.inverse() * ray.dir,
..*ray
};
let hit = ray_vs_capsule(&local, &target.capsule)?;
Some(ShapeHit {
point: ray.origin + ray.dir * hit.t,
normal: target.rotation * hit.normal,
..hit
})
}
pub(crate) fn swept_sphere_vs_turned_capsule(
segment: &Segment,
swept_radius: f64,
target: &TurnedCapsule,
) -> Option<ShapeHit> {
if target.stands() {
return swept_sphere_vs_capsule(segment, swept_radius, &target.capsule);
}
let local = Segment::new(
target.local_point(segment.start),
target.local_point(segment.end),
);
let hit = swept_sphere_vs_capsule(&local, swept_radius, &target.capsule)?;
Some(ShapeHit {
point: target.world_point(hit.point),
normal: target.rotation * hit.normal,
..hit
})
}
pub(crate) fn swept_capsule_vs_turned_capsule(
segment: &Segment,
swept_radius: f64,
swept_half_height: f64,
target: &TurnedCapsule,
) -> Option<ShapeHit> {
if target.stands() {
return swept_capsule_vs_capsule(segment, swept_radius, swept_half_height, &target.capsule);
}
let (t, normal, started_inside) = advance_upright_capsule(
&target.contact_shape(),
segment,
swept_radius,
swept_half_height,
)?;
let centre = segment.start + (segment.end - segment.start) * t;
let up = DVec3::Y * swept_half_height;
let (a, b) = target.core();
let (core_point, _) = closest_between_segments(a, b, centre - up, centre + up);
Some(ShapeHit {
t,
point: core_point + normal * target.capsule.radius,
normal,
started_inside,
part: 0,
})
}
pub(super) fn advance_upright_capsule(
obstacle: &ContactShape,
segment: &Segment,
radius: f64,
half_height: f64,
) -> Option<(f64, DVec3, bool)> {
let motion = segment.end - segment.start;
let up = DVec3::Y * half_height;
let at = |t: f64| {
let centre = segment.start + motion * t;
ContactShape::Capsule {
a: centre - up,
b: centre + up,
radius,
}
};
let start = gap(obstacle, &at(0.0));
if start.0 <= 0.0 {
return Some((0.0, start.1, true));
}
let t = time_of_contact(obstacle, at, motion, start)?;
Some((t, gap(obstacle, &at(t)).1, false))
}
pub(crate) fn capsule_penetration_vs_turned_capsule(
capsule: &Capsule,
target: &TurnedCapsule,
) -> Option<Penetration> {
if target.stands() {
return capsule_penetration_vs_capsule(capsule, &target.capsule);
}
let core = ContactShape::Capsule {
a: capsule.bottom(),
b: capsule.top(),
radius: capsule.radius,
};
let (distance, normal) = gap(&target.contact_shape(), &core);
(distance < 0.0).then_some(Penetration {
normal,
depth: -distance,
})
}