bevy_xpbd
bevy_xpbd copied to clipboard
Also clamp angular and linear velocities to max speeds while solving constraints
trafficstars
Objective
Allow MaxLinearSpeed and MaxAngularSpeed components to be used to prevent the issue described on this Discord message that can cause objects to “disappear”. (Actually get corrupted with NaNs)
Some details on the issue:
- When many objects interact, particularly when constrained by joints (that update position and rotation instantaneously via impulse) and seemingly depending on the arbitrary solving order, it's possible for angular velocity/linear velocity to be exchanged and transferred between objects in a cascade that can produce really high velocity values (beyond reasonable floating point precision range) that then quickly diverge into non-finite values.
- I haven't been able to fully understand the exact mechanism, but believe
RigidBodyQueryItem::velocity_at_point()is involved, producing a “lever“-like effect (that somehow doesn't properly conserve momentum, possibly due to the interaction with joint constraints) MaxLinearSpeedandMaxAngularSpeedcan't really be used to prevent this, as duringContactConstraintsolving it is possible for a dynamic rigid body's linear and angular velocity to (temporarily) exceed the limits specified by them, as they are presently only applied during velocity integration.
Solution
- Added
MaxLinearSpeedandMaxAngularSpeedtoRigidBodyQuery - Added
RigidBodyQueryItem::clamp_velocities()method - Clamp velocities during
ContactConstraint::warm_start(),ContactConstraint::solve()andContactConstraint::apply_restitution()
Changelog
- Fixed:
MaxLinearSpeedandMaxAngularSpeednow apply continuously duringContactConstraintsolving, preventing dynamic rigid bodies from (temporarily) exceeding the speed limits during this computation.
Migration Guide
- If you're using really low values of
MaxLinearSpeedandMaxAngularSpeed(e.g. same order of magnitude asSolverConfig::max_overlap_solve_speedor smaller) and encounter collision “tunneling” issues (i.e. objects clipping through each other) consider bumping them to higher amounts, as these components now also apply duringContactConstraintsolving and can prevent overlap solving from working properly if set too low.