Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for collision events regardless if started was called: CollisionEvent::Active #550

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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