Fix wrong sign in MassProperties::construct_shifted_inertia_matrix
#334
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Parry's formula for the 3D shifted angular inertia tensor is wrong, causing compound shapes with offsets to have an incorrect angular inertia. This results in wonky behavior in physics engines such as Rapier and Avian.
An example of this: A 1x3x1 cuboid collider in bevy_rapier3d has a principal angular inertia of
[2.5, 0.5, 2.5]. However, a compound collider of three 1x1x1 cubes stacked on top of each other has a principal angular inertia of[2.5, 4.5, 2.5].Spawning dynamic bodies for the two colliders with a slight rotation about the x axis and some initial angular velocity, this results in the following behavior:
2025-04-04.02-20-50.mp4
The 1x3x1 cuboid spins a bit at the start, falls on its side, and goes to rest quickly, like expected. However, the compound shape has a lot more angular inertia about the y axis, causing it to spin a lot longer and even start rolling on the ground in a bizarre way.
Solution
MassProperties::construct_shifted_inertia_matrixuses the parallel axis theorem to compute the moment of inertia at a given offset. More specifically, it uses the generalization for tensors.The correct formula looks like the following:
where
E_3x3is the 3x3 identity matrix andRis the displacement vector (i.e. shift). However, Parry incorrectly adds the outer product of the displacement rather than subtracting it.Fixing this results in the correct values and much more reasonable behavior.
2025-04-04.02-40-35.mp4