Combine contact events and intersection events into a single event type and flags

This commit is contained in:
Sébastien Crozet
2022-03-19 17:52:56 +01:00
committed by Sébastien Crozet
parent a9e3441ecd
commit 063c638ec5
12 changed files with 101 additions and 103 deletions

View File

@@ -5,7 +5,7 @@ use crate::dynamics::{
RigidBodyCcd, RigidBodyHandle, RigidBodyMassProps, RigidBodyPosition, RigidBodyVelocity,
};
use crate::geometry::{
ColliderParent, ColliderPosition, ColliderShape, ColliderType, IntersectionEvent, NarrowPhase,
ColliderParent, ColliderPosition, ColliderShape, ColliderType, CollisionEvent, NarrowPhase,
};
use crate::math::Real;
use crate::parry::utils::SortedPair;
@@ -613,11 +613,11 @@ impl CCDSolver {
if !intersect_before
&& !intersect_after
&& (co_flags1.active_events | co_flags2.active_events)
.contains(ActiveEvents::INTERSECTION_EVENTS)
.contains(ActiveEvents::COLLISION_EVENTS)
{
// Emit one intersection-started and one intersection-stopped event.
events.handle_intersection_event(IntersectionEvent::new(toi.c1, toi.c2, true));
events.handle_intersection_event(IntersectionEvent::new(toi.c1, toi.c2, false));
events.handle_intersection_event(CollisionEvent::Started(toi.c1, toi.c2));
events.handle_intersection_event(CollisionEvent::Stopped(toi.c1, toi.c2));
}
}

View File

@@ -52,38 +52,44 @@ pub type TOI = parry::query::TOI;
pub use parry::shape::SharedShape;
#[derive(Copy, Clone, Hash, Debug)]
/// Events occurring when two collision objects start or stop being in contact (or penetration).
pub enum ContactEvent {
/// Event occurring when two collision objects start being in contact.
///
/// This event is generated whenever the narrow-phase finds a contact between two collision objects that did not have any contact at the last update.
/// Events occurring when two colliders start or stop being in contact (or intersecting)
pub enum CollisionEvent {
/// Event occurring when two colliders start being in contact (or intersecting)
Started(ColliderHandle, ColliderHandle),
/// Event occurring when two collision objects stop being in contact.
///
/// This event is generated whenever the narrow-phase fails to find any contact between two collision objects that did have at least one contact at the last update.
/// Event occurring when two colliders stop being in contact (or intersecting)
Stopped(ColliderHandle, ColliderHandle),
}
#[derive(Copy, Clone, Debug)]
/// Events occurring when two collision objects start or stop being in close proximity, contact, or disjoint.
pub struct IntersectionEvent {
/// The first collider to which the proximity event applies.
pub collider1: ColliderHandle,
/// The second collider to which the proximity event applies.
pub collider2: ColliderHandle,
/// Are the two colliders intersecting?
pub intersecting: bool,
}
impl CollisionEvent {
pub(crate) fn new(h1: ColliderHandle, h2: ColliderHandle, start: bool) -> Self {
if start {
Self::Started(h1, h2)
} else {
Self::Stopped(h1, h2)
}
}
impl IntersectionEvent {
/// Instantiates a new proximity event.
///
/// Panics if `prev_status` is equal to `new_status`.
pub fn new(collider1: ColliderHandle, collider2: ColliderHandle, intersecting: bool) -> Self {
Self {
collider1,
collider2,
intersecting,
/// Is this a `Started` collision event?
pub fn started(self) -> bool {
matches!(self, CollisionEvent::Started(_, _))
}
/// Is this a `Stopped` collision event?
pub fn stopped(self) -> bool {
matches!(self, CollisionEvent::Stopped(_, _))
}
/// 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,
}
}
/// 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,
}
}
}

View File

@@ -8,9 +8,9 @@ use crate::dynamics::{
};
use crate::geometry::{
BroadPhasePairEvent, ColliderChanges, ColliderGraphIndex, ColliderHandle, ColliderMaterial,
ColliderPair, ColliderParent, ColliderPosition, ColliderShape, ColliderType, ContactData,
ContactEvent, ContactManifold, ContactManifoldData, ContactPair, InteractionGraph,
IntersectionEvent, SolverContact, SolverFlags,
ColliderPair, ColliderParent, ColliderPosition, ColliderShape, ColliderType, CollisionEvent,
ContactData, ContactManifold, ContactManifoldData, ContactPair, InteractionGraph,
SolverContact, SolverFlags,
};
use crate::math::{Real, Vector};
use crate::pipeline::{
@@ -516,10 +516,10 @@ impl NarrowPhase {
let co_flag2: &ColliderFlags = colliders.index(pair.collider2.0);
if (co_flag1.active_events | co_flag2.active_events)
.contains(ActiveEvents::INTERSECTION_EVENTS)
.contains(ActiveEvents::COLLISION_EVENTS)
{
let prox_event =
IntersectionEvent::new(pair.collider1, pair.collider2, false);
CollisionEvent::Stopped(pair.collider1, pair.collider2);
events.handle_intersection_event(prox_event)
}
}
@@ -551,10 +551,10 @@ impl NarrowPhase {
let co_flag2: &ColliderFlags = colliders.index(pair.collider2.0);
if (co_flag1.active_events | co_flag2.active_events)
.contains(ActiveEvents::CONTACT_EVENTS)
.contains(ActiveEvents::COLLISION_EVENTS)
{
events.handle_contact_event(
ContactEvent::Stopped(pair.collider1, pair.collider2),
CollisionEvent::Stopped(pair.collider1, pair.collider2),
&ctct,
)
}
@@ -795,10 +795,10 @@ impl NarrowPhase {
let co_flags2: &ColliderFlags = colliders.index(handle2.0);
let active_events = co_flags1.active_events | co_flags2.active_events;
if active_events.contains(ActiveEvents::INTERSECTION_EVENTS)
if active_events.contains(ActiveEvents::COLLISION_EVENTS)
&& had_intersection != edge.weight
{
events.handle_intersection_event(IntersectionEvent::new(
events.handle_intersection_event(CollisionEvent::new(
handle1,
handle2,
edge.weight,
@@ -1027,15 +1027,15 @@ impl NarrowPhase {
let active_events = co_flags1.active_events | co_flags2.active_events;
if pair.has_any_active_contact != had_any_active_contact {
if active_events.contains(ActiveEvents::CONTACT_EVENTS) {
if active_events.contains(ActiveEvents::COLLISION_EVENTS) {
if pair.has_any_active_contact {
events.handle_contact_event(
ContactEvent::Started(pair.collider1, pair.collider2),
CollisionEvent::Started(pair.collider1, pair.collider2),
pair,
);
} else {
events.handle_contact_event(
ContactEvent::Stopped(pair.collider1, pair.collider2),
CollisionEvent::Stopped(pair.collider1, pair.collider2),
pair,
);
}

View File

@@ -1,14 +1,13 @@
use crate::geometry::{ContactEvent, ContactPair, IntersectionEvent};
use crate::geometry::{CollisionEvent, ContactPair};
use crossbeam::channel::Sender;
bitflags::bitflags! {
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
/// Flags affecting the events generated for this collider.
pub struct ActiveEvents: u32 {
/// If set, Rapier will call `EventHandler::handle_intersection_event` whenever relevant for this collider.
const INTERSECTION_EVENTS = 0b0001;
/// If set, Rapier will call `PhysicsHooks::handle_contact_event` whenever relevant for this collider.
const CONTACT_EVENTS = 0b0010;
/// If set, Rapier will call `EventHandler::handle_intersection_event` and
/// `EventHandler::handle_contact_event` whenever relevant for this collider.
const COLLISION_EVENTS = 0b0001;
}
}
@@ -22,47 +21,40 @@ impl Default for ActiveEvents {
///
/// Implementors of this trait will typically collect these events for future processing.
pub trait EventHandler: Send + Sync {
/// Handle an intersection event.
/// Handle a collision event.
///
/// A intersection event is emitted when the state of intersection between two colliders changes.
fn handle_intersection_event(&self, event: IntersectionEvent);
fn handle_intersection_event(&self, event: CollisionEvent);
/// Handle a contact event.
///
/// A contact event is emitted when two collider start or stop touching, independently from the
/// number of contact points involved.
fn handle_contact_event(&self, event: ContactEvent, contact_pair: &ContactPair);
fn handle_contact_event(&self, event: CollisionEvent, contact_pair: &ContactPair);
}
impl EventHandler for () {
fn handle_intersection_event(&self, _event: IntersectionEvent) {}
fn handle_contact_event(&self, _event: ContactEvent, _contact_pair: &ContactPair) {}
fn handle_intersection_event(&self, _event: CollisionEvent) {}
fn handle_contact_event(&self, _event: CollisionEvent, _contact_pair: &ContactPair) {}
}
/// A physics event handler that collects events into a crossbeam channel.
/// A collision event handler that collects events into a crossbeam channel.
pub struct ChannelEventCollector {
intersection_event_sender: Sender<IntersectionEvent>,
contact_event_sender: Sender<ContactEvent>,
event_sender: Sender<CollisionEvent>,
}
impl ChannelEventCollector {
/// Initialize a new physics event handler from crossbeam channel senders.
pub fn new(
intersection_event_sender: Sender<IntersectionEvent>,
contact_event_sender: Sender<ContactEvent>,
) -> Self {
Self {
intersection_event_sender,
contact_event_sender,
}
/// Initialize a new collision event handler from crossbeam channel senders.
pub fn new(event_sender: Sender<CollisionEvent>) -> Self {
Self { event_sender }
}
}
impl EventHandler for ChannelEventCollector {
fn handle_intersection_event(&self, event: IntersectionEvent) {
let _ = self.intersection_event_sender.send(event);
fn handle_intersection_event(&self, event: CollisionEvent) {
let _ = self.event_sender.send(event);
}
fn handle_contact_event(&self, event: ContactEvent, _: &ContactPair) {
let _ = self.contact_event_sender.send(event);
fn handle_contact_event(&self, event: CollisionEvent, _: &ContactPair) {
let _ = self.event_sender.send(event);
}
}