Use implicit Euler for gyroscopic torque#420
Merged
Merged
Conversation
This was referenced Aug 10, 2024
Jondolf
added a commit
that referenced
this pull request
May 15, 2025
# Objective #420 changed Avian to use implicit Euler integration for solving gyroscopic torque. This was done for stability, as the semi-implicit version was prone to blowing up, because it extrapolated angular velocity and brought energy into the system. However, that implementation of gyroscopic torque is fairly expensive, often being more than half of the total cost of velocity integration. It is also computed even for shapes with isotropic inertia tensors (e.g. spheres and regular solids) that don't experience gyroscopic torque. It would be nice to make this have less overhead. Additionally, #735 broke gyroscopic motion, which needs to be fixed. ## Solution 1. Switch gyroscopic torque back to an approach that is closer to semi-implicit Euler integration, but clamp the magnitude of the angular momentum to remain the same. This prevents energy from being introduced to the system and seems to be sufficiently accurate for game physics. This idea was inspired by Jolt's [`ApplyGyroscopicForceInternal`](https://github.com/jrouwe/JoltPhysics/blob/d497df2b9b0fa9aaf41295e1406079c23148232d/Jolt/Physics/Body/MotionProperties.inl#L102). 2. Skip computation of gyroscopic torque for shapes with isotropic inertia tensors. This makes e.g. spheres, cubes, and other regular solids faster. > [!NOTE] > Do regular solids really have isotropic inertia tensors? Yes! > See [Moments of inertia of Archimedean solids](https://web.archive.org/web/20211003110410/https://www.ipgp.fr/sites/default/files/archimedeani_200115.pdf) by Frédéric Perrier: "The condition of two axes of threefold or higher rotational symmetry crossing at the center of gravity is sufficient to produce an isotropic tensor of inertia. The MI around any axis passing by the center of gravity are then identical." ## Results This is a 3D test scene with a bunch of cube stacks. The cubes have isotropic inertia tensors. - **Left**: Before any optimizations. - **Middle**: With the faster gyroscopic torque solver. - **Right**: With the faster gyroscopic torque solver, but ignoring computation for isotropic tensors (all dynamic bodies in this scene).  Velocity integration went down from 0.24 ms to 0.18 ms with the new solver, and still down to 0.13 ms ignoring unnecessary gyroscopic computations. Nice! In the future, we could also consider making gyroscopic motion opt-in with a `GyroscopicMotion` component if we wanted to optimize this further for non-isotropic cases. ## Bonus: `gyroscopic_motion` Example I added a new `gyroscopic_motion` example to demonstrate the Dzhanibekov effect and test the conservation of angular momentum. https://github.com/user-attachments/assets/9ef9e335-feb9-4685-8f12-2f34ba442937
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Fixes #400.
Gyroscopic motion is currently accounted for with a gyroscopic term computed using semi-implicit Euler. However, this sometimes leads to the torque just blowing up and making angular velocities essentially infinite, because semi-implicit Euler extrapolates velocities, and the gyroscopic torque is quadratic in angular velocity.
This manifested as angular velocities blowing up and rotation becoming invalid for objects with non-uniform inertia, especially when spinning at high speeds, as seen in the videos below:
2024-07-13.02-24-29.mp4
2024-07-13.02-23-23.mp4
Solution
Use implicit Euler for computing the gyroscopic torque in a much more stable way. The Newton Raphson method is used to solve the resulting non-linear equations. See Erin Catto's GDC 2015 slides on Numerical Methods for more information.
This fixes the big explosions and produces higher quality simulation:
2024-07-13.02-19-21.mp4
2024-07-13.02-30-28.mp4
Implicit Euler is slightly more expensive for this, but it should be acceptable in this case. We could explore other options in the future, like making gyroscopic motion optional, or trying an adaptive approach that only uses implicit Euler as a fallback.