pfx_physics/rigid/
event.rs1use std::sync::Mutex;
2
3use rapier3d::prelude::{
4 ColliderHandle, ColliderSet, CollisionEvent, ContactPair, EventHandler, RigidBodySet,
5 SoftBodySet, SoftBodyTearEvent,
6};
7
8use super::ColliderId;
9
10#[derive(Clone, Copy, Debug, PartialEq)]
11pub enum Event {
12 ContactStarted {
13 a: ColliderId,
14 b: ColliderId,
15 impulse: f32,
16 },
17 ContactStopped {
18 a: ColliderId,
19 b: ColliderId,
20 },
21 ContactImpulse {
22 a: ColliderId,
23 b: ColliderId,
24 impulse: f32,
25 },
26 SensorEntered {
27 sensor: ColliderId,
28 other: ColliderId,
29 },
30 SensorExited {
31 sensor: ColliderId,
32 other: ColliderId,
33 },
34}
35
36#[derive(Default)]
37pub(crate) struct Collector {
38 pub(crate) collisions: Mutex<Vec<CollisionEvent>>,
39 pub(crate) impulses: Mutex<Vec<(ColliderHandle, ColliderHandle, f32)>>,
40}
41
42impl EventHandler for Collector {
43 fn handle_collision_event(
44 &self,
45 _bodies: &RigidBodySet,
46 _colliders: &ColliderSet,
47 event: CollisionEvent,
48 _pair: Option<&ContactPair>,
49 ) {
50 if let Ok(mut events) = self.collisions.lock() {
51 events.push(event);
52 }
53 }
54
55 fn handle_contact_force_event(
56 &self,
57 dt: f32,
58 _bodies: &RigidBodySet,
59 _colliders: &ColliderSet,
60 pair: &ContactPair,
61 total_force_magnitude: f32,
62 ) {
63 if let Ok(mut impulses) = self.impulses.lock() {
64 impulses.push((pair.collider1, pair.collider2, total_force_magnitude * dt));
65 }
66 }
67
68 fn handle_soft_body_tear_event(&self, _soft_bodies: &SoftBodySet, _event: &SoftBodyTearEvent) {}
69}