1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::math::{Isometry, Point, Vector};
use crate::query::{PointProjection, PointQuery};
use crate::shape::{Capsule, FeatureId, Segment};
use na::{self, RealField, Unit};

impl<N: RealField> PointQuery<N> for Capsule<N> {
    #[inline]
    fn project_point(&self, m: &Isometry<N>, pt: &Point<N>, solid: bool) -> PointProjection<N> {
        let mut y = Point::origin();
        y[1] = self.half_height();
        let seg = Segment::new(-y, y);
        let proj = seg.project_point(m, pt, solid);
        let dproj = *pt - proj.point;

        if let Some((dir, dist)) = Unit::try_new_and_get(dproj, N::default_epsilon()) {
            let inside = dist <= self.radius();
            if solid && inside {
                PointProjection::new(true, *pt)
            } else {
                PointProjection::new(inside, proj.point + dir.into_inner() * self.radius())
            }
        } else {
            if solid {
                PointProjection::new(true, *pt)
            } else {
                let mut dir: Vector<N> = na::zero();
                dir[1] = na::one();
                dir = m * dir;
                PointProjection::new(true, proj.point + dir * self.radius())
            }
        }
    }

    #[inline]
    fn project_point_with_feature(
        &self,
        m: &Isometry<N>,
        pt: &Point<N>,
    ) -> (PointProjection<N>, FeatureId) {
        (self.project_point(m, pt, false), FeatureId::Face(0))
    }
}