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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use na::{Unit, Real};
use math::{Isometry, Point};
use utils::{IdAllocator, IsometryOps};
use bounding_volume::PolyhedralCone;
use shape::{Ball, FeatureId, Shape};
use query::{Contact, ContactKinematic, ContactManifold, ContactPrediction};
use pipeline::narrow_phase::{ContactDispatcher, ContactManifoldGenerator};

/// Collision detector between two balls.
pub struct BallConvexPolyhedronManifoldGenerator<N: Real> {
    flip: bool,
    contact_manifold: ContactManifold<N>,
}

impl<N: Real> Clone for BallConvexPolyhedronManifoldGenerator<N> {
    fn clone(&self) -> BallConvexPolyhedronManifoldGenerator<N> {
        BallConvexPolyhedronManifoldGenerator {
            flip: self.flip,
            contact_manifold: self.contact_manifold.clone(),
        }
    }
}

impl<N: Real> BallConvexPolyhedronManifoldGenerator<N> {
    /// Creates a new persistent collision detector between two balls.
    #[inline]
    pub fn new(flip: bool) -> BallConvexPolyhedronManifoldGenerator<N> {
        BallConvexPolyhedronManifoldGenerator {
            flip,
            contact_manifold: ContactManifold::new(),
        }
    }

    fn do_update(
        &mut self,
        m1: &Isometry<N>,
        a: &Shape<N>,
        m2: &Isometry<N>,
        b: &Shape<N>,
        prediction: &ContactPrediction<N>,
        id_alloc: &mut IdAllocator,
        flip: bool,
    ) -> bool {
        if let (Some(ball), Some(pq2), Some(cp2)) = (
            a.as_shape::<Ball<N>>(),
            b.as_point_query(),
            b.as_convex_polyhedron(),
        ) {
            self.contact_manifold.save_cache_and_clear(id_alloc);

            let ball_center = Point::from_coordinates(m1.translation.vector);
            let (proj, f2) = pq2.project_point_with_feature(m2, &ball_center);
            let world2 = proj.point;
            let dpt = world2 - ball_center;

            if let Some((dir, dist)) = Unit::try_new_and_get(dpt, N::default_epsilon()) {
                let depth;
                let normal;

                if proj.is_inside {
                    depth = dist + ball.radius();
                    normal = -dir;
                } else {
                    depth = -dist + ball.radius();
                    normal = dir;
                }

                if depth >= -prediction.linear {
                    let mut kinematic = ContactKinematic::new();
                    let f1 = FeatureId::Face(0);
                    let world1 = ball_center + normal.unwrap() * ball.radius();

                    let contact;

                    if !flip {
                        contact = Contact::new(world1, world2, normal, depth);
                        kinematic.set_point1(f1, Point::origin(), PolyhedralCone::Full);
                        kinematic.set_dilation1(ball.radius());
                    } else {
                        contact = Contact::new(world2, world1, -normal, depth);
                        kinematic.set_point2(f1, Point::origin(), PolyhedralCone::Full);
                        kinematic.set_dilation2(ball.radius());
                    }

                    let local2 = m2.inverse_transform_point(&world2);
                    let n2 = cp2.normal_cone(f2);

                    match f2 {
                        FeatureId::Face { .. } => {
                            if !flip {
                                kinematic.set_plane2(f2, local2, n2.unwrap_half_line())
                            } else {
                                kinematic.set_plane1(f2, local2, n2.unwrap_half_line())
                            }
                        }
                        #[cfg(feature = "dim3")]
                        FeatureId::Edge { .. } => {
                            let edge = cp2.edge(f2);
                            let dir = Unit::new_normalize(edge.1 - edge.0);

                            if !flip {
                                kinematic.set_line2(f2, local2, dir, n2)
                            } else {
                                kinematic.set_line1(f2, local2, dir, n2)
                            }
                        }
                        FeatureId::Vertex { .. } => {
                            if !flip {
                                kinematic.set_point2(f2, local2, n2)
                            } else {
                                kinematic.set_point1(f2, local2, n2)
                            }
                        }
                        FeatureId::Unknown => panic!("Feature id cannot be unknown."),
                    }

                    let _ = self.contact_manifold.push(contact, kinematic, id_alloc);
                }
            } else {
                // FIXME: unhandled case where the ball center is exactly on the polyhedra surface.
            }

            true
        } else {
            false
        }
    }
}

impl<N: Real> ContactManifoldGenerator<N>
    for BallConvexPolyhedronManifoldGenerator<N>
{
    fn update(
        &mut self,
        _: &ContactDispatcher<N>,
        id1: usize,
        m1: &Isometry<N>,
        a: &Shape<N>,
        id2: usize,
        m2: &Isometry<N>,
        b: &Shape<N>,
        prediction: &ContactPrediction<N>,
        id_alloc: &mut IdAllocator,
    ) -> bool {
        self.contact_manifold.set_subshape_id1(id1);
        self.contact_manifold.set_subshape_id2(id2);

        if !self.flip {
            self.do_update(m1, a, m2, b, prediction, id_alloc, false)
        } else {
            self.do_update(m2, b, m1, a, prediction, id_alloc, true)
        }
    }

    #[inline]
    fn num_contacts(&self) -> usize {
        self.contact_manifold.len()
    }

    #[inline]
    fn contacts<'a: 'b, 'b>(&'a self, out: &'b mut Vec<&'a ContactManifold<N>>) {
        if self.contact_manifold.len() != 0 {
            out.push(&self.contact_manifold)
        }
    }
}