godotengine / godot-docs

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

Manually interpolating VehicleWheel #6689

Open GearedForFear opened 1 year ago

GearedForFear commented 1 year ago

A VehicleWheel can easily push the physics interpolation of Godot 3.5 to its limits. See this issue. The VehicleWheel documentation does not mention this. Of course, not every known issue needs to be part of the docs. However, this one can occur by just having a fast car.
The documentation could explain the issue. It could also advise readers to turn off physics interpolation for affected meshes. Additionally, it could offer example code for interpolating them manually, such as:

func _process(_delta):
    global_translation = get_parent().get_global_transform_interpolated().origin
GearedForFear commented 1 year ago

I found a flaw with my suggested code. The method get_global_transform_interpolated() will always return the interpolated transform, even if there is no visible interpolation. This can happen, when reset_physics_interpolation( ) is called. Improved implementation:

var interpolated_parent: bool = true
var wait: bool = false

func _process(_delta):
    if interpolated_parent:
        global_translation = get_parent().get_global_transform_interpolated().origin

func _physics_process(_delta):
    if wait:
        wait = false
    else:
        interpolated_parent = true

func _notification(what):
    if what == NOTIFICATION_RESET_PHYSICS_INTERPOLATION:
        interpolated_parent = false
        wait = true