0 votes

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?

Godot version 3.5
in Engine by (16 points)

Whenever you have problems like this and don't really know where to start, try using the call_deferred() method on the problematic functions: https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred

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.

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.

1 Answer

0 votes

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.

by (1,100 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.