Need help with jittery top-down 2d physics platforms

Godot Version

Godot Engine v4.5.1.stable.mono.arch_linux

Question

I’m having some trouble implementing a mechanic in Godot where an area2d will cause the characterbody2d to ride a platform. Think somewhat similar to frogger with the moving logs. What I’m seeing is on collision a quick jitter and then the “exit the platform” code runs. What I’m expecting to happen is that the player character can move relative to the platform when they are riding it, and they can transfer between platforms if they are close enough.

I’ve created a minimal project that exemplifies the general approach. What is the best option to upload the zip file?

Given that I’m very new to Godot I’m assuming that I’m doing something I shouldn’t be with the physics, or I have some other suboptimal design. Would appreciate it if someone could take a quick look at the code and setup and let me know what may be going wrong (or how I can improve upon it).

extends CharacterBody2D
class_name Character

const max_speed := 300.0
const acceleration := 50.0
const friction := 20.0

var connected_body: TestAnimateableBody


# source: https://www.youtube.com/watch?v=M66_K_oZdcg
func _physics_process(delta: float) -> void:
    var input = Vector2(Input.get_action_strength("right") - Input.get_action_strength("left"),
        Input.get_action_strength("backward") - Input.get_action_strength("forward")
    ).normalized()

    # https://forum.godotengine.org/t/calculating-a-staticbody-animatablebodys-velocity/62846/2
    if connected_body != null:
        velocity += PhysicsServer2D.body_get_state(connected_body.get_rid(), PhysicsServer2D.BODY_STATE_LINEAR_VELOCITY)
    else:
        velocity = Vector2.ZERO

    var lerp_weight = delta * (acceleration if input else friction)
    velocity += lerp(velocity, input * max_speed, lerp_weight)

    move_and_slide()


func _on_riding_detection_area_body_entered(body: Node2D) -> void:
    if body is TestAnimateableBody:
        print("connecting body")
        connected_body = body


func _on_riding_detection_area_body_exited(body: Node2D) -> void:
    if body is TestAnimateableBody:
        print("disconnecting body")
        connected_body = null

This has been fixed, and all of the related issues were not physics related. The issues were that the animateablebody2d was being indirectly moved so the physics state didn’t calculate any velocity. The other issue was in code in that the characterbody needs to track relative and exact velocity. Easiest way was to separate the concepts into two variables. Input to control relative velocity, then apply velocity of the riding platform if is riding on one.