Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.54k stars 120 forks source link

Fix 3D friction #542

Closed Jondolf closed 2 weeks ago

Jondolf commented 2 weeks ago

Objective

Fixes #489.

Friction in 3D currently behaves very unrealistically. In the README's example, the cube should bounce to the side as it lands on a corner:

Broken friction

Instead, the cube bounces up a bit, spins in the air, and falls in the middle of the platform.

And in a case with locked rotation and zero restitution, friction also causes a weird upward bounce:

https://github.com/user-attachments/assets/0eec421c-741e-4639-b74e-ee3c16cc6cc5

The culprit here is that the tangent directions for friction are computed wrong in 3D. For the initial contact, one of them is pointing along the contact normal, while the other is close to zero.

Incorrect tangent directions

Solution

Fix the tangent directions. This turned out to be just a dumb sign error...

This changes 3D behavior quite a bit, but it's for the better. Friction and bounces are more realistic, and stacking is more stable. Bounces behave correctly:

3d_scene

(note, the initial angular velocity is slightly different here than in the earlier example, it looks better for the README)