Use implicit Euler for gyroscopic torque #420
Merged
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.