parry3d/query/contact_manifolds/
contact_manifolds_convex_ball.rs

1use crate::math::{Isometry, Point, Real, Vector};
2use crate::query::contact_manifolds::{NormalConstraints, NormalConstraintsPair};
3use crate::query::{ContactManifold, Ray, TrackedContact};
4use crate::shape::{Ball, PackedFeatureId, Shape};
5use na::Unit;
6
7/// Computes the contact manifold between a convex shape and a ball, both represented as a `Shape` trait-object.
8pub fn contact_manifold_convex_ball_shapes<ManifoldData, ContactData>(
9    pos12: &Isometry<Real>,
10    shape1: &dyn Shape,
11    shape2: &dyn Shape,
12    normal_constraints1: Option<&dyn NormalConstraints>,
13    normal_constraints2: Option<&dyn NormalConstraints>,
14    prediction: Real,
15    manifold: &mut ContactManifold<ManifoldData, ContactData>,
16) where
17    ContactData: Default + Copy,
18{
19    if let Some(ball1) = shape1.as_ball() {
20        contact_manifold_convex_ball(
21            &pos12.inverse(),
22            shape2,
23            ball1,
24            normal_constraints2,
25            normal_constraints1,
26            prediction,
27            manifold,
28            true,
29        );
30    } else if let Some(ball2) = shape2.as_ball() {
31        contact_manifold_convex_ball(
32            pos12,
33            shape1,
34            ball2,
35            normal_constraints1,
36            normal_constraints2,
37            prediction,
38            manifold,
39            false,
40        );
41    }
42}
43
44/// Computes the contact manifold between a convex shape and a ball.
45pub fn contact_manifold_convex_ball<'a, ManifoldData, ContactData, S1>(
46    pos12: &Isometry<Real>,
47    shape1: &'a S1,
48    ball2: &'a Ball,
49    normal_constraints1: Option<&dyn NormalConstraints>,
50    normal_constraints2: Option<&dyn NormalConstraints>,
51    prediction: Real,
52    manifold: &mut ContactManifold<ManifoldData, ContactData>,
53    flipped: bool,
54) where
55    S1: ?Sized + Shape,
56    ContactData: Default + Copy,
57{
58    let local_p2_1 = Point::from(pos12.translation.vector);
59    let (proj, mut fid1) = shape1.project_local_point_and_get_feature(&local_p2_1);
60    let mut local_p1 = proj.point;
61    let dpos = local_p2_1 - local_p1;
62
63    // local_n1 points from the surface towards our origin if defined, otherwise from the other
64    // shape's origin towards our origin if defined, otherwise towards +x
65    let (mut local_n1, mut dist) = Unit::try_new_and_get(dpos, 0.0).unwrap_or_else(|| {
66        (
67            Unit::try_new(pos12.translation.vector, 0.0).unwrap_or_else(Vector::x_axis),
68            0.0,
69        )
70    });
71
72    if proj.is_inside {
73        local_n1 = -local_n1;
74        dist = -dist;
75    }
76
77    if dist <= ball2.radius + prediction {
78        let mut local_n2 = pos12.inverse_transform_vector(&-*local_n1);
79        let uncorrected_local_n2 = local_n2;
80
81        if !(normal_constraints1, normal_constraints2).project_local_normals(
82            pos12,
83            local_n1.as_mut_unchecked(),
84            &mut local_n2,
85        ) {
86            // The contact got completely discarded by the normal correction.
87            manifold.clear();
88            return;
89        }
90
91        let local_p2 = (local_n2 * ball2.radius).into();
92
93        // If a correction happened, adjust the contact point on the first body.
94        if uncorrected_local_n2 != local_n2 {
95            let ray1 = Ray::new(
96                pos12.translation.vector.into(),
97                if proj.is_inside {
98                    *local_n1
99                } else {
100                    -*local_n1
101                },
102            );
103
104            if let Some(hit) = shape1.cast_local_ray_and_get_normal(&ray1, Real::MAX, false) {
105                local_p1 = ray1.point_at(hit.time_of_impact);
106                dist = if proj.is_inside {
107                    -hit.time_of_impact
108                } else {
109                    hit.time_of_impact
110                };
111                fid1 = hit.feature;
112            } else {
113                manifold.clear();
114                return;
115            }
116        }
117
118        let contact_point = TrackedContact::flipped(
119            local_p1,
120            local_p2,
121            fid1.into(),
122            PackedFeatureId::face(0),
123            dist - ball2.radius,
124            flipped,
125        );
126
127        if manifold.points.len() != 1 {
128            manifold.clear();
129            manifold.points.push(contact_point);
130        } else {
131            // Copy only the geometry so we keep the warmstart impulses.
132            manifold.points[0].copy_geometry_from(contact_point);
133        }
134
135        if flipped {
136            manifold.local_n1 = local_n2;
137            manifold.local_n2 = *local_n1;
138        } else {
139            manifold.local_n1 = *local_n1;
140            manifold.local_n2 = local_n2;
141        }
142    } else {
143        manifold.clear();
144    }
145}