Skip to content

Commit fc1cd60

Browse files
kristoff3rWiwip
authored andcommitted
Add system that asserts physics components are finite (avianphysics#909)
A lot of people (me included) have had to track down NaNs or infinities that causes avian internals to crash, and they can be difficult to track down because they cause crashes a lot later than they are introduced. Add a system when running with debug assertions that checks relevant components during `PhysicsSystems::First`. For bugs in Avian internals we can potentially also schedule this system in other places. * I placed it in `PhysicsSchedulePlugin` because it feels more general purpose than the rest of `PhysicsDebugPlugin` and because it needs `self.schedule`, but I'm not sure if that's the best location for it? * We could make the system use `assert!` instead of `debug_assert!` and make it runnable outside of `debug_assertions`? I modified one of the examples to introduce an infinity during Update and saw that it was caught. Added mass query data accessor.
1 parent 73c0e0d commit fc1cd60

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/dynamics/rigid_body/forces/query_data.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ pub struct Forces {
121121

122122
/// A [`ForcesItem`] that does not wake up the body when applying forces, torques, impulses, or accelerations.
123123
/// Returned by [`ForcesItem::non_waking`].
124-
///
125124
/// See the documentation of [`Forces`] for more information on how to apply forces in Avian.
126125
pub struct NonWakingForcesItem<'w, 's>(pub ForcesItem<'w, 's>);
127126

@@ -213,6 +212,12 @@ pub trait ReadRigidBodyForces: ReadRigidBodyForcesInternal {
213212
self.ang_vel()
214213
}
215214

215+
/// Returns the [`ComputedMass`] of the body.
216+
#[inline]
217+
fn mass(&self) -> &ComputedMass {
218+
ReadRigidBodyForcesInternal::mass(self)
219+
}
220+
216221
/// Returns the linear acceleration that the body has accumulated
217222
/// before the physics step in world space, including acceleration
218223
/// caused by forces.
@@ -593,6 +598,7 @@ trait ReadRigidBodyForcesInternal {
593598
fn rot(&self) -> &Rotation;
594599
fn lin_vel(&self) -> Vector;
595600
fn ang_vel(&self) -> AngularVector;
601+
fn mass(&self) -> &ComputedMass;
596602
fn global_center_of_mass(&self) -> Vector;
597603
fn locked_axes(&self) -> LockedAxes;
598604
fn integration_data(&self) -> &VelocityIntegrationData;
@@ -630,6 +636,10 @@ impl ReadRigidBodyForcesInternal for ForcesItem<'_, '_> {
630636
self.angular_velocity.0
631637
}
632638
#[inline]
639+
fn mass(&self) -> &ComputedMass {
640+
self.mass
641+
}
642+
#[inline]
633643
fn global_center_of_mass(&self) -> Vector {
634644
self.position.0 + self.rotation * self.center_of_mass.0
635645
}
@@ -711,6 +721,10 @@ impl ReadRigidBodyForcesInternal for NonWakingForcesItem<'_, '_> {
711721
self.0.ang_vel()
712722
}
713723
#[inline]
724+
fn mass(&self) -> &ComputedMass {
725+
self.0.mass
726+
}
727+
#[inline]
714728
fn global_center_of_mass(&self) -> Vector {
715729
self.0.global_center_of_mass()
716730
}
@@ -746,6 +760,10 @@ impl ReadRigidBodyForcesInternal for ForcesReadOnlyItem<'_, '_> {
746760
self.angular_velocity.0
747761
}
748762
#[inline]
763+
fn mass(&self) -> &ComputedMass {
764+
self.mass
765+
}
766+
#[inline]
749767
fn global_center_of_mass(&self) -> Vector {
750768
self.position.0 + self.rotation * self.center_of_mass.0
751769
}

0 commit comments

Comments
 (0)