ncollide3d/pipeline/narrow_phase/contact_generator/
capsule_shape_manifold_generator.rs

1use crate::math::Isometry;
2use crate::pipeline::{ContactAlgorithm, ContactDispatcher, ContactManifoldGenerator};
3use crate::query::{ContactManifold, ContactPrediction, ContactPreprocessor};
4use crate::shape::{Capsule, Shape};
5use na::{self, RealField};
6
7/// Collision detector between a concave shape and another shape.
8pub struct CapsuleShapeManifoldGenerator<N: RealField + Copy> {
9    sub_detector: Option<ContactAlgorithm<N>>,
10    flip: bool,
11}
12
13impl<N: RealField + Copy> CapsuleShapeManifoldGenerator<N> {
14    /// Creates a new collision detector between a concave shape and another shape.
15    pub fn new(flip: bool) -> CapsuleShapeManifoldGenerator<N> {
16        CapsuleShapeManifoldGenerator {
17            sub_detector: None,
18            flip,
19        }
20    }
21
22    fn do_update(
23        &mut self,
24        dispatcher: &dyn ContactDispatcher<N>,
25        m1: &Isometry<N>,
26        g1: &Capsule<N>,
27        proc1: Option<&dyn ContactPreprocessor<N>>,
28        m2: &Isometry<N>,
29        g2: &dyn Shape<N>,
30        proc2: Option<&dyn ContactPreprocessor<N>>,
31        prediction: &ContactPrediction<N>,
32        manifold: &mut ContactManifold<N>,
33        flip: bool,
34    ) -> bool {
35        let segment = g1.segment();
36        let mut prediction = prediction.clone();
37        let new_linear_prediction = prediction.linear() + g1.radius;
38        prediction.set_linear(new_linear_prediction);
39
40        if self.sub_detector.is_none() {
41            self.sub_detector = if flip {
42                dispatcher.get_contact_algorithm(g2, &segment)
43            } else {
44                dispatcher.get_contact_algorithm(&segment, g2)
45            }
46        }
47
48        // Update all collisions
49        if flip {
50            self.sub_detector.as_mut().unwrap().generate_contacts(
51                dispatcher,
52                m2,
53                g2,
54                proc2,
55                m1,
56                &segment,
57                Some(&(proc1, &g1.contact_preprocessor())),
58                &prediction,
59                manifold,
60            )
61        } else {
62            self.sub_detector.as_mut().unwrap().generate_contacts(
63                dispatcher,
64                m1,
65                &segment,
66                Some(&(proc1, &g1.contact_preprocessor())),
67                m2,
68                g2,
69                proc2,
70                &prediction,
71                manifold,
72            )
73        }
74    }
75}
76
77impl<N: RealField + Copy> ContactManifoldGenerator<N> for CapsuleShapeManifoldGenerator<N> {
78    fn generate_contacts(
79        &mut self,
80        d: &dyn ContactDispatcher<N>,
81        ma: &Isometry<N>,
82        a: &dyn Shape<N>,
83        proc1: Option<&dyn ContactPreprocessor<N>>,
84        mb: &Isometry<N>,
85        b: &dyn Shape<N>,
86        proc2: Option<&dyn ContactPreprocessor<N>>,
87        prediction: &ContactPrediction<N>,
88        manifold: &mut ContactManifold<N>,
89    ) -> bool {
90        if !self.flip {
91            if let Some(cs) = a.as_shape::<Capsule<N>>() {
92                return self.do_update(d, ma, cs, proc1, mb, b, proc2, prediction, manifold, false);
93            }
94        } else {
95            if let Some(cs) = b.as_shape::<Capsule<N>>() {
96                return self.do_update(d, mb, cs, proc2, ma, a, proc1, prediction, manifold, true);
97            }
98        }
99
100        return false;
101    }
102}