Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find_deepest_contact for Contacts and ContactManifold #556

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/collision/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,23 @@ impl Contacts {
pub fn collision_stopped(&self) -> bool {
self.during_previous_frame && !self.during_current_frame
}

/// Returns the contact with the largest penetration depth.
///
/// If the objects are separated but there is still a speculative contact,
/// the penetration depth will be negative.
///
/// If there are no contacts, `None` is returned.
pub fn find_deepest_contact(&self) -> Option<&ContactData> {
self.manifolds
.iter()
.filter_map(|manifold| manifold.find_deepest_contact())
.max_by(|a, b| {
a.penetration
.partial_cmp(&b.penetration)
.unwrap_or(std::cmp::Ordering::Equal)
})
}
}

/// A contact manifold between two colliders, containing a set of contact points.
Expand Down Expand Up @@ -420,6 +437,20 @@ impl ContactManifold {
}
}
}

/// Returns the contact with the largest penetration depth.
///
/// If the objects are separated but there is still a speculative contact,
/// the penetration depth will be negative.
///
/// If there are no contacts, `None` is returned.
pub fn find_deepest_contact(&self) -> Option<&ContactData> {
self.contacts.iter().max_by(|a, b| {
a.penetration
.partial_cmp(&b.penetration)
.unwrap_or(std::cmp::Ordering::Equal)
})
}
}

/// Data related to a single contact between two bodies.
Expand Down