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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use crate::math::{Real, Vect};
use crate::plugin::RapierContext;
use bevy::prelude::*;
use rapier::geometry::{Contact, ContactManifold, ContactPair, SolverContact, SolverFlags};
impl RapierContext {
pub fn contacts_with(&self, collider: Entity) -> impl Iterator<Item = ContactPairView> {
self.entity2collider
.get(&collider)
.into_iter()
.flat_map(|h| {
self.narrow_phase
.contacts_with(*h)
.map(|raw| ContactPairView { context: self, raw })
})
}
pub fn intersections_with(
&self,
collider: Entity,
) -> impl Iterator<Item = (Entity, Entity, bool)> + '_ {
self.entity2collider
.get(&collider)
.into_iter()
.flat_map(|h| {
self.narrow_phase
.intersections_with(*h)
.map(|(h1, h2, inter)| {
(
self.collider_entity(h1).unwrap(),
self.collider_entity(h2).unwrap(),
inter,
)
})
})
}
pub fn contact_pair(&self, collider1: Entity, collider2: Entity) -> Option<ContactPairView> {
let h1 = self.entity2collider.get(&collider1)?;
let h2 = self.entity2collider.get(&collider2)?;
self.narrow_phase
.contact_pair(*h1, *h2)
.map(|raw| ContactPairView { context: self, raw })
}
pub fn intersection_pair(&self, collider1: Entity, collider2: Entity) -> Option<bool> {
let h1 = self.entity2collider.get(&collider1)?;
let h2 = self.entity2collider.get(&collider2)?;
self.narrow_phase.intersection_pair(*h1, *h2)
}
pub fn contact_pairs(&self) -> impl Iterator<Item = ContactPairView> {
self.narrow_phase
.contact_pairs()
.map(|raw| ContactPairView { context: self, raw })
}
pub fn intersection_pairs(&self) -> impl Iterator<Item = (Entity, Entity, bool)> + '_ {
self.narrow_phase
.intersection_pairs()
.map(|(h1, h2, inter)| {
(
self.collider_entity(h1).unwrap(),
self.collider_entity(h2).unwrap(),
inter,
)
})
}
}
pub struct ContactManifoldView<'a> {
context: &'a RapierContext,
pub raw: &'a ContactManifold,
}
impl<'a> ContactManifoldView<'a> {
pub fn num_points(&self) -> usize {
self.raw.points.len()
}
pub fn point(&self, i: usize) -> Option<ContactView> {
self.raw.points.get(i).map(|raw| ContactView { raw })
}
pub fn points(&self) -> impl ExactSizeIterator<Item = ContactView> {
self.raw.points.iter().map(|raw| ContactView { raw })
}
pub fn local_n1(&self) -> Vect {
self.raw.local_n1.into()
}
pub fn local_n2(&self) -> Vect {
self.raw.local_n2.into()
}
pub fn subshape1(&self) -> u32 {
self.raw.subshape1
}
pub fn subshape2(&self) -> u32 {
self.raw.subshape2
}
pub fn rigid_body1(&self) -> Option<Entity> {
self.raw
.data
.rigid_body1
.and_then(|h| self.context.rigid_body_entity(h))
}
pub fn rigid_body2(&self) -> Option<Entity> {
self.raw
.data
.rigid_body2
.and_then(|h| self.context.rigid_body_entity(h))
}
pub fn solver_flags(&self) -> SolverFlags {
self.raw.data.solver_flags
}
pub fn normal(&self) -> Vect {
self.raw.data.normal.into()
}
pub fn num_solver_contacts(&self) -> usize {
self.raw.data.solver_contacts.len()
}
pub fn solver_contact(&self, i: usize) -> Option<SolverContactView> {
self.raw
.data
.solver_contacts
.get(i)
.map(|raw| SolverContactView { raw })
}
pub fn solver_contacts(&self) -> impl ExactSizeIterator<Item = SolverContactView> {
self.raw
.data
.solver_contacts
.iter()
.map(|raw| SolverContactView { raw })
}
pub fn relative_dominance(&self) -> i16 {
self.raw.data.relative_dominance
}
pub fn user_data(&self) -> u32 {
self.raw.data.user_data
}
}
impl<'a> ContactManifoldView<'a> {
pub fn find_deepest_contact(&self) -> Option<ContactView> {
self.raw
.find_deepest_contact()
.map(|raw| ContactView { raw })
}
}
pub struct ContactView<'a> {
pub raw: &'a Contact,
}
impl<'a> ContactView<'a> {
pub fn local_p1(&self) -> Vect {
self.raw.local_p1.into()
}
pub fn local_p2(&self) -> Vect {
self.raw.local_p2.into()
}
pub fn dist(&self) -> Real {
self.raw.dist
}
pub fn fid1(&self) -> u32 {
self.raw.fid1
}
pub fn fid2(&self) -> u32 {
self.raw.fid2
}
pub fn impulse(&self) -> Real {
self.raw.data.impulse
}
#[cfg(feature = "dim2")]
pub fn tangent_impulse(&self) -> Real {
self.raw.data.tangent_impulse
}
#[cfg(feature = "dim3")]
pub fn tangent_impulse(&self) -> [Real; 2] {
self.raw.data.tangent_impulse.into()
}
}
pub struct SolverContactView<'a> {
pub raw: &'a SolverContact,
}
impl<'a> SolverContactView<'a> {
pub fn point(&self) -> Vect {
self.raw.point.into()
}
pub fn dist(&self) -> Real {
self.raw.dist
}
pub fn friction(&self) -> Real {
self.raw.friction
}
pub fn restitution(&self) -> Real {
self.raw.restitution
}
pub fn tangent_velocity(&self) -> Vect {
self.raw.tangent_velocity.into()
}
pub fn is_new(&self) -> bool {
self.raw.is_new
}
}
pub struct ContactPairView<'a> {
context: &'a RapierContext,
pub raw: &'a ContactPair,
}
impl<'a> ContactPairView<'a> {
pub fn collider1(&self) -> Entity {
self.context.collider_entity(self.raw.collider1).unwrap()
}
pub fn collider2(&self) -> Entity {
self.context.collider_entity(self.raw.collider2).unwrap()
}
pub fn manifolds_len(&self) -> usize {
self.raw.manifolds.len()
}
pub fn manifold(&self, i: usize) -> Option<ContactManifoldView> {
self.raw.manifolds.get(i).map(|raw| ContactManifoldView {
context: self.context,
raw,
})
}
pub fn manifolds(&self) -> impl ExactSizeIterator<Item = ContactManifoldView> {
self.raw.manifolds.iter().map(|raw| ContactManifoldView {
context: self.context,
raw,
})
}
pub fn has_any_active_contacts(&self) -> bool {
self.raw.has_any_active_contact
}
pub fn find_deepest_contact(&self) -> Option<(ContactManifoldView, ContactView)> {
self.raw.find_deepest_contact().map(|(manifold, contact)| {
(
ContactManifoldView {
context: self.context,
raw: manifold,
},
ContactView { raw: contact },
)
})
}
}