Skip to content

Commit

Permalink
Add convenience methods for determining if collisions started or stop…
Browse files Browse the repository at this point in the history
…ped. (#529)

# Objective

It can be tricky to match and handle all of an entity's collision states in a single event loop. I routinely find myself writing code like this:

```
    for Collision(contacts)in events.read() {
        let started = contacts.during_current_frame && !contacts.during_previous_frame;
        let stopped = !contacts.during_current_frame && contacts.during_previous_frame;
        if started {
        } else if stopped {
        }
```

## Solution

Add `Contacts::collision_started` and `Contacts::collision_stopped` to more conveniently check these conditions.

## Changelog

### Added

- Added `Contacts::collision_started` and `Contacts::collision_stopped` for more conveniently checking if a collision just started or stopped.
  • Loading branch information
ndarilek authored Oct 7, 2024
1 parent 39a7480 commit 4ea5373
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/collision/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ impl Contacts {
pub fn total_tangent_force(&self, delta_time: Scalar) -> Vector2 {
self.total_tangent_impulse / delta_time
}

/// Returns `true` if a collision started during the current frame.
pub fn collision_started(&self) -> bool {
!self.during_previous_frame && self.during_current_frame
}

/// Returns `true` if a collision stopped during the current frame.
pub fn collision_stopped(&self) -> bool {
self.during_previous_frame && !self.during_current_frame
}
}

/// A contact manifold between two colliders, containing a set of contact points.
Expand Down

0 comments on commit 4ea5373

Please sign in to comment.