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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::mem;
use std::vec::IntoIter;
use math::{Point, Isometry};
use utils::data::uid_remap::{UidRemap, FastKey};
use utils::data::vec_map::Values;
use geometry::bounding_volume::{self, BoundingVolume, AABB};
use geometry::shape::ShapeHandle;
use geometry::query::{RayCast, Ray, RayIntersection, PointQuery};
use narrow_phase::{NarrowPhase, DefaultNarrowPhase, DefaultContactDispatcher, DefaultProximityDispatcher,
ContactHandler, ContactPairs, Contacts, ContactSignal, ProximityHandler,
ProximitySignal, ProximityPairs};
use broad_phase::{BroadPhase, DBVTBroadPhase, BroadPhasePairFilter, BroadPhasePairFilters};
use world::{CollisionObject, GeometricQueryType, CollisionGroups, CollisionGroupsPairFilter};
pub type NarrowPhaseObject<P, M, T> = Box<NarrowPhase<P, M, T> + 'static>;
pub type BroadPhaseObject<P> = Box<BroadPhase<P, AABB<P>, FastKey> + 'static>;
pub struct CollisionWorld<P: Point, M, T> {
objects: UidRemap<CollisionObject<P, M, T>>,
broad_phase: BroadPhaseObject<P>,
narrow_phase: Box<NarrowPhase<P, M, T>>,
contact_signal: ContactSignal<P, M, T>,
proximity_signal: ProximitySignal<P, M, T>,
pair_filters: BroadPhasePairFilters<P, M, T>,
pos_to_update: Vec<(FastKey, M)>,
objects_to_remove: Vec<usize>,
objects_to_add: Vec<CollisionObject<P, M, T>>,
timestamp: usize
}
impl<P: Point, M: Isometry<P>, T> CollisionWorld<P, M, T> {
pub fn new(margin: P::Real, small_uids: bool) -> CollisionWorld<P, M, T> {
let objects = UidRemap::new(small_uids);
let coll_dispatcher = Box::new(DefaultContactDispatcher::new());
let prox_dispatcher = Box::new(DefaultProximityDispatcher::new());
let broad_phase = Box::new(DBVTBroadPhase::<P, AABB<P>, FastKey>::new(margin, true));
let narrow_phase = DefaultNarrowPhase::new(coll_dispatcher, prox_dispatcher);
let contact_signal = ContactSignal::new();
let proximity_signal = ProximitySignal::new();
CollisionWorld {
contact_signal: contact_signal,
proximity_signal: proximity_signal,
objects: objects,
broad_phase: broad_phase,
narrow_phase: Box::new(narrow_phase),
pair_filters: BroadPhasePairFilters::new(),
pos_to_update: Vec::new(),
objects_to_remove: Vec::new(),
objects_to_add: Vec::new(),
timestamp: 0
}
}
pub fn deferred_add(&mut self,
uid: usize,
position: M,
shape: ShapeHandle<P, M>,
collision_groups: CollisionGroups,
query_type: GeometricQueryType<P::Real>,
data: T) {
assert!(!self.objects.contains_key(uid), "Unable to add a collision object with the same uid twice.");
let mut collision_object = CollisionObject::new(uid, position, shape, collision_groups, query_type, data);
collision_object.timestamp = self.timestamp;
self.objects_to_add.push((collision_object));
}
pub fn update(&mut self) {
self.perform_position_update();
self.perform_additions_removals_and_broad_phase();
self.perform_narrow_phase();
}
pub fn deferred_remove(&mut self, uid: usize) {
if self.objects.contains_key(uid) {
self.objects_to_remove.push(uid);
}
else {
panic!("Attempting to remove an unknown object. \
Did you forgot to call `.update()` after `.deferred_add()`-ing your objects?");
}
}
pub fn deferred_set_position(&mut self, uid: usize, pos: M) {
if let Some(fk) = self.objects.get_fast_key(uid) {
self.pos_to_update.push((fk, pos))
}
else {
panic!("Attempting to set the position of an unknown object. \
Did you forgot to call `.update()` after `.deferred_add()`-ing your objects?");
}
}
pub fn register_broad_phase_pair_filter<F>(&mut self, name: &str, filter: F)
where F: BroadPhasePairFilter<P, M, T> + 'static {
self.pair_filters.register_collision_filter(name, Box::new(filter));
self.broad_phase.deferred_recompute_all_proximities();
}
pub fn unregister_broad_phase_pair_filter(&mut self, name: &str) {
if self.pair_filters.unregister_collision_filter(name) {
self.broad_phase.deferred_recompute_all_proximities();
}
}
pub fn register_contact_handler<H>(&mut self, name: &str, handler: H)
where H: ContactHandler<P, M, T> + 'static {
self.contact_signal.register_contact_handler(name, Box::new(handler));
}
pub fn unregister_contact_handler(&mut self, name: &str) {
self.contact_signal.unregister_contact_handler(name);
}
pub fn register_proximity_handler<H>(&mut self, name: &str, handler: H)
where H: ProximityHandler<P, M, T> + 'static {
self.proximity_signal.register_proximity_handler(name, Box::new(handler));
}
pub fn unregister_proximity_handler(&mut self, name: &str) {
self.proximity_signal.unregister_proximity_handler(name);
}
pub fn perform_position_update(&mut self) {
for &(ref fk, ref pos) in self.pos_to_update.iter() {
if let Some(co) = self.objects.get_fast_mut(fk) {
co.position = pos.clone();
co.timestamp = self.timestamp;
let mut aabb = bounding_volume::aabb(co.shape.as_ref(), pos);
aabb.loosen(co.query_type.query_limit());
self.broad_phase.deferred_set_bounding_volume(fk.uid(), aabb);
}
}
self.pos_to_update.clear();
}
pub fn perform_additions_removals_and_broad_phase(&mut self) {
for co in self.objects_to_add.drain(..) {
let mut aabb = bounding_volume::aabb(co.shape.as_ref(), &co.position);
aabb.loosen(co.query_type.query_limit());
let fk = self.objects.insert(co.uid, co).0;
self.broad_phase.deferred_add(fk.uid(), aabb, fk)
}
self.objects_to_add.shrink_to_fit();
for uid in self.objects_to_remove.iter() {
if let Some(fk) = self.objects.get_fast_key(*uid) {
self.broad_phase.deferred_remove(fk.uid());
}
}
self.perform_broad_phase();
while let Some(uid) = self.objects_to_remove.pop() {
let _ = self.objects.remove(uid);
}
}
pub fn perform_broad_phase(&mut self) {
let bf = &mut self.broad_phase;
let nf = &mut self.narrow_phase;
let sig = &mut self.contact_signal;
let prox = &mut self.proximity_signal;
let filts = &self.pair_filters;
let objs = &self.objects;
bf.update(
&mut |b1, b2| CollisionWorld::filter_collision(filts, objs, b1, b2),
&mut |b1, b2, started| nf.handle_interaction(sig, prox, objs, b1, b2, started));
}
pub fn perform_narrow_phase(&mut self) {
self.narrow_phase.update(
&self.objects,
&mut self.contact_signal,
&mut self.proximity_signal,
self.timestamp);
self.timestamp = self.timestamp + 1;
}
pub fn set_narrow_phase(&mut self, narrow_phase: Box<NarrowPhase<P, M, T>>) -> Box<NarrowPhase<P, M, T>> {
let old = mem::replace(&mut self.narrow_phase, narrow_phase);
self.broad_phase.deferred_recompute_all_proximities();
old
}
#[inline]
pub fn contact_pairs(&self) -> ContactPairs<P, M, T> {
self.narrow_phase.contact_pairs(&self.objects)
}
#[inline]
pub fn proximity_pairs(&self) -> ProximityPairs<P, M, T> {
self.narrow_phase.proximity_pairs(&self.objects)
}
#[inline]
pub fn contacts(&self) -> Contacts<P, M, T> {
self.narrow_phase.contact_pairs(&self.objects).contacts()
}
#[inline]
pub fn collision_objects(&self) -> Values<CollisionObject<P, M, T>> {
self.objects.values()
}
#[inline]
pub fn collision_object(&self, uid: usize) -> Option<&CollisionObject<P, M, T>> {
self.objects.get(uid)
}
#[inline]
pub fn interferences_with_ray<'a>(&'a self, ray: &'a Ray<P>, groups: &'a CollisionGroups)
-> InterferencesWithRay<'a, P, M, T> {
let mut fks = Vec::new();
self.broad_phase.interferences_with_ray(ray, &mut fks);
InterferencesWithRay {
ray: ray,
groups: groups,
objects: &self.objects,
idx: fks.into_iter()
}
}
#[inline]
pub fn interferences_with_point<'a>(&'a self, point: &'a P, groups: &'a CollisionGroups)
-> InterferencesWithPoint<'a, P, M, T> {
let mut fks = Vec::new();
self.broad_phase.interferences_with_point(point, &mut fks);
InterferencesWithPoint {
point: point,
groups: groups,
objects: &self.objects,
idx: fks.into_iter()
}
}
#[inline]
pub fn interferences_with_aabb<'a>(&'a self, aabb: &'a AABB<P>, groups: &'a CollisionGroups)
-> InterferencesWithAABB<'a, P, M, T> {
let mut fks = Vec::new();
self.broad_phase.interferences_with_bounding_volume(aabb, &mut fks);
InterferencesWithAABB {
groups: groups,
objects: &self.objects,
idx: fks.into_iter()
}
}
#[inline]
fn filter_collision(filters: &BroadPhasePairFilters<P, M, T>,
objects: &UidRemap<CollisionObject<P, M, T>>,
fk1: &FastKey,
fk2: &FastKey) -> bool {
let o1 = &objects[*fk1];
let o2 = &objects[*fk2];
let filter_by_groups = CollisionGroupsPairFilter;
filter_by_groups.is_pair_valid(o1, o2) && filters.is_pair_valid(o1, o2)
}
}
pub struct InterferencesWithRay<'a, P: 'a + Point, M: 'a, T: 'a> {
ray: &'a Ray<P>,
objects: &'a UidRemap<CollisionObject<P, M, T>>,
groups: &'a CollisionGroups,
idx: IntoIter<&'a FastKey>,
}
impl<'a, P: Point, M: Isometry<P>, T> Iterator for InterferencesWithRay<'a, P, M, T> {
type Item = (&'a CollisionObject<P, M, T>, RayIntersection<P::Vector>);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some(id) = self.idx.next() {
let co = &self.objects[*id];
if co.collision_groups.can_interact_with_groups(self.groups) {
let inter = co.shape.toi_and_normal_with_ray(&co.position, self.ray, true);
if let Some(inter) = inter {
return Some((co, inter))
}
}
}
None
}
}
pub struct InterferencesWithPoint<'a, P: 'a + Point, M: 'a, T: 'a> {
point: &'a P,
objects: &'a UidRemap<CollisionObject<P, M, T>>,
groups: &'a CollisionGroups,
idx: IntoIter<&'a FastKey>,
}
impl<'a, P: Point, M: Isometry<P>, T> Iterator for InterferencesWithPoint<'a, P, M, T> {
type Item = &'a CollisionObject<P, M, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some(id) = self.idx.next() {
let co = &self.objects[*id];
if co.collision_groups.can_interact_with_groups(self.groups) &&
co.shape.contains_point(&co.position, self.point) {
return Some(co)
}
}
None
}
}
pub struct InterferencesWithAABB<'a, P: 'a + Point, M: 'a, T: 'a> {
objects: &'a UidRemap<CollisionObject<P, M, T>>,
groups: &'a CollisionGroups,
idx: IntoIter<&'a FastKey>,
}
impl<'a, P: Point, M, T> Iterator for InterferencesWithAABB<'a, P, M, T> {
type Item = &'a CollisionObject<P, M, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some(id) = self.idx.next() {
let co = &self.objects[*id];
if co.collision_groups.can_interact_with_groups(self.groups) {
return Some(co)
}
}
None
}
}