Skip to content

Commit 24947d1

Browse files
committed
added support for active collision events regardless if started was called
1 parent e9ea2ca commit 24947d1

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/geometry/contact_pair.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ impl IntersectionPair {
101101
None,
102102
);
103103
}
104+
105+
pub(crate) fn emit_active_event(
106+
&mut self,
107+
bodies: &RigidBodySet,
108+
colliders: &ColliderSet,
109+
collider1: ColliderHandle,
110+
collider2: ColliderHandle,
111+
events: &dyn EventHandler,
112+
) {
113+
events.handle_collision_event(
114+
bodies,
115+
colliders,
116+
CollisionEvent::Active(collider1, collider2, CollisionEventFlags::SENSOR),
117+
None,
118+
);
119+
}
104120
}
105121

106122
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
@@ -231,6 +247,20 @@ impl ContactPair {
231247
Some(self),
232248
);
233249
}
250+
251+
pub(crate) fn emit_active_event(
252+
&mut self,
253+
bodies: &RigidBodySet,
254+
colliders: &ColliderSet,
255+
events: &dyn EventHandler,
256+
) {
257+
events.handle_collision_event(
258+
bodies,
259+
colliders,
260+
CollisionEvent::Active(self.collider1, self.collider2, CollisionEventFlags::empty()),
261+
Some(self),
262+
);
263+
}
234264
}
235265

236266
#[derive(Clone, Debug)]

src/geometry/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ pub enum CollisionEvent {
7676
Started(ColliderHandle, ColliderHandle, CollisionEventFlags),
7777
/// Event occurring when two colliders stop colliding.
7878
Stopped(ColliderHandle, ColliderHandle, CollisionEventFlags),
79+
/// Event occurring when two colliders are actively colliding.
80+
/// Even after `CollisionEvent::Started` has already been called.
81+
Active(ColliderHandle, ColliderHandle, CollisionEventFlags),
7982
}
8083

8184
impl CollisionEvent {
@@ -92,21 +95,21 @@ impl CollisionEvent {
9295
/// The handle of the first collider involved in this collision event.
9396
pub fn collider1(self) -> ColliderHandle {
9497
match self {
95-
Self::Started(h, _, _) | Self::Stopped(h, _, _) => h,
98+
Self::Started(h, _, _) | Self::Stopped(h, _, _) | Self::Active(h, _, _) => h,
9699
}
97100
}
98101

99102
/// The handle of the second collider involved in this collision event.
100103
pub fn collider2(self) -> ColliderHandle {
101104
match self {
102-
Self::Started(_, h, _) | Self::Stopped(_, h, _) => h,
105+
Self::Started(_, h, _) | Self::Stopped(_, h, _) | Self::Active(_, h, _) => h,
103106
}
104107
}
105108

106109
/// Was at least one of the colliders involved in the collision a sensor?
107110
pub fn sensor(self) -> bool {
108111
match self {
109-
Self::Started(_, _, f) | Self::Stopped(_, _, f) => {
112+
Self::Started(_, _, f) | Self::Stopped(_, _, f) | Self::Active(_, _, f) => {
110113
f.contains(CollisionEventFlags::SENSOR)
111114
}
112115
}
@@ -115,7 +118,7 @@ impl CollisionEvent {
115118
/// Was at least one of the colliders involved in the collision removed?
116119
pub fn removed(self) -> bool {
117120
match self {
118-
Self::Started(_, _, f) | Self::Stopped(_, _, f) => {
121+
Self::Started(_, _, f) | Self::Stopped(_, _, f) | Self::Active(_, _, f) => {
119122
f.contains(CollisionEventFlags::REMOVED)
120123
}
121124
}

src/geometry/narrow_phase.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,11 @@ impl NarrowPhase {
762762

763763
let active_events = co1.flags.active_events | co2.flags.active_events;
764764

765+
if had_intersection && active_events.contains(ActiveEvents::ACTIVE_COLLISION_EVENTS) {
766+
edge.weight
767+
.emit_active_event(bodies, colliders, handle1, handle2, events);
768+
}
769+
765770
if active_events.contains(ActiveEvents::COLLISION_EVENTS)
766771
&& had_intersection != edge.weight.intersecting
767772
{
@@ -993,6 +998,12 @@ impl NarrowPhase {
993998

994999
let active_events = co1.flags.active_events | co2.flags.active_events;
9951000

1001+
if pair.has_any_active_contact
1002+
&& active_events.contains(ActiveEvents::ACTIVE_COLLISION_EVENTS)
1003+
{
1004+
pair.emit_active_event(bodies, colliders, events);
1005+
}
1006+
9961007
if pair.has_any_active_contact != had_any_active_contact {
9971008
if active_events.contains(ActiveEvents::COLLISION_EVENTS) {
9981009
if pair.has_any_active_contact {

src/pipeline/event_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ bitflags::bitflags! {
1313
/// If set, Rapier will call `EventHandler::handle_contact_force_event`
1414
/// whenever relevant for this collider.
1515
const CONTACT_FORCE_EVENTS = 0b0010;
16+
/// If set, Rapier will call `EventHandler::handle_collision_event`
17+
/// whenever relevant for this collider. Even after `CollisionEvent::Started`
18+
/// has already been called.
19+
const ACTIVE_COLLISION_EVENTS = 0b0100;
1620
}
1721
}
1822

0 commit comments

Comments
 (0)