Get the coordinates of a collision in 2D

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

Hi,
My player character (kinematic body 2D) has a weapon as a child with its own hitbox. When a enemy (also kinematic body 2D) enters the weapon hitbox it passes a signal to the player script to trigger functions in the enemy to take damage like this:

func _on_WeaponHitbox_body_entered(body):
enemy = body
enemy._take_damage(weapon_damage)
enemy = null

I would like to play some hit effect animation at the exact point of contact between the weapon and the enemy when they collide. How do I get these coordinates?

:bust_in_silhouette: Reply From: maxwellc055

Hi! Because the enemy is a KinematicBody2D, it can be made to return a KinematicCollision2D object upon being hit.

An easy way to achieve the effect you are looking for is to say something likeVector2 position = get_slide_collision(0).position in the script of the enemy object. The object we called position will then hold the coordinates of collision.

get_slide_collision(int slide_idx) is a method in the KinematicBody2D class that returns a KinematicCollision2D object. KinematicCollision2D objects have a position property which contains the exact coordinates of the collision.

This code depends on you using move and slide for your movement. Please let me know if this helps!

More information can be found in the Godot API (we love the Godot API) for the KinematicBody2D class and for the KinematicCollision2D class

Hi, thanks for answering,
does this require the enemy object to be looking for collision with the weapon? I have set things up to have as few collision checks as possible. Thus, I have the activated weapon looking for collision with enemies, which is 1 object checking when attacking. The enemy does not know that it collided with anything. If I did it the other way around it would be many enemy objects checking constantly for collision with a weapon.

jonkbonker | 2021-05-23 18:16

Happy to help. This code would require the enemy to be checking for collisions from the weapon. If the weapon is also a KinematicBody2D, what I described before can be done with the weapon, rather than the enemy, which would not require the enemy to check for weapon collisions.

I sadly cannot think of a way to receive the exact coordinates of collisions without using the previously mentioned methods and hence a KinematicBody2D, so I believe your code will have to work around that condition. All this being said, enemies checking for weapon collisions is fairly typical and should have no significant performance effects, though I understand if you have hordes of enemies and are trying to keep things as clean as possible.

maxwellc055 | 2021-05-23 22:05

yes its probably not that big of a deal for enemies to check, collision checks with body entered and signals in Godot seems quite efficient. This does explain why some other projects I looked at seem to prioritize convenience over minimizing collision checks, so that is probably the way to go for me as well. Thank you very much for your answers! :slight_smile:

jonkbonker | 2021-05-24 13:46