Spawning KinematicBody2D inside one another causes teleportation

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

I am making a SHMUP style game with asteroids and a main player ship. When the player kills an asteroid, multiple can spawn where the old one was. It appears that if a new asteroid spawns inside the player, he will teleport or just be instantly destroyed. Both entities move via move_and_slide. Asteroid uses a circle CollisionShape2D, ship is a CollisionPolygon2D.

How can I prevent this wonky player ship behavior? The only thing I can think of is checking position to ensure asteroids are not on top of the player, but that seems like it would be a lot of collision checking, tedious/fragile code.

:bust_in_silhouette: Reply From: Footurist

KinematicBody2D will try to avoid collision. So if you move it by move_and_slide, it will move away from the collider if it collides. If enough force is involved, this could look like teleportation, which really just is very fast movement in this case. To avoid this, set the CollisionMask and Layer of the new asteroid so, that it won’t collide with the player for a fixed amount of time and have it snap back to normal after some time.

Good idea! But what if they are overlapping when the mask/layer snap back to normal? Wouldn’t this cause the same problem?

jarlowrey | 2018-03-07 20:22

using move_and_collide instead of move_and_slide fixes the issue, like you suggested it might. I think I will go with this solution for now because of simplicity/robustness, even though it’s not quite the effect I’m after. I’m still interested in getting a working solution for the original problem though.

Also, I believe the player was being destroyed because he was being moved/teleported outside the viewport, which triggers code to call queue_free

jarlowrey | 2018-03-07 20:53

Is there any way to set a maximum force/speed on a kinematic body?

jarlowrey | 2018-03-07 21:06