dimforge / rapier

2D and 3D physics engines focused on performance.
https://rapier.rs
Apache License 2.0
3.77k stars 235 forks source link

Feat: implement a "small-steps" velocity-based constraints solver + joint improvements #579

Closed sebcrozet closed 5 months ago

sebcrozet commented 5 months ago

This is a complete rework of Rapier’s constraints solver. This has been cooking for quite a while now and I think it is time to be published, even though the parallel version has not been fully done yet (it will work, but with only island-based parallelism).

The idea is to combine the substep approach to solving constraints, similar to the latest known improvements of XPBD, but applied to Rapier’s velocity-based constraints formulation. In other words, it does not use the PBD formalism, but will solve Rapier’s pre-existing velocity-based constraints by subdividing the timestep into n substeps with 1 force calculation iteration per substep, instead of running n force calculation iteration for the full timestep. This result in a significantly improved convergence of joints, especially in case of large mass ratios due to the more non-linear handling of constraints.

Other changes include:

The following video shows the stability improvements on a high-mass-ratio pendulum scenario. The large ball is 1000x heavier than the small ones:

https://github.com/dimforge/rapier/assets/1734958/c65b8e27-a1e0-4226-a949-9177ce8b9f77

https://github.com/dimforge/rapier/assets/1734958/1ab06183-1d40-4444-afdd-6a610eb16553

Here is a video of the new spring joint. The springs, with various damping coefficients, are attached to the balls. The cubes are just object falling freely on top of the balls:

https://github.com/dimforge/rapier/assets/1734958/99ff79c4-67f2-4af5-ad7e-306650fc1ac0

Fix #241