Two KinematicBody2Ds moving in the same frame collide when they shouldn't

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

I have two KinematicBody2Ds, call them left and right box. They are touching, or nearly touching, at the beginning of the frame. I want to move both boxes to the left by 1 unit in the same frame, but right box is colliding with left box even though I moved left box first. They’re being moved with move_and_colide(Vector2(-1,0)).

I thought, at first, they were being moved in the wrong order (right then left, instead of left then right), so as a test I’m moving them from the same script where I can precisely control the sequence of events. Even though I’m moving the left box to the left, then the right box to the left in that order, the right box still hits the left box. The left box should be out of the way after the first call to move_and_collide.

However, this same sequence of events works fine if spread over multiple frames. If I move the left box on frame 1, then the right box on frame 2, it works fine. This leads me to believe that when you move a KinematicBody2D it doesn’t update its collision information in the physics engine until the end of the physics frame. If this is the case, then how can I get two KinematicBody2D objects that are close to each other to move in this way?

Whenever you have problems like this and don’t really know where to start, try using the call_deferred() method on the problematic functions: Object — Godot Engine (stable) documentation in English

It works for disabling collision shapes in nodes, for example, because if you just try to disable it normally, the collision engine can be occupied with something else and do it on the next frame. So this may be what you’re looking for if you need some physics stuff done ASAP. Try it.

Although I’m not even close to being an expert on the Godot engine and my understanding of call_deferred() may not be the best, I found this to be quite useful.

Ox0zOwra | 2022-09-16 09:23

I’ve made progress on this. The engine does support updating transforms mid physics update by using the sync to transform option in the inspector. However, checking this option breaks move_and_slide and move_and_collide, so I had to make a new system for movement. Which is okay anyway, since move_and_slide in particular was not playing nice with the pixel precision I needed.

BumfuzzledGames | 2022-09-20 03:33

:bust_in_silhouette: Reply From: aXu_AP

Maybe the engine updates the collision shape positions only after everything has moved to make sure everything moves in sync and order of objects shouldn’t matter. Think about it: if moving boxes to the left wouldn’t result in collision but moving to the right would, I’d say that’s bugged behaviour.

I believe your best option if this is something that realistically happens often in your game is: find out beforehand if you’re about to collide with another object (for example with test_move) and then add_collision_exception_with if the other object is moving in the same direction.