amethyst_nphysics 0.2.0

The NPhysics backend for the `amethyst_physics` crate.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use amethyst_core::ecs::Entity;
use amethyst_core::math::{one, zero, Isometry3, Point, Vector3};
use amethyst_physics::{objects::*, servers::*, PtReal};
use log::error;
use nphysics3d::{
    math::{Force, ForceType},
    object::{
        BodyPartHandle as NpBodyPartHandle, Collider as NpCollider, ColliderDesc as NpColliderDesc,
        RigidBodyDesc as NpRigidBodyDesc,
    },
};

use crate::{
    body::{Body, BodyData},
    conversors::*,
    servers_storage::*,
    shape::RigidShape,
    storage::StoreKey,
    utils::*,
};

pub struct RBodyNpServer<N: PtReal> {
    storages: ServersStorages<N>,
}

impl<N: PtReal> RBodyNpServer<N> {
    pub fn new(storages: ServersStorages<N>) -> Self {
        RBodyNpServer { storages }
    }
}

// This is a collection of function that can be used by other servers to perform some common
// operations on the body.
impl<N: PtReal> RBodyNpServer<N> {
    pub fn drop_body(
        body_tag: PhysicsRigidBodyTag,
        bodies_storage: &mut BodiesStorageWrite<'_, N>,
        colliders_storage: &mut CollidersStorageWrite<'_, N>,
        shapes_storage: &ShapesStorageRead<'_, N>,
    ) {
        let body_key = rigid_tag_to_store_key(body_tag);
        if let Some(mut body) = bodies_storage.get_body(body_key) {
            Self::remove_shape(&mut *body, shapes_storage, colliders_storage);
        }
        bodies_storage.drop_body(body_key);
    }

    /// Set shape.
    /// Take care to register the shape and set the collider to the body.
    pub fn install_shape(
        body: &mut Body<N>,
        shape: &mut RigidShape<N>,
        collider_desc: &NpColliderDesc<N>,
        colliders: &mut CollidersStorageWrite<'_, N>,
    ) {
        Self::install_collider(body, collider_desc, colliders);

        // Collider registration
        shape.register_body(body.self_key.unwrap());
        body.shape_key = shape.self_key;
    }

    /// Remove shape.
    /// Take care to unregister the shape and then drop the internal collider.
    pub fn remove_shape(
        body: &mut Body<N>,
        shapes: &ShapesStorageRead<'_, N>,
        colliders: &mut CollidersStorageWrite<'_, N>,
    ) {
        if let Some(shape_key) = body.shape_key {
            if let Some(mut shape) = shapes.get(shape_key) {
                shape.unregister_body(body.self_key.unwrap());
            } else {
                error!("A body is associated with a shape, but the shape doesn't exist!");
            }
            body.shape_key = None;
        }
        Self::drop_collider(body, colliders);
    }

    /// Set collider to the body
    pub fn install_collider(
        body: &mut Body<N>,
        collider_desc: &NpColliderDesc<N>,
        colliders: &mut CollidersStorageWrite<'_, N>,
    ) {
        let mut collider = collider_desc.build(NpBodyPartHandle(body.self_key.unwrap(), 0));

        RBodyNpServer::update_user_data(&mut collider, body);

        let key = colliders.insert_collider(collider);
        body.collider_key = Some(key);
    }

    /// Just drop the internal collider of the passed body.
    pub fn drop_collider(body: &mut Body<N>, colliders: &mut CollidersStorageWrite<'_, N>) {
        if let Some(collider_key) = body.collider_key {
            colliders.drop_collider(collider_key);
            body.collider_key = None;
        }
    }

    pub fn update_user_data(collider: &mut NpCollider<N, StoreKey>, body: &Body<N>) {
        collider.set_user_data(Some(Box::new(UserData::new(
            ObjectType::RigidBody,
            body.self_key.unwrap(),
            body.entity,
        ))));
    }

    pub fn create_collider_desc(body: &Body<N>, shape: &RigidShape<N>) -> NpColliderDesc<N> {
        let mut collider_desc = NpColliderDesc::new(shape.shape_handle().clone())
            .collision_groups(body.np_collision_groups)
            .material(body.material_handle.clone());
        if shape.is_concave() {
            collider_desc.set_density(zero());
        } else {
            collider_desc.set_density(one());
        }
        collider_desc
    }

    pub fn active_body(body_key: StoreKey, bodies: &BodiesStorageRead<'_, N>) {
        if let Some(mut body) = bodies.get_body(body_key) {
            body.activate();
        }
    }

