0 votes

I am using something like

player.shape.collide(
    player.get_transform() * player.shape_node.get_transform(), 
    enemy.shape, 
    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.

in Engine by (386 points)

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

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.

1 Answer

+1 vote

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.

by (21,920 points)

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.

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.