parry3d/query/contact_manifolds/
contact_manifolds_convex_ball.rs1use crate::math::{Pose, Real, Vector};
2use crate::query::contact_manifolds::{NormalConstraints, NormalConstraintsPair};
3use crate::query::{ContactManifold, Ray, TrackedContact};
4use crate::shape::{Ball, PackedFeatureId, Shape};
5
6pub fn contact_manifold_convex_ball_shapes<ManifoldData, ContactData>(
8 pos12: &Pose,
9 shape1: &dyn Shape,
10 shape2: &dyn Shape,
11 normal_constraints1: Option<&dyn NormalConstraints>,
12 normal_constraints2: Option<&dyn NormalConstraints>,
13 prediction: Real,
14 manifold: &mut ContactManifold<ManifoldData, ContactData>,
15) where
16 ContactData: Default + Copy,
17{
18 if let Some(ball1) = shape1.as_ball() {
19 contact_manifold_convex_ball(
20 &pos12.inverse(),
21 shape2,
22 ball1,
23 normal_constraints2,
24 normal_constraints1,
25 prediction,
26 manifold,
27 true,
28 );
29 } else if let Some(ball2) = shape2.as_ball() {
30 contact_manifold_convex_ball(
31 pos12,
32 shape1,
33 ball2,
34 normal_constraints1,
35 normal_constraints2,
36 prediction,
37 manifold,
38 false,
39 );
40 }
41}
42
43pub fn contact_manifold_convex_ball<'a, ManifoldData, ContactData, S1>(
45 pos12: &Pose,
46 shape1: &'a S1,
47 ball2: &'a Ball,
48 normal_constraints1: Option<&dyn NormalConstraints>,
49 normal_constraints2: Option<&dyn NormalConstraints>,
50 prediction: Real,
51 manifold: &mut ContactManifold<ManifoldData, ContactData>,
52 flipped: bool,
53) where
54 S1: ?Sized + Shape,
55 ContactData: Default + Copy,
56{
57 let local_p2_1 = pos12.translation;
58 let (proj, mut fid1) = shape1.project_local_point_and_get_feature(local_p2_1);
59 let mut local_p1 = proj.point;
60 let dpos = local_p2_1 - local_p1;
61
62 let mut dist = dpos.length();
70 let mut local_n1 = if dist == 0.0 {
71 pos12.translation.normalize_or(Vector::Y)
72 } else {
73 dpos / dist
74 };
75
76 if proj.is_inside {
77 local_n1 = -local_n1;
78 dist = -dist;
79 }
80
81 if dist <= ball2.radius + prediction {
82 let mut local_n2 = pos12.rotation.inverse() * -local_n1;
83 let uncorrected_local_n2 = local_n2;
84
85 if !(normal_constraints1, normal_constraints2).project_local_normals(
86 pos12,
87 &mut local_n1,
88 &mut local_n2,
89 ) {
90 manifold.clear();
92 return;
93 }
94
95 let local_p2 = local_n2 * ball2.radius;
96
97 if uncorrected_local_n2 != local_n2 {
99 let ray1 = Ray::new(
100 pos12.translation,
101 if proj.is_inside { local_n1 } else { -local_n1 },
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 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}