jrouwe / JoltPhysics

A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West.
MIT License
6.72k stars 447 forks source link

`GetBodyCreationSettings()` stores NaNs when using limited DOFs #915

Closed mihe closed 9 months ago

mihe commented 9 months ago

I figured this probably warranted its own issue. I was trying to load a snapshot (this one) from the project linked in #907 and noticed that it immediately asserted here:

https://github.com/jrouwe/JoltPhysics/blob/bab41c750f0c39ffd4c1193c880a5c418591e546/Jolt/Physics/Body/MotionProperties.inl#L41

... which seems to be because the body is stored and re-created with NaN mass and inertia.

When stepping through PhysicsScene::FromPhysicsSystem on the Godot side of things it seems like Body::GetBodyCreationSettings is storing the mass by doing 1.0f / mMotionProperties->GetInverseMassUnchecked() as seen here:

https://github.com/jrouwe/JoltPhysics/blob/bab41c750f0c39ffd4c1193c880a5c418591e546/Jolt/Physics/Body/Body.cpp#L353-L354

... but this inverse mass has been set to 0 as a result of limiting all the translation axes, as seen here:

https://github.com/jrouwe/JoltPhysics/blob/bab41c750f0c39ffd4c1193c880a5c418591e546/Jolt/Physics/Body/MotionProperties.cpp#L21-L26

... which leads to NaNs.

Similarly, because one angular axis is also limited in this case, one of the columns of the inverse inertia ends up being all zeroes, which leads to the determinant also being zero when inversing the inverse inertia, which then makes the inertia tensor NaN as well.

jrouwe commented 9 months ago

Thanks for reporting!