How to know from which side a shape2D is colliding with another?

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

I am using something like

	player.get_transform() * player.shape_node.get_transform(), 
	enemy.get_transform() * enemy.shape_node.get_transform() )

but I would also like to know the ‘direction’ (left/right or above/below) of the collision

I know there is a collide_and_get_contacts() method for Shape2D, but I can’t seem to use it in a helpfull way.

Are your Shape2Ds parented to a PhysicsBody2D? For example, KinematicBody2D has get_collision_normal() which would be a useful way to get collision direction.

kidscancode | 2017-08-14 02:05

Thanks, it’s a great idea!
My code does use a KinematicBody2D as the holder of the shape.

Unfortunately I am setting the trigger of the shape to true, which causes the get_collision_normal(), is_colliding(), etc methods to not report coliisions.

ps: kidscancode? Uau, last week I was checking a video/blog post from you! Very usefull.

cardoso | 2017-08-14 14:35

:bust_in_silhouette: Reply From: kidscancode

If you’re using trigger, you could just compare the position of each shape when is_colliding() is true.

Relative position is just get_pos() - get_collider().get_pos() normalized. Check the direction of that vector in x and/or y and you know what side the colliding body is on.

That normalized logic sounds great. I’ll see if I find a way to use it with the points returned from collide_and_get_contacts() method of Shape2D.

As for is_colliding() of KinematicBody2D, unfortunately I cannot use it, cause I set the trigger on for the shapes permanently.

cardoso | 2017-08-14 21:40