How to detect collision with KinematicBody2D without move()?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By inTech
:warning: Old Version Published before Godot 3 was released.

Hey people

So I have two scenes set-up. One to spawn the player and another one as the level. The player is KinematicBody2D node and the level’s object is RigidBody2D node. I have CollisionShap2D on all my objects and currently they are able to move each other when they collide.

However, I cannot detect collision via GDScript. I tried is_colliding() function and it never return true. I searched for similar question already and one of the replies was that is_colliding only works if I have move() function inside my KinematicBody2D object.

Now, my question is how do I detect collision without using the move() function?

:bust_in_silhouette: Reply From: bruteforce

Try the test_move function!

From the docs:

bool test_move ( Vector2 rel_vec )
Return true if there would be a collision if the body moved in the given direction.

Thanks for the quick response! This simple and handy trick worked for me!

if (test_move(Vector2(get_pos().x, get_pos().y))):

inTech | 2016-12-13 09:01

I don’t think that the get_pos() Vector is the right choice for this purpose.
The test_move function expects a direction (documentation).
For example, if the test_move(Vector2(0,1)) returns true, it means, that your kinematicbody can not move downward (in a side-scrolling game).
But I’m a beginner… so it is not completely sure :smiley:

bruteforce | 2016-12-13 10:19

Oh thanks details. After some testing with the previous script, there is indeed a logic bug.

I changed my code to the following and it seem to return true only when its collide with an object above themselves.

if (test_move(Vector2(0, -1)) || test_move(Vector2(1, -1)) || test_move(Vector2(-1, -1))):

I am not sure if I need the last two OR conditions or not. I think it really depend on how my game works. I will be testing it and see which one blend in with my game better.

Thank you again.

inTech | 2016-12-14 03:59