Rework the event system
This commit is contained in:
@@ -6,9 +6,9 @@ use crate::dynamics::{
|
||||
RigidBodyIds, RigidBodyPosition, RigidBodyType, RigidBodyVelocity,
|
||||
};
|
||||
use crate::geometry::{
|
||||
BroadPhase, BroadPhasePairEvent, ColliderBroadPhaseData, ColliderChanges, ColliderGroups,
|
||||
ColliderHandle, ColliderMaterial, ColliderPair, ColliderParent, ColliderPosition,
|
||||
ColliderShape, ColliderType, NarrowPhase,
|
||||
BroadPhase, BroadPhasePairEvent, ColliderBroadPhaseData, ColliderChanges, ColliderFlags,
|
||||
ColliderGroups, ColliderHandle, ColliderMaterial, ColliderPair, ColliderParent,
|
||||
ColliderPosition, ColliderShape, ColliderType, NarrowPhase,
|
||||
};
|
||||
use crate::math::Real;
|
||||
use crate::pipeline::{EventHandler, PhysicsHooks};
|
||||
@@ -66,7 +66,8 @@ impl CollisionPipeline {
|
||||
+ ComponentSetOption<ColliderParent>
|
||||
+ ComponentSet<ColliderType>
|
||||
+ ComponentSet<ColliderGroups>
|
||||
+ ComponentSet<ColliderMaterial>,
|
||||
+ ComponentSet<ColliderMaterial>
|
||||
+ ComponentSet<ColliderFlags>,
|
||||
{
|
||||
// Update broad-phase.
|
||||
self.broad_phase_events.clear();
|
||||
@@ -173,7 +174,8 @@ impl CollisionPipeline {
|
||||
+ ComponentSetOption<ColliderParent>
|
||||
+ ComponentSet<ColliderType>
|
||||
+ ComponentSet<ColliderGroups>
|
||||
+ ComponentSet<ColliderMaterial>,
|
||||
+ ComponentSet<ColliderMaterial>
|
||||
+ ComponentSet<ColliderFlags>,
|
||||
{
|
||||
super::user_changes::handle_user_changes_to_colliders(
|
||||
bodies,
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
use crate::geometry::{ContactEvent, IntersectionEvent};
|
||||
use crate::geometry::{ContactEvent, ContactPair, IntersectionEvent};
|
||||
use crossbeam::channel::Sender;
|
||||
|
||||
bitflags::bitflags! {
|
||||
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
||||
/// Flags affecting the behavior of the constraints solver for a given contact manifold.
|
||||
pub struct ActiveEvents: u32 {
|
||||
/// If set, Rapier will call `PhysicsHooks::FILTER_CONTACT_PAIRS` whenever relevant.
|
||||
const INTERSECTION_EVENTS = 0b0001;
|
||||
/// If set, Rapier will call `PhysicsHooks::filter_intersection_pair` whenever relevant.
|
||||
const CONTACT_EVENTS = 0b0010;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ActiveEvents {
|
||||
fn default() -> Self {
|
||||
ActiveEvents::empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait implemented by structures responsible for handling events generated by the physics engine.
|
||||
///
|
||||
/// Implementors of this trait will typically collect these events for future processing.
|
||||
@@ -13,12 +30,12 @@ pub trait EventHandler: Send + Sync {
|
||||
///
|
||||
/// 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);
|
||||
fn handle_contact_event(&self, event: ContactEvent, contact_pair: &ContactPair);
|
||||
}
|
||||
|
||||
impl EventHandler for () {
|
||||
fn handle_intersection_event(&self, _event: IntersectionEvent) {}
|
||||
fn handle_contact_event(&self, _event: ContactEvent) {}
|
||||
fn handle_contact_event(&self, _event: ContactEvent, _contact_pair: &ContactPair) {}
|
||||
}
|
||||
|
||||
/// A physics event handler that collects events into a crossbeam channel.
|
||||
@@ -45,7 +62,7 @@ impl EventHandler for ChannelEventCollector {
|
||||
let _ = self.intersection_event_sender.send(event);
|
||||
}
|
||||
|
||||
fn handle_contact_event(&self, event: ContactEvent) {
|
||||
fn handle_contact_event(&self, event: ContactEvent, _: &ContactPair) {
|
||||
let _ = self.contact_event_sender.send(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
//! Structure for combining the various physics components to perform an actual simulation.
|
||||
|
||||
pub use collision_pipeline::CollisionPipeline;
|
||||
pub use event_handler::{ChannelEventCollector, EventHandler};
|
||||
pub use physics_hooks::{
|
||||
ContactModificationContext, PairFilterContext, PhysicsHooks, PhysicsHooksFlags,
|
||||
};
|
||||
pub use event_handler::{ActiveEvents, ChannelEventCollector, EventHandler};
|
||||
pub use physics_hooks::{ActiveHooks, ContactModificationContext, PairFilterContext, PhysicsHooks};
|
||||
pub use physics_pipeline::PhysicsPipeline;
|
||||
pub use query_pipeline::{QueryPipeline, QueryPipelineMode};
|
||||
|
||||
|
||||
@@ -118,24 +118,23 @@ impl<'a, Bodies, Colliders> ContactModificationContext<'a, Bodies, Colliders> {
|
||||
bitflags::bitflags! {
|
||||
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
||||
/// Flags affecting the behavior of the constraints solver for a given contact manifold.
|
||||
pub struct PhysicsHooksFlags: u32 {
|
||||
pub struct ActiveHooks: u32 {
|
||||
/// If set, Rapier will call `PhysicsHooks::filter_contact_pair` whenever relevant.
|
||||
const FILTER_CONTACT_PAIR = 0b0001;
|
||||
const FILTER_CONTACT_PAIRS = 0b0001;
|
||||
/// If set, Rapier will call `PhysicsHooks::filter_intersection_pair` whenever relevant.
|
||||
const FILTER_INTERSECTION_PAIR = 0b0010;
|
||||
/// If set, Rapier will call `PhysicsHooks::modify_solver_contact` whenever relevant.
|
||||
const MODIFY_SOLVER_CONTACTS = 0b0100;
|
||||
}
|
||||
}
|
||||
impl Default for PhysicsHooksFlags {
|
||||
impl Default for ActiveHooks {
|
||||
fn default() -> Self {
|
||||
PhysicsHooksFlags::empty()
|
||||
ActiveHooks::empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub trait PhysicsHooks<Bodies, Colliders> {
|
||||
fn active_hooks(&self) -> PhysicsHooksFlags;
|
||||
fn filter_contact_pair(
|
||||
&self,
|
||||
_context: &PairFilterContext<Bodies, Colliders>,
|
||||
@@ -155,7 +154,7 @@ pub trait PhysicsHooks<Bodies, Colliders>: Send + Sync {
|
||||
/// Applies the contact pair filter.
|
||||
///
|
||||
/// Note that this method will only be called if at least one of the colliders
|
||||
/// involved in the contact contains the `PhysicsHooksFlags::FILTER_CONTACT_PAIR` flags
|
||||
/// involved in the contact contains the `ActiveHooks::FILTER_CONTACT_PAIRS` flags
|
||||
/// in its physics hooks flags.
|
||||
///
|
||||
/// User-defined filter for potential contact pairs detected by the broad-phase.
|
||||
@@ -186,7 +185,7 @@ pub trait PhysicsHooks<Bodies, Colliders>: Send + Sync {
|
||||
/// Applies the intersection pair filter.
|
||||
///
|
||||
/// Note that this method will only be called if at least one of the colliders
|
||||
/// involved in the contact contains the `PhysicsHooksFlags::FILTER_INTERSECTION_PAIR` flags
|
||||
/// involved in the contact contains the `ActiveHooks::FILTER_INTERSECTION_PAIR` flags
|
||||
/// in its physics hooks flags.
|
||||
///
|
||||
/// User-defined filter for potential intersection pairs detected by the broad-phase.
|
||||
@@ -210,7 +209,7 @@ pub trait PhysicsHooks<Bodies, Colliders>: Send + Sync {
|
||||
/// Modifies the set of contacts seen by the constraints solver.
|
||||
///
|
||||
/// Note that this method will only be called if at least one of the colliders
|
||||
/// involved in the contact contains the `PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS` flags
|
||||
/// involved in the contact contains the `ActiveHooks::MODIFY_SOLVER_CONTACTS` flags
|
||||
/// in its physics hooks flags.
|
||||
///
|
||||
/// By default, the content of `solver_contacts` is computed from `manifold.points`.
|
||||
@@ -237,7 +236,10 @@ pub trait PhysicsHooks<Bodies, Colliders>: Send + Sync {
|
||||
}
|
||||
|
||||
impl<Bodies, Colliders> PhysicsHooks<Bodies, Colliders> for () {
|
||||
fn filter_contact_pair(&self, _: &PairFilterContext<Bodies, Colliders>) -> Option<SolverFlags> {
|
||||
fn filter_contact_pair(
|
||||
&self,
|
||||
_context: &PairFilterContext<Bodies, Colliders>,
|
||||
) -> Option<SolverFlags> {
|
||||
Some(SolverFlags::default())
|
||||
}
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ use crate::dynamics::{
|
||||
#[cfg(feature = "parallel")]
|
||||
use crate::dynamics::{JointGraphEdge, ParallelIslandSolver as IslandSolver};
|
||||
use crate::geometry::{
|
||||
BroadPhase, BroadPhasePairEvent, ColliderBroadPhaseData, ColliderChanges, ColliderGroups,
|
||||
ColliderHandle, ColliderMaterial, ColliderPair, ColliderParent, ColliderPosition,
|
||||
ColliderShape, ColliderType, ContactManifoldIndex, NarrowPhase,
|
||||
BroadPhase, BroadPhasePairEvent, ColliderBroadPhaseData, ColliderChanges, ColliderFlags,
|
||||
ColliderGroups, ColliderHandle, ColliderMaterial, ColliderPair, ColliderParent,
|
||||
ColliderPosition, ColliderShape, ColliderType, ContactManifoldIndex, NarrowPhase,
|
||||
};
|
||||
use crate::math::{Real, Vector};
|
||||
use crate::pipeline::{EventHandler, PhysicsHooks};
|
||||
@@ -104,7 +104,8 @@ impl PhysicsPipeline {
|
||||
+ ComponentSetOption<ColliderParent>
|
||||
+ ComponentSet<ColliderType>
|
||||
+ ComponentSet<ColliderGroups>
|
||||
+ ComponentSet<ColliderMaterial>,
|
||||
+ ComponentSet<ColliderMaterial>
|
||||
+ ComponentSet<ColliderFlags>,
|
||||
{
|
||||
self.counters.stages.collision_detection_time.resume();
|
||||
self.counters.cd.broad_phase_time.resume();
|
||||
@@ -365,7 +366,8 @@ impl PhysicsPipeline {
|
||||
Colliders: ComponentSetOption<ColliderParent>
|
||||
+ ComponentSet<ColliderPosition>
|
||||
+ ComponentSet<ColliderShape>
|
||||
+ ComponentSet<ColliderType>,
|
||||
+ ComponentSet<ColliderType>
|
||||
+ ComponentSet<ColliderFlags>,
|
||||
{
|
||||
self.counters.ccd.toi_computation_time.start();
|
||||
// Handle CCD
|
||||
@@ -520,7 +522,8 @@ impl PhysicsPipeline {
|
||||
+ ComponentSetOption<ColliderParent>
|
||||
+ ComponentSet<ColliderType>
|
||||
+ ComponentSet<ColliderGroups>
|
||||
+ ComponentSet<ColliderMaterial>,
|
||||
+ ComponentSet<ColliderMaterial>
|
||||
+ ComponentSet<ColliderFlags>,
|
||||
{
|
||||
self.counters.reset();
|
||||
self.counters.step_started();
|
||||
|
||||
Reference in New Issue
Block a user