From 158308ad715d00f2c0f259b185bf0410395b1aac Mon Sep 17 00:00:00 2001 From: Sébastien Crozet Date: Mon, 4 Jul 2022 15:04:06 +0200 Subject: Add hepler function for building a contact force event from a contact pair --- src/geometry/mod.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/geometry') diff --git a/src/geometry/mod.rs b/src/geometry/mod.rs index df9a785..ddb161e 100644 --- a/src/geometry/mod.rs +++ b/src/geometry/mod.rs @@ -140,6 +140,42 @@ pub struct ContactForceEvent { pub max_force_magnitude: Real, } +impl ContactForceEvent { + /// Init a contact force event from a contact pair. + pub fn from_contact_pair(dt: Real, pair: &ContactPair, total_force_magnitude: Real) -> Self { + let mut result = ContactForceEvent { + collider1: pair.collider1, + collider2: pair.collider2, + total_force_magnitude, + ..ContactForceEvent::default() + }; + + for m in &pair.manifolds { + let mut total_manifold_impulse = 0.0; + for pt in m.contacts() { + total_manifold_impulse += pt.data.impulse; + + if pt.data.impulse > result.max_force_magnitude { + result.max_force_magnitude = pt.data.impulse; + result.max_force_direction = m.data.normal; + } + } + + result.total_force += m.data.normal * total_manifold_impulse; + } + + let inv_dt = crate::utils::inv(dt); + // NOTE: convert impulses to forces. Note that we + // don’t need to convert the `total_force_magnitude` + // because it’s an input of this function already + // assumed to be a force instead of an impulse. + result.total_force *= inv_dt; + result.max_force_direction *= inv_dt; + result.max_force_magnitude *= inv_dt; + result + } +} + pub(crate) use self::broad_phase_multi_sap::SAPProxyIndex; pub(crate) use self::narrow_phase::ContactManifoldIndex; pub(crate) use parry::partitioning::QBVH; -- cgit