Two KinematicBody2Ds ram into each other and jitter

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By josh65536

When 2 KinematicBody2Ds run into each other at high speed, they jitter. I think what’s happening is that both colliders update using move_and_slide assuming that the other collider didn’t move, causing them to overlap each other, then back up too far away. The thing is then when I print the first collider’s position before the move_and_slide call, the second collider knows that the first one’s position has changed, which is weird. Is there a way that the second collider’s move_and_slide can take into account the fact that the first collider already moved?

Things that didn’t work:

  • Making one collider sync to physics
  • Making both colliders sync to physics
  • Using RigidBody2D instead (it was better, but the colliders still got to overlap considerably first)
  • Using RigidBody2D with a custom integrator (when the colliders backed up, they stayed apart for good, even if the velocity is set every frame)

The code:

extends KinematicBody2D


export (Color) var color := Color(1, 1, 1)
export (Vector2) var velocity := Vector2(200, 0)

func _ready():
    $Polygon2D.color = color

func _physics_process(delta):
    move_and_slide(velocity, Vector2(0, -1))

The scene consists of 2 nodes with this script. Each one has a rectangle CollisionShape2D centered at its origin and with extents 16 and 16. The first node is at (-16, 384) and has velocity (800, 0). The second node is at (1168, 384) and has velocity (-800, 0).