Skip to content

Optimize gyroscopic motion#738

Merged
Jondolf merged 6 commits into
mainfrom
improved-gyroscopic-motion
May 15, 2025
Merged

Optimize gyroscopic motion#738
Jondolf merged 6 commits into
mainfrom
improved-gyroscopic-motion

Conversation

@Jondolf

@Jondolf Jondolf commented May 14, 2025

Copy link
Copy Markdown
Member

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

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.

2025-05-14.20-21-16.mp4

@Jondolf Jondolf added this to the 0.4 milestone May 14, 2025
@Jondolf Jondolf added C-Performance Improvements or questions related to performance A-Dynamics Relates to rigid body dynamics: motion, mass, constraint solving, joints, CCD, and so on labels May 14, 2025
@Jondolf Jondolf merged commit c15d760 into main May 15, 2025
6 checks passed
@Jondolf Jondolf deleted the improved-gyroscopic-motion branch May 15, 2025 10:35
@Jondolf Jondolf mentioned this pull request May 26, 2025
Jondolf added a commit that referenced this pull request May 26, 2025
# Objective

#738 optimized gyroscopic torque. However, it also broke it! The current algorithm only uses the diagonal of the inertia tensor at the end, when it should be using the full tensor.

## Solution

Use the full tensor.

Alternatively, if we represented the inertia tensor with the principal moments of inertia and an orientation, we would just apply that additional orientation. This is what Jolt does.
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-Performance Improvements or questions related to performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant