use glam::{DMat3, DQuat, DVec3};
use crate::collider::Aabb;
use crate::components::{ColliderComponent, Transform};
use crate::compound_shape::CompoundPart;
use crate::mesh::triangle_bounds;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ContactShape {
Sphere {
centre: DVec3,
radius: f64,
},
Capsule {
a: DVec3,
b: DVec3,
radius: f64,
},
Box {
centre: DVec3,
rotation: DQuat,
half: DVec3,
},
Triangle {
corners: [DVec3; 3],
normal: DVec3,
active_edges: u8,
},
Plane {
normal: DVec3,
offset: f64,
},
}
impl ContactShape {
#[must_use]
pub fn placed(component: &ColliderComponent, transform: &Transform) -> Option<Self> {
Self::placed_part(component, 0, transform)
}
#[must_use]
pub fn placed_part(
component: &ColliderComponent,
part: usize,
transform: &Transform,
) -> Option<Self> {
let rotation = transform.rotation;
match *component {
ColliderComponent::Compound {
offset,
ref shape,
is_trigger,
} => shape
.parts()
.get(part)
.filter(|_| !is_trigger)
.map(|part| Self::compound_part(part, offset, transform)),
ColliderComponent::Mesh {
ref mesh,
is_trigger,
} => (!is_trigger && part < mesh.triangle_count()).then(|| Self::Triangle {
corners: mesh
.corners(part)
.map(|corner| transform.position + rotation * corner),
normal: rotation * mesh.normal(part),
active_edges: mesh.active_edges(part),
}),
_ if part != 0 => None,
ColliderComponent::Sphere {
offset,
radius,
is_trigger,
} => (!is_trigger).then(|| Self::Sphere {
centre: transform.position + rotation * offset,
radius,
}),
ColliderComponent::Capsule {
offset,
radius,
half_height,
is_trigger,
} => (!is_trigger).then(|| {
let centre = transform.position + rotation * offset;
let axis = rotation * DVec3::new(0.0, half_height, 0.0);
Self::Capsule {
a: centre - axis,
b: centre + axis,
radius,
}
}),
ColliderComponent::Box {
offset,
half_extents,
is_trigger,
} => (!is_trigger).then(|| Self::Box {
centre: transform.position + rotation * offset,
rotation,
half: half_extents,
}),
}
}
#[must_use]
pub(crate) fn compound_part(part: &CompoundPart, offset: DVec3, transform: &Transform) -> Self {
crate::query::contact_box(&part.placed(offset, transform))
}
#[must_use]
pub const fn rank(&self) -> u8 {
match self {
Self::Sphere { .. } => 0,
Self::Capsule { .. } => 1,
Self::Box { .. } => 2,
Self::Triangle { .. } => 3,
Self::Plane { .. } => 4,
}
}
#[must_use]
pub fn aabb(&self) -> Option<Aabb> {
match *self {
Self::Sphere { centre, radius } => {
Some(Aabb::from_centre_half(centre, DVec3::splat(radius)))
}
Self::Capsule { a, b, radius } => Some(Aabb::new(
a.min(b) - DVec3::splat(radius),
a.max(b) + DVec3::splat(radius),
)),
Self::Box {
centre,
rotation,
half,
} => {
let m = DMat3::from_quat(rotation);
let reach = DVec3::new(
m.x_axis.x.abs() * half.x
+ m.y_axis.x.abs() * half.y
+ m.z_axis.x.abs() * half.z,
m.x_axis.y.abs() * half.x
+ m.y_axis.y.abs() * half.y
+ m.z_axis.y.abs() * half.z,
m.x_axis.z.abs() * half.x
+ m.y_axis.z.abs() * half.y
+ m.z_axis.z.abs() * half.z,
);
Some(Aabb::from_centre_half(centre, reach))
}
Self::Triangle { corners, .. } => Some(triangle_bounds(&corners)),
Self::Plane { .. } => None,
}
}
#[must_use]
pub fn reach_from(&self, centre: DVec3) -> f64 {
match *self {
Self::Sphere {
centre: c, radius, ..
} => (c - centre).length() + radius,
Self::Capsule { a, b, radius } => {
(a - centre).length().max((b - centre).length()) + radius
}
Self::Box {
centre: c, half, ..
} => (c - centre).length() + half.length(),
Self::Triangle { corners, .. } => corners.iter().fold(0.0, |reach: f64, corner| {
reach.max((*corner - centre).length())
}),
Self::Plane { .. } => 0.0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_turned_capsule_lies_along_its_turned_axis() {
let quarter = DQuat::from_xyzw(
0.0,
0.0,
core::f64::consts::FRAC_1_SQRT_2,
core::f64::consts::FRAC_1_SQRT_2,
);
let shape = ContactShape::placed(
&ColliderComponent::Capsule {
offset: DVec3::ZERO,
radius: 0.1,
half_height: 0.5,
is_trigger: false,
},
&Transform::new(DVec3::new(0.0, 1.0, 0.0), quarter),
)
.expect("a solid capsule");
let ContactShape::Capsule { a, b, .. } = shape else {
panic!("placed as {shape:?}");
};
assert!((a - DVec3::new(0.5, 1.0, 0.0)).length() < 1e-12, "{a:?}");
assert!((b - DVec3::new(-0.5, 1.0, 0.0)).length() < 1e-12, "{b:?}");
let bounds = shape.aabb().expect("bounded");
assert!((bounds.extents().x - 1.2).abs() < 1e-12, "{bounds:?}");
assert!((bounds.extents().y - 0.2).abs() < 1e-12, "{bounds:?}");
}
#[test]
fn a_turned_boxs_bounds_hold_its_corners_and_a_trigger_places_nothing() {
let turn = crate::rotation_from_scaled_axis(DVec3::new(0.3, 0.7, -0.2));
let half = DVec3::new(0.5, 0.2, 0.9);
let shape = ContactShape::Box {
centre: DVec3::new(1.0, 2.0, 3.0),
rotation: turn,
half,
};
let bounds = shape.aabb().expect("bounded");
for i in 0..8u32 {
let sign = |bit: u32| if i & (1 << bit) == 0 { -1.0 } else { 1.0 };
let corner =
DVec3::new(1.0, 2.0, 3.0) + turn * (half * DVec3::new(sign(0), sign(1), sign(2)));
assert!(
bounds.inflated(1e-12).contains(&Aabb::new(corner, corner)),
"corner {corner:?} outside {bounds:?}"
);
}
assert_eq!(
ContactShape::placed(
&ColliderComponent::Sphere {
offset: DVec3::ZERO,
radius: 1.0,
is_trigger: true,
},
&Transform::IDENTITY,
),
None
);
}
}