Weird collision when changing kinematicbody2d position

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

I’m having this problem:
I have 2 players, when a player is dead, the collision layer/mask bit for colliding with other players is disabled. After some time, the player respawns and teleports to another place. But when a player is inside the dead player collision, it simply warps to the nearest point outside the collision.

Red player warps to the nearest point outside the blue player collision
Note: my code firstly teleports the player, them enable the collision bits.

$Respawn.start()
yield($Respawn, "timeout")
position = get_parent().get_respawn_positions()[randi()%4].position
set_collision_layer_bit(0, true)
set_collision_mask_bit(0, true)
is_dead = false

(Wild guess)
Not sure if this is a good/clean solution, but maybe you should try to set the collision-layer (and mask?) 1 frame later.

whiteshampoo | 2020-07-07 06:21

Yeah. I think everything is happening so fast and at the same frame. Maybe deferring the call or wait for the idle frame? Or even use another yield “timeout”? I’m curious about this too.

arthurZ | 2020-07-07 20:10

if i understand the physics-engine correctly, there is no update in the same frame after you change the position.
I also cannot find a way to recalculate the physics after you change the position manually.
If someone knows a clean way, i would be very interested!

whiteshampoo | 2020-07-07 20:26

:bust_in_silhouette: Reply From: INKnight

So, I tried whiteshampoo and arthurZ solution above by waiting a frame after the “warp” before setting the collision bits, and worked like a charm, thank you guys!

position = get_parent().get_respawn_positions()[randi()%4].position
yield(get_tree(), "idle_frame")
set_collision_layer_bit(0, true)
set_collision_mask_bit(0, true)