pub trait EventHandler: MaybeSync {
// Required methods
fn handle_collision_event(
&self,
bodies: &RigidBodySet,
colliders: &ColliderSet,
event: CollisionEvent,
contact_pair: Option<&ContactPair>,
);
fn handle_contact_force_event(
&self,
dt: f32,
bodies: &RigidBodySet,
colliders: &ColliderSet,
contact_pair: &ContactPair,
total_force_magnitude: f32,
);
fn handle_soft_body_tear_event(
&self,
soft_bodies: &SoftBodySet,
event: &SoftBodyTearEvent,
);
}Expand description
A callback interface for receiving physics events (collisions starting/stopping, contact forces, soft-body tears).
Implement this trait to get notified when:
- Two colliders start or stop touching (
handle_collision_event) - Contact forces exceed a threshold (
handle_contact_force_event) - A soft body tears (
handle_soft_body_tear_event)
§Common use cases
- Playing sound effects when objects collide
- Triggering game events (damage, pickups, checkpoints)
- Monitoring structural stress
- Detecting when specific objects touch
§Built-in implementation
Use ChannelEventCollector to collect events into channels for processing after the physics step.
§Example
struct MyEventHandler;
impl EventHandler for MyEventHandler {
fn handle_collision_event(
&self,
bodies: &RigidBodySet,
colliders: &ColliderSet,
event: CollisionEvent,
contact_pair: Option<&ContactPair>,
) {
match event {
CollisionEvent::Started(h1, h2, _) => {
println!("Collision started between {:?} and {:?}", h1, h2);
}
CollisionEvent::Stopped(h1, h2, _) => {
println!("Collision ended between {:?} and {:?}", h1, h2);
}
}
}
}Required Methods§
Sourcefn handle_collision_event(
&self,
bodies: &RigidBodySet,
colliders: &ColliderSet,
event: CollisionEvent,
contact_pair: Option<&ContactPair>,
)
fn handle_collision_event( &self, bodies: &RigidBodySet, colliders: &ColliderSet, event: CollisionEvent, contact_pair: Option<&ContactPair>, )
Called when two colliders start or stop touching each other.
Collision events are triggered when intersection state changes (Started/Stopped).
At least one collider must have ActiveEvents::COLLISION_EVENTS enabled.
§Parameters
event- EitherStarted(h1, h2, flags)orStopped(h1, h2, flags)bodies- All rigid bodies (to look up body info)colliders- All colliders (to look up collider info)contact_pair- Detailed contact info (Nonefor sensors, since they don’t compute contacts)
§Use cases
- Play collision sound effects
- Apply damage when objects hit
- Trigger game events (entering zones, picking up items)
- Track what’s touching what
Sourcefn handle_contact_force_event(
&self,
dt: f32,
bodies: &RigidBodySet,
colliders: &ColliderSet,
contact_pair: &ContactPair,
total_force_magnitude: f32,
)
fn handle_contact_force_event( &self, dt: f32, bodies: &RigidBodySet, colliders: &ColliderSet, contact_pair: &ContactPair, total_force_magnitude: f32, )
Called when contact forces exceed a threshold.
Triggered when the total force magnitude between two colliders exceeds the
Collider::contact_force_event_threshold.
At least one collider must have ActiveEvents::CONTACT_FORCE_EVENTS enabled.
§Use cases
- Detect hard impacts (for damage, breaking objects)
- Monitor structural stress
- Trigger effects at certain force levels (sparks, cracks)
§Parameters
total_force_magnitude- Sum of magnitudes of all contact forces (not vector sum!) Example: Two forces[0, 100, 0]and[0, -100, 0]→ magnitude = 200 (not 0)
Sourcefn handle_soft_body_tear_event(
&self,
soft_bodies: &SoftBodySet,
event: &SoftBodyTearEvent,
)
fn handle_soft_body_tear_event( &self, soft_bodies: &SoftBodySet, event: &SoftBodyTearEvent, )
Called at the end of a step for every soft body that tore during it (an element past its
tear threshold, or a SoftBody::tear_edge/tear_cell request), once the topology change is
applied (SoftBodyTearEvent); immediate SoftBodySet::tear/cut return theirs instead.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl EventHandler for ()
Available on crate feature alloc only.
impl EventHandler for ()
alloc only.fn handle_collision_event( &self, _bodies: &RigidBodySet, _colliders: &ColliderSet, _event: CollisionEvent, _contact_pair: Option<&ContactPair>, )
fn handle_contact_force_event( &self, _dt: f32, _bodies: &RigidBodySet, _colliders: &ColliderSet, _contact_pair: &ContactPair, _total_force_magnitude: f32, )
fn handle_soft_body_tear_event( &self, _soft_bodies: &SoftBodySet, _event: &SoftBodyTearEvent, )
Implementors§
impl EventHandler for ChannelEventCollector
std only.