ncollide3d/query/point/
point_capsule.rs

1use crate::math::{Isometry, Point, Vector};
2use crate::query::{PointProjection, PointQuery};
3use crate::shape::{Capsule, FeatureId, Segment};
4use na::{self, RealField, Unit};
5
6impl<N: RealField + Copy> PointQuery<N> for Capsule<N> {
7    #[inline]
8    fn project_point(&self, m: &Isometry<N>, pt: &Point<N>, solid: bool) -> PointProjection<N> {
9        let mut y = Point::origin();
10        y[1] = self.half_height;
11        let seg = Segment::new(-y, y);
12        let proj = seg.project_point(m, pt, solid);
13        let dproj = *pt - proj.point;
14
15        if let Some((dir, dist)) = Unit::try_new_and_get(dproj, N::default_epsilon()) {
16            let inside = dist <= self.radius;
17            if solid && inside {
18                PointProjection::new(true, *pt)
19            } else {
20                PointProjection::new(inside, proj.point + dir.into_inner() * self.radius)
21            }
22        } else {
23            if solid {
24                PointProjection::new(true, *pt)
25            } else {
26                let mut dir: Vector<N> = na::zero();
27                dir[1] = na::one();
28                dir = m * dir;
29                PointProjection::new(true, proj.point + dir * self.radius)
30            }
31        }
32    }
33
34    #[inline]
35    fn project_point_with_feature(
36        &self,
37        m: &Isometry<N>,
38        pt: &Point<N>,
39    ) -> (PointProjection<N>, FeatureId) {
40        (self.project_point(m, pt, false), FeatureId::Face(0))
41    }
42}