Skip to content

Commit

Permalink
added support for active collision events regardless if started was c…
Browse files Browse the repository at this point in the history
…alled
  • Loading branch information
TCROC committed Nov 12, 2023
1 parent e9ea2ca commit 24947d1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
30 changes: 30 additions & 0 deletions src/geometry/contact_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ impl IntersectionPair {
None,
);
}

pub(crate) fn emit_active_event(
&mut self,
bodies: &RigidBodySet,
colliders: &ColliderSet,
collider1: ColliderHandle,
collider2: ColliderHandle,
events: &dyn EventHandler,
) {
events.handle_collision_event(
bodies,
colliders,
CollisionEvent::Active(collider1, collider2, CollisionEventFlags::SENSOR),
None,
);
}
}

#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -231,6 +247,20 @@ impl ContactPair {
Some(self),
);
}

pub(crate) fn emit_active_event(
&mut self,
bodies: &RigidBodySet,
colliders: &ColliderSet,
events: &dyn EventHandler,
) {
events.handle_collision_event(
bodies,
colliders,
CollisionEvent::Active(self.collider1, self.collider2, CollisionEventFlags::empty()),
Some(self),
);
}
}

#[derive(Clone, Debug)]
Expand Down
11 changes: 7 additions & 4 deletions src/geometry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub enum CollisionEvent {
Started(ColliderHandle, ColliderHandle, CollisionEventFlags),
/// Event occurring when two colliders stop colliding.
Stopped(ColliderHandle, ColliderHandle, CollisionEventFlags),
/// Event occurring when two colliders are actively colliding.
/// Even after `CollisionEvent::Started` has already been called.
Active(ColliderHandle, ColliderHandle, CollisionEventFlags),
}

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

/// The handle of the second collider involved in this collision event.
pub fn collider2(self) -> ColliderHandle {
match self {
Self::Started(_, h, _) | Self::Stopped(_, h, _) => h,
Self::Started(_, h, _) | Self::Stopped(_, h, _) | Self::Active(_, h, _) => h,
}
}

/// Was at least one of the colliders involved in the collision a sensor?
pub fn sensor(self) -> bool {
match self {
Self::Started(_, _, f) | Self::Stopped(_, _, f) => {
Self::Started(_, _, f) | Self::Stopped(_, _, f) | Self::Active(_, _, f) => {
f.contains(CollisionEventFlags::SENSOR)
}
}
Expand All @@ -115,7 +118,7 @@ impl CollisionEvent {
/// Was at least one of the colliders involved in the collision removed?
pub fn removed(self) -> bool {
match self {
Self::Started(_, _, f) | Self::Stopped(_, _, f) => {
Self::Started(_, _, f) | Self::Stopped(_, _, f) | Self::Active(_, _, f) => {
f.contains(CollisionEventFlags::REMOVED)
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/geometry/narrow_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,11 @@ impl NarrowPhase {

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

if had_intersection && active_events.contains(ActiveEvents::ACTIVE_COLLISION_EVENTS) {
edge.weight
.emit_active_event(bodies, colliders, handle1, handle2, events);
}

if active_events.contains(ActiveEvents::COLLISION_EVENTS)
&& had_intersection != edge.weight.intersecting
{
Expand Down Expand Up @@ -993,6 +998,12 @@ impl NarrowPhase {

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

if pair.has_any_active_contact
&& active_events.contains(ActiveEvents::ACTIVE_COLLISION_EVENTS)
{
pair.emit_active_event(bodies, colliders, events);
}

if pair.has_any_active_contact != had_any_active_contact {
if active_events.contains(ActiveEvents::COLLISION_EVENTS) {
if pair.has_any_active_contact {
Expand Down
4 changes: 4 additions & 0 deletions src/pipeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ bitflags::bitflags! {
/// If set, Rapier will call `EventHandler::handle_contact_force_event`
/// whenever relevant for this collider.
const CONTACT_FORCE_EVENTS = 0b0010;
/// If set, Rapier will call `EventHandler::handle_collision_event`
/// whenever relevant for this collider. Even after `CollisionEvent::Started`
/// has already been called.
const ACTIVE_COLLISION_EVENTS = 0b0100;
}
}

Expand Down

0 comments on commit 24947d1

Please sign in to comment.