ncollide3d/shape/
capsule.rsuse na::{self, RealField, Unit};
use crate::math::{Point, Vector};
use crate::query::{Contact, ContactKinematic, ContactPreprocessor};
use crate::shape::{FeatureId, Segment, SupportMap};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Capsule<N> {
pub half_height: N,
pub radius: N,
}
impl<N: RealField + Copy> Capsule<N> {
pub fn new(half_height: N, radius: N) -> Capsule<N> {
Capsule {
half_height,
radius,
}
}
#[inline]
#[deprecated(note = "use the `self.half_height` public field directly.")]
pub fn half_height(&self) -> N {
self.half_height
}
#[inline]
pub fn height(&self) -> N {
self.half_height * na::convert(2.0)
}
#[inline]
#[deprecated(note = "use the `self.radius` public field directly.")]
pub fn radius(&self) -> N {
self.radius
}
#[inline]
pub fn segment(&self) -> Segment<N> {
let mut a = Point::origin();
let mut b = Point::origin();
a.y = -self.half_height;
b.y = self.half_height;
Segment::new(a, b)
}
#[inline]
pub fn contact_preprocessor(&self) -> impl ContactPreprocessor<N> {
CapsuleContactPreprocessor {
radius: self.radius,
}
}
}
impl<N: RealField + Copy> SupportMap<N> for Capsule<N> {
#[inline]
fn local_support_point(&self, dir: &Vector<N>) -> Point<N> {
self.local_support_point_toward(&Unit::new_normalize(*dir))
}
#[inline]
fn local_support_point_toward(&self, dir: &Unit<Vector<N>>) -> Point<N> {
let mut res: Vector<N> = na::zero();
res[1] = self.half_height.copysign(dir[1]);
Point::from(res + **dir * self.radius)
}
}
struct CapsuleContactPreprocessor<N: RealField + Copy> {
radius: N,
}
impl<N: RealField + Copy> CapsuleContactPreprocessor<N> {
}
impl<N: RealField + Copy> ContactPreprocessor<N> for CapsuleContactPreprocessor<N> {
fn process_contact(
&self,
c: &mut Contact<N>,
kinematic: &mut ContactKinematic<N>,
is_first: bool,
) -> bool {
let feature = if is_first {
kinematic.feature1()
} else {
kinematic.feature2()
};
let actual_feature = match feature {
FeatureId::Vertex(i) => FeatureId::Face(i),
#[cfg(feature = "dim3")]
FeatureId::Edge(_) => FeatureId::Face(2),
FeatureId::Face(i) => FeatureId::Face(2 + i),
FeatureId::Unknown => return false,
};
if is_first {
kinematic.set_feature1(actual_feature);
kinematic.set_dilation1(self.radius);
c.world1 += *c.normal * self.radius;
c.depth += self.radius;
} else {
kinematic.set_feature2(actual_feature);
kinematic.set_dilation2(self.radius);
c.world2 -= *c.normal * self.radius;
c.depth += self.radius;
}
true
}
}