jrouwe / JoltPhysics

A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West.
MIT License
6.46k stars 420 forks source link

[Feature Request] Compute gyroscopic forces #803

Closed akb825 closed 9 months ago

akb825 commented 9 months ago

When comparing Jolt to other physics engines such as Bullet and PhysX, one feature I see missing is the ability to compute gyroscopic forces on rigid bodies.

For both Bullet and PhysX, gyroscopic forces may be opted into for individual rigid bodies based on the body flags. In Bullet there's a few different options, but the default is for implicit body forces (with the computeGyroscopicImpulseImplicit_Body function), which I'd imagine would be the most generally applicable.

It would be useful to be able to opt into gyroscopic forces for rigid bodies in Jolt as well.

jrouwe commented 9 months ago

Hello,

Do you have a practical use case where the gyroscopic forces are important?

akb825 commented 9 months ago

For my own immediate usage, I'm in the early process of integrating physics into my project, part of which includes having a generalized interface that makes it easy to swap out the underlying physics engine as needed. When trying to ensure as much feature parity as possible, and using Jolt, Bullet, and PhysX as references, this was one hole that I noticed for Jolt.

That said, there are a few practical use cases that come to mind:

jrouwe commented 9 months ago

It was easy to add, so I added it but:

You've recently added some experimental motorcycle vehicle support. Given that gyroscopic forces on the wheels are what keep two-wheeled vehicles upright in real life, this could allow for more accurate simulations.

The vehicles don't use rotating rigid bodies for wheels, they fake wheels by doing ray/shape casts. This gives more control over the simulation but doesn't allow this effect. B.t.w. gyroscopic forces only occur when an object has 3 different moments of inertia but wheels only have 2. I think this is a nice video explaining the effect: https://www.youtube.com/watch?v=1VPfZ_XzisU

Simulation of a spinning top would be a rather obvious example. If you wanted to create a physics-based Beyblade (or legally distinct equivalent 😉) game, for example, gyroscopic forces would be required.

Also this would normally not see an effect since tops are (close to) symmetric around their rotation axis so only have 2 different moments of inertia.

akb825 commented 9 months ago

Thanks for the commit and the instructional video.

B.t.w. gyroscopic forces only occur when an object has 3 different moments of inertia but wheels only have 2.

Based on the video and this clarification, I think you're referring to the "tennis racket" effect as described in the video. In these cases I described for the motorcycle or top, my understanding (granted from high school physics quite some time ago) was that the gyroscopic effect would try to keep the rotation along the largest moment of inertia. So in the case of a rotating disk (like a wheel or a wide top), with two moments of inertia as you describe, it would try to keep rotation oriented with the axis perpendicular to the disk and could counteract the forces of gravity, such as keeping a two-wheeled vehicle (at least one that is using rigid bodies for the wheels) upright or a top from toppling over.

Is this a separate force, or a subset of what you're describing and already covered with your commit? I just wanted to make sure we're using the same terminology and I'm not misunderstanding either what you're describing or the gyroscopic forces implemented by other engines such as Bullet or PhysX.

jrouwe commented 9 months ago

You're right, there is an effect when the object has 2 different axis of inertia too, it just doesn't exhibit the Dzhanibekov effect. In any case the gyroscopic forces are now implemented in the same way as Bullet or PhysX.