Combine contact events and intersection events into a single event type and flags
This commit is contained in:
committed by
Sébastien Crozet
parent
a9e3441ecd
commit
063c638ec5
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user