Skip to content

Conversation

@Jondolf
Copy link
Contributor

@Jondolf Jondolf commented Apr 3, 2025

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].

Logs

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_matrix uses 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:

I_shifted = I + m * (dot(R, R) E_3x3 - outer_product(R, R))

where E_3x3 is the 3x3 identity matrix and R is 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.

Logs

2025-04-04.02-40-35.mp4

Copy link
Contributor

@ThierryBerger ThierryBerger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I appreciate the PR description, changelog and tests alongside the bugfix <3

@ThierryBerger ThierryBerger merged commit 95288fa into dimforge:master Apr 7, 2025
7 checks passed
@Jondolf Jondolf deleted the fix-shifted-inertia-matrix branch April 7, 2025 13:39
@sebcrozet sebcrozet mentioned this pull request Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants