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,
);
}Expand description
A callback interface for receiving physics events (collisions starting/stopping, contact forces).
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)
§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)
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, )
Implementors§
impl EventHandler for ChannelEventCollector
std only.