    #[allow(clippy::collapsible_if)]
    pub fn update_contacts_watcher(
        body: &mut Body<N>,
        contacts_storage: &mut WatchContactsWrite<'_>,
    ) {
        if let BodyData::Rigid {
            contacts_to_report, ..
        } = body.body_data
        {
            let i = contacts_storage.binary_search(&body.self_key.unwrap());
            if let Result::Ok(i) = i {
                if contacts_to_report == 0 {
                    // Removes the entry on the contacts storage.
                    contacts_storage.remove(i);
                }
            } else {
                if contacts_to_report != 0 {
                    // Creates new entry on the contacts storage.
                    contacts_storage.push(body.self_key.unwrap());
                }
            }
        }
    }
}

// This is a collection of utility function to perform common operations.
impl<N: crate::PtReal> RBodyNpServer<N> {
    /// Update the collider collision group.
    pub fn update_collider_collision_groups(&self, body: &Body<N>) {
        // Update the collider collision groups.
        if let Some(key) = body.collider_key {
            let colliders = self.storages.colliders_r();
            let mut collider = colliders.get_collider(key).unwrap();
            collider.set_collision_groups(body.np_collision_groups);
        }
    }
}

impl<N> RBodyPhysicsServerTrait<N> for RBodyNpServer<N>
where
    N: PtReal,
{
    fn create(&self, body_desc: &RigidBodyDesc<N>) -> PhysicsHandle<PhysicsRigidBodyTag> {
        let mut bodies_storage = self.storages.bodies_w();

        // Create Rigid body
        let np_rigid_body = NpRigidBodyDesc::new()
            .set_status(body_mode_conversor::to_physics(body_desc.mode))
            .set_mass(body_desc.mass)
            .build();

        let cg =
            collision_group_conversor::to_nphysics(&body_desc.belong_to, &body_desc.collide_with);

        let b_key = bodies_storage.insert_body(Body::new_rigid_body(
            Box::new(np_rigid_body),
            body_desc.friction,
            body_desc.bounciness,
            cg,
            body_desc.contacts_to_report,
        ));

        // Initialize the body
        let mut body = bodies_storage.get_body(b_key).unwrap();
        body.self_key = Some(b_key);
        body.rigid_body_mut()
            .unwrap()
            .set_translations_kinematic(Vector3::new(
                body_desc.lock_translation_x,
                body_desc.lock_translation_y,
                body_desc.lock_translation_z,
            ));
        body.rigid_body_mut()
            .unwrap()
            .set_rotations_kinematic(Vector3::new(
                body_desc.lock_rotation_x,
                body_desc.lock_rotation_y,
                body_desc.lock_rotation_z,
            ));
        Self::update_contacts_watcher(&mut *body, &mut self.storages.watch_contacts_w());

        PhysicsHandle::new(store_key_to_rigid_tag(b_key), self.storages.gc.clone())
    }

    fn set_entity(&self, body_tag: PhysicsRigidBodyTag, entity: Option<Entity>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            fail_cond!(!matches!(body.body_data, BodyData::Rigid{..}));
            body.entity = entity;

            if let Some(collider_key) = body.collider_key {
                let colliders = self.storages.colliders_r();
                let collider = colliders.get_collider(collider_key);
                if let Some(mut collider) = collider {
                    RBodyNpServer::update_user_data(&mut *collider, &*body);
                } else {
                    error!("A body is assigned to a collider, but the collider doesn't exist!")
                }
            }
        }
    }

    fn entity(&self, body_tag: PhysicsRigidBodyTag) -> Option<Entity> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            body.entity
        } else {
            None
        }
    }

    fn set_shape(&self, body_tag: PhysicsRigidBodyTag, shape_tag: Option<PhysicsShapeTag>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let shape_key = shape_tag.map(shape_tag_to_store_key);

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            if body.shape_key == shape_key {
                return;
            }

            let mut colliders = self.storages.colliders_w();
            let shapes = self.storages.shapes_r();

            // Remove the old shape
            if let Some(_b_shape_key) = body.shape_key {
                RBodyNpServer::remove_shape(&mut *body, &shapes, &mut colliders);
            }

            // Assign the new shape if shape_tag is Some
            if let Some(shape_key) = shape_key {
                let shape = shapes.get(shape_key);
                if let Some(mut shape) = shape {
                    // Create and attach the collider
                    let collider_desc = RBodyNpServer::create_collider_desc(&body, &shape);

                    RBodyNpServer::install_shape(
                        &mut *body,
                        &mut *shape,
                        &collider_desc,
                        &mut colliders,
                    );
                } else {
                    error!("During the rigid body creation, was not possible to find the shape to assign");
                }
            }
        } else {
            error!("Body not found");
        }
    }

    fn shape(&self, body_tag: PhysicsRigidBodyTag) -> Option<PhysicsShapeTag> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            body.shape_key.map(store_key_to_shape_tag)
        } else {
            None
        }
    }

    fn set_transform(&self, body_tag: PhysicsRigidBodyTag, transf: &Isometry3<N>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.set_body_transform(transf);
        }
    }

    fn transform(&self, body_tag: PhysicsRigidBodyTag) -> Isometry3<N> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            *body.body_transform()
        } else {
            Isometry3::identity()
        }
    }

    fn set_mode(&self, body_tag: PhysicsRigidBodyTag, mode: BodyMode) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body
                .set_status(body_mode_conversor::to_physics(mode));
        }
    }

    fn mode(&self, body_tag: PhysicsRigidBodyTag) -> BodyMode {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            body_mode_conversor::from_physics(body.np_body.status())
        } else {
            error!("Rigid Body not foud");
            BodyMode::Disabled
        }
    }

    fn set_friction(&self, _body_tag: PhysicsRigidBodyTag, _friction: N) {
        unimplemented!("Make sure to have a sharable material instead");
    }

    fn friction(&self, _body_tag: PhysicsRigidBodyTag) -> N {
        unimplemented!();
    }

    fn set_bounciness(&self, _body_tag: PhysicsRigidBodyTag, _bounciness: N) {
        unimplemented!("Make sure to have a sharable material instead");
    }

    fn bounciness(&self, _body_tag: PhysicsRigidBodyTag) -> N {
        unimplemented!();
    }

    fn set_belong_to(&self, body_tag: PhysicsRigidBodyTag, groups: Vec<CollisionGroup>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            let (_, collide_with) =
                collision_group_conversor::from_nphysics(&body.np_collision_groups);
            body.np_collision_groups =
                collision_group_conversor::to_nphysics(&groups, &collide_with);
            self.update_collider_collision_groups(&body);
        }
    }

    fn belong_to(&self, body_tag: PhysicsRigidBodyTag) -> Vec<CollisionGroup> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            collision_group_conversor::from_nphysics(&body.np_collision_groups).0
        } else {
            Vec::new()
        }
    }

    fn set_collide_with(&self, body_tag: PhysicsRigidBodyTag, groups: Vec<CollisionGroup>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            let (belong_to, _) =
                collision_group_conversor::from_nphysics(&body.np_collision_groups);
            body.np_collision_groups = collision_group_conversor::to_nphysics(&belong_to, &groups);
            self.update_collider_collision_groups(&body);
        }
    }

    fn collide_with(&self, body_tag: PhysicsRigidBodyTag) -> Vec<CollisionGroup> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            collision_group_conversor::from_nphysics(&body.np_collision_groups).1
        } else {
            Vec::new()
        }
    }

    fn set_lock_translation(&self, body_tag: PhysicsRigidBodyTag, axis: Vector3<bool>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            if let Some(rb) = body.rigid_body_mut() {
                rb.set_translations_kinematic(axis);
            }
        }
    }

    fn lock_translation(&self, body_tag: PhysicsRigidBodyTag) -> Vector3<bool> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            if let Some(rb) = body.rigid_body() {
                return rb.kinematic_translations();
            }
        }
        Vector3::new(false, false, false)
    }

    fn set_lock_rotation(&self, body_tag: PhysicsRigidBodyTag, axis: Vector3<bool>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            if let Some(rb) = body.rigid_body_mut() {
                rb.set_rotations_kinematic(axis);
            }
        }
    }

    fn lock_rotation(&self, body_tag: PhysicsRigidBodyTag) -> Vector3<bool> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            if let Some(rb) = body.rigid_body() {
                return rb.kinematic_rotations();
            }
        }
        Vector3::new(false, false, false)
    }

    fn clear_forces(&self, body_tag: PhysicsRigidBodyTag) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body.clear_forces();
        }
    }

    fn apply_force(&self, body_tag: PhysicsRigidBodyTag, force: &Vector3<N>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body
                .apply_force(0, &Force::linear(*force), ForceType::Force, true);
        }
    }

    fn apply_torque(&self, body_tag: PhysicsRigidBodyTag, force: &Vector3<N>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body
                .apply_force(0, &Force::torque(*force), ForceType::Force, true);
        }
    }

    fn apply_force_at_position(
        &self,
        body_tag: PhysicsRigidBodyTag,
        force: &Vector3<N>,
        position: &Vector3<N>,
    ) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body.apply_force_at_local_point(
                0,
                force,
                &Point::from(*position),
                ForceType::Force,
                true,
            );
        }
    }

    fn apply_impulse(&self, body_tag: PhysicsRigidBodyTag, impulse: &Vector3<N>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body
                .apply_force(0, &Force::linear(*impulse), ForceType::Impulse, true);
        }
    }

    fn apply_angular_impulse(&self, body_tag: PhysicsRigidBodyTag, impulse: &Vector3<N>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body
                .apply_force(0, &Force::torque(*impulse), ForceType::Impulse, true);
        }
    }

    fn apply_impulse_at_position(
        &self,
        body_tag: PhysicsRigidBodyTag,
        impulse: &Vector3<N>,
        position: &Vector3<N>,
    ) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            body.np_body.apply_force_at_point(
                0,
                impulse,
                &Point::from(*position),
                ForceType::Impulse,
                true,
            );
        }
    }

    fn set_linear_velocity(&self, body_tag: PhysicsRigidBodyTag, velocity: &Vector3<N>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            if let Some(rb_body) = body.rigid_body_mut() {
                rb_body.set_linear_velocity(*velocity);
            } else {
                error!("The tag is not associated to any RigidBody");
            }
        }
    }

    fn linear_velocity(&self, body_tag: PhysicsRigidBodyTag) -> Vector3<N> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            if let Some(rb_body) = body.rigid_body() {
                return rb_body.velocity().linear;
            } else {
                error!("The tag is not associated to any RigidBody");
            }
        }
        Vector3::zeros()
    }

    fn set_angular_velocity(&self, body_tag: PhysicsRigidBodyTag, velocity: &Vector3<N>) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(mut body) = body {
            if let Some(rb_body) = body.rigid_body_mut() {
                rb_body.set_angular_velocity(*velocity);
            } else {
                error!("The tag is not associated to any RigidBody");
            }
        }
    }

    fn angular_velocity(&self, body_tag: PhysicsRigidBodyTag) -> Vector3<N> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            if let Some(rb_body) = body.rigid_body() {
                return rb_body.velocity().angular;
            } else {
                error!("The tag is not associated to any RigidBody");
            }
        }
        Vector3::zeros()
    }

    fn linear_velocity_at_position(
        &self,
        body_tag: PhysicsRigidBodyTag,
        position: &Vector3<N>,
    ) -> Vector3<N> {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let body = bodies.get_body(body_key);
        if let Some(body) = body {
            if let Some(rb_body) = body.rigid_body() {
                return rb_body.velocity().shift(&position).linear;
            } else {
                error!("The tag is not associated to any RigidBody");
            }
        }
        Vector3::zeros()
    }

    fn set_contacts_to_report(&self, body_tag: PhysicsRigidBodyTag, count: usize) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let b = bodies.get_body(body_key);
        if let Some(mut body) = b {
            if let BodyData::Rigid {
                contacts_to_report, ..
            } = &mut body.body_data
            {
                *contacts_to_report = count;
            }
            Self::update_contacts_watcher(&mut *body, &mut self.storages.watch_contacts_w());
        }
    }

    fn contacts_to_report(&self, body_tag: PhysicsRigidBodyTag) -> usize {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        let b = bodies.get_body(body_key);
        if let Some(body) = b {
            if let BodyData::Rigid {
                contacts_to_report, ..
            } = body.body_data
            {
                contacts_to_report
            } else {
                0
            }
        } else {
            0
        }
    }

    fn contact_events(
        &self,
        body_tag: PhysicsRigidBodyTag,
        out_contacts: &mut Vec<ContactEvent<N>>,
    ) {
        let body_key = rigid_tag_to_store_key(body_tag);
        let bodies = self.storages.bodies_r();

        if let Some(body) = bodies.get_body(body_key) {
            if let BodyData::Rigid { contacts, .. } = &body.body_data {
                out_contacts.resize_with(contacts.len(), ContactEvent::default);
                out_contacts.copy_from_slice(contacts.as_slice());
                return;
            }
        }
        out_contacts.clear();
    }
}