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
use crate::math::{Real, Vect};
use bevy::prelude::{Entity, EventWriter};
use rapier::dynamics::RigidBodySet;
use rapier::geometry::{
ColliderHandle, ColliderSet, CollisionEvent as RapierCollisionEvent, CollisionEventFlags,
ContactForceEvent as RapierContactForceEvent, ContactPair,
};
use rapier::pipeline::EventHandler;
use std::collections::HashMap;
use std::sync::RwLock;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum CollisionEvent {
Started(Entity, Entity, CollisionEventFlags),
Stopped(Entity, Entity, CollisionEventFlags),
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ContactForceEvent {
pub collider1: Entity,
pub collider2: Entity,
pub total_force: Vect,
pub total_force_magnitude: Real,
pub max_force_direction: Vect,
pub max_force_magnitude: Real,
}
pub(crate) struct EventQueue<'a> {
pub deleted_colliders: &'a HashMap<ColliderHandle, Entity>,
pub collision_events: RwLock<EventWriter<'a, 'a, CollisionEvent>>,
pub contact_force_events: RwLock<EventWriter<'a, 'a, ContactForceEvent>>,
}
impl<'a> EventQueue<'a> {
fn collider2entity(&self, colliders: &ColliderSet, handle: ColliderHandle) -> Entity {
colliders
.get(handle)
.map(|co| Entity::from_bits(co.user_data as u64))
.or_else(|| self.deleted_colliders.get(&handle).copied())
.expect("Internal error: entity not found for collision event.")
}
}
impl<'a> EventHandler for EventQueue<'a> {
fn handle_collision_event(
&self,
_bodies: &RigidBodySet,
colliders: &ColliderSet,
event: RapierCollisionEvent,
_: Option<&ContactPair>,
) {
let event = match event {
RapierCollisionEvent::Started(h1, h2, flags) => {
let e1 = self.collider2entity(colliders, h1);
let e2 = self.collider2entity(colliders, h2);
CollisionEvent::Started(e1, e2, flags)
}
RapierCollisionEvent::Stopped(h1, h2, flags) => {
let e1 = self.collider2entity(colliders, h1);
let e2 = self.collider2entity(colliders, h2);
CollisionEvent::Stopped(e1, e2, flags)
}
};
if let Ok(mut events) = self.collision_events.write() {
events.send(event)
}
}
fn handle_contact_force_event(
&self,
dt: Real,
_bodies: &RigidBodySet,
colliders: &ColliderSet,
contact_pair: &ContactPair,
total_force_magnitude: Real,
) {
let rapier_event =
RapierContactForceEvent::from_contact_pair(dt, contact_pair, total_force_magnitude);
let event = ContactForceEvent {
collider1: self.collider2entity(colliders, rapier_event.collider1),
collider2: self.collider2entity(colliders, rapier_event.collider2),
total_force: rapier_event.total_force.into(),
total_force_magnitude: rapier_event.total_force_magnitude,
max_force_direction: rapier_event.max_force_direction.into(),
max_force_magnitude: rapier_event.max_force_magnitude,
};
if let Ok(mut events) = self.contact_force_events.write() {
events.send(event);
}
}
}