Skip to content

Use implicit Euler for gyroscopic torque#420

Merged
Jondolf merged 2 commits into
mainfrom
stable-gyroscopic-motion
Jul 13, 2024
Merged

Use implicit Euler for gyroscopic torque#420
Jondolf merged 2 commits into
mainfrom
stable-gyroscopic-motion

Conversation

@Jondolf

@Jondolf Jondolf commented Jul 12, 2024

Copy link
Copy Markdown
Member

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.

@Jondolf Jondolf added bugfix A-Dynamics Relates to rigid body dynamics: motion, mass, constraint solving, joints, CCD, and so on labels Jul 12, 2024
@Jondolf Jondolf merged commit 2a25e51 into main Jul 13, 2024
@Jondolf Jondolf deleted the stable-gyroscopic-motion branch July 13, 2024 12:51
@Jondolf Jondolf added C-Bug Something isn't working and removed bugfix labels Jul 19, 2024
Jondolf added a commit that referenced this pull request Aug 10, 2024
# Objective

Fixes #474.

The angular locked axes are applied to the inertia instead of the inverse inertia in gyroscopic torque computations introduced in #420, causing incorrect behavior.

## Solution

Apply the locked axes correctly.
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).

![Performance comparison](https://github.com/user-attachments/assets/a5887f4d-298d-4522-b4b4-777ff816a9c0)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Dynamics Relates to rigid body dynamics: motion, mass, constraint solving, joints, CCD, and so on C-Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spawn objects randomly disappears

1 participant