Collisions and 2D physics

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

Hi guys !

After some time of research, I don’t find how to do what I want to do.
My main player (2D) has a sword in her hand and I want to detect the collision of the sword with ennemies of course (which are of RigidBody2D type).

However, concerning the physic engine, I don’t want the sword to interact with anything, even not with the ennemies. So for me making it a rigidbody2d or staticbody is out of question. My first attempt was to make it as an Area2D, but in that case, the collision are not catched by the ennemies (collision triggering ennemies damage).

Is that clear ? Do you have any idea ?

:bust_in_silhouette: Reply From: Hinsbart

All “collisions” with an Area trigger on the area itself, not on the other body.
So you could connect to “body_enter” and test if the body is an enemy, then call the damage function.

an example (off the top of my mind, not tested):

func on_area_body_enter(body):
    if body in get_tree().get_nodes_in_group("enemy"):
        body.damage()

Yeah, I thought about that also. It should work, but I would have prefer to delegate it to the ennemy class itself, after all it’s the ennemy which is hurt…
Thank you !

thomas_laverne | 2016-02-23 13:49

Alright, then how about adding an area also for the enemy? So it would have both a rigidbody, with collision shapes that allow it to move around the world, and an area that checks for damage collisions.

Area/Area collisions will be triggered on both objects.

Hinsbart | 2016-02-23 14:02

I thought about it too!!
It will work for sure, but the inconvenient is to have too different collision shape for each ennemy: one for the rigid body behavior and one for the Area2D.
That was the reason why I discarded this idea.

However, I think it’s not so bad after all. And it makes sense also, ennemies might not be vulnerable on all their body :wink:

Thank you very much for your quick responses !

thomas_laverne | 2016-02-23 14:07

It works fine!

Except when my ennemy is also an Area2D…
It seems to me that Area2D don’t report collision with Area2D (such has in the case StaticBody2D vs Area2D). Is this a bug or is this only me?

thomas_laverne | 2016-02-23 16:31

I found the issue! I was using body_enter on the sword which doesn’t monitors area. Using additionally area_enter when sword enter a Area2D made it work !
pfiouuuu !

thomas_laverne | 2016-02-23 17:13