parry2d/query/point/
point_ball.rs

1use crate::math::ComplexField;
2
3use crate::math::{Real, Vector};
4use crate::query::{PointProjection, PointQuery};
5use crate::shape::{Ball, FeatureId};
6
7impl PointQuery for Ball {
8    #[inline]
9    fn project_local_point(&self, pt: Vector, solid: bool) -> PointProjection {
10        let distance_squared = pt.length_squared();
11
12        let inside = distance_squared <= self.radius * self.radius;
13
14        if inside && solid {
15            PointProjection::new(true, pt)
16        } else {
17            let proj = pt * (self.radius / <Real as ComplexField>::sqrt(distance_squared));
18            PointProjection::new(inside, proj)
19        }
20    }
21
22    #[inline]
23    fn project_local_point_and_get_feature(&self, pt: Vector) -> (PointProjection, FeatureId) {
24        (self.project_local_point(pt, false), FeatureId::Face(0))
25    }
26
27    #[inline]
28    fn distance_to_local_point(&self, pt: Vector, solid: bool) -> Real {
29        let dist = pt.length() - self.radius;
30
31        if solid && dist < 0.0 {
32            0.0
33        } else {
34            dist
35        }
36    }
37
38    #[inline]
39    fn contains_local_point(&self, pt: Vector) -> bool {
40        pt.length_squared() <= self.radius * self.radius
41    }
42}