godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.78k stars 3.07k forks source link

Damping for 2D and 3D is described incorrectly. #8163

Closed thygrrr closed 7 months ago

thygrrr commented 11 months ago

Your Godot version: 4.1.2.stable but also 4.2.dev.custombuild

Issue description: The documentation is grossly(!) incorrect in describing the linear and angular damping techniques used. It insinuates a critical spring damper function (sometimes known as smoothdamp), in particular in the language where it says:

Note: Good values are in the range 0 to 1. At value 0 objects will keep moving with the same velocity. Values greater than 1 will aim to reduce the velocity to 0 in less than a second e.g. a value of 2 will aim to reduce the velocity to 0 in half a second.

This is wrong because:

The source code applies the following operation:

real_t damp = 1.0 - p_step * total_linear_damp;
//...
linear_velocity *= damp;

This is, after n physics ticks, equivalent of linear_velocity = linear_velocity_start * pow(1-p_step*total_linear_damp, n)

For a value of 2 at a physics simulation frame rate of 60, this will ACTUALLY result in about 18% remaining velocity after 0.5 seconds, and depending on the starting velocity.

At different physics tick rates, this will also result in different behaviour altogether of the damping function; this would be an issue to fix in the engine repo, however; the correct way to do this would be to peg this "damp" value (I usually call it k) to a reference tick rate; calculating k = (1.0total_linear_damp/reference_rate)^(reference_rate p_step).

Without this correcion, for instance, the remaining speed difference after 0.5 seconds for 60 tps physics vs 200 tps physics is 1:1.45 (+45%))

As for the documentation, before this fix is in, this very crass diffrence in behaviours from something as inoccuous as changing the physics step time (should result in higher precision, not lower precision!) must be mentioned.

URL to the documentation page: https://docs.godotengine.org/en/stable/classes/class_rigidbody3d.html#class-rigidbody3d-property-linear-damp https://docs.godotengine.org/en/stable/classes/class_projectsettings.html#class-projectsettings-property-physics-3d-default-linear-damp https://docs.godotengine.org/en/stable/classes/class_rigidbody2d.html#class-rigidbody2d-property-linear-damp https://docs.godotengine.org/en/stable/classes/class_projectsettings.html#class-projectsettings-property-physics-2d-default-linear-damp

If you know how to fix the issue you are reporting please consider opening a pull request.

I offer to do this later today (the documentation fix).

Further notes:

thygrrr commented 11 months ago

If you know how to fix the issue you are reporting please consider opening a pull request.

I offer to do this later today (the documentation fix).

thygrrr commented 11 months ago

These wild and floaty damper functions are also the reason why Godot physics needs that giant default linear velocity sleep threshold of 0.1 - this is offtopic for documentation but the more I read this physics code, the more I'm amazed it stayed like this for 3, 6, even 9 years in some places.

I'll see what I can do to help improve it in a proposal or two (but I'm in the camp "just integrate Jolt, and adapt physics server to its structure, breaking compatibility entirely, but at least have a clean cut").

Physics engines are hard. :) I failed at making two of them myself. ;)