Area2D with multiple collisionsshapes

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

I have an Area2D (a character) who has several collisions shapes, one for his body, one is a large circle around him which is its range to detect object.

Is it possible to put each collision shape in a different layer object? Because I want the first collision shape to only collide with other characters, and the second one to only collide with objects (another node of type Area2D).

Unless, the only solution is to distinguish collisions (process some and ignore others) in the code? Because as far as I know, only an area2D can have a collision layer, not his collisions shapes.

Or maybe, it’s good practice to define the node tree like this?

Area2D (Character)
–Area2D (to detect other characters)
---- CollisionShape
–Area2D (to detect objects)
---- Collision Shape

instead of

Area2D
–Collision1
–Collision2

Which is the best solution?

:bust_in_silhouette: Reply From: kidscancode

Multiple collision shapes on one Area2D still define only one area, so they will act like one large one.

If you want the shapes to have different functionality, then yes, you need them to be separate Area2D nodes. These can then be put on different collision layers, have different code for area_entered, etc.

Then I should do something like this?

Area2D (Character)
–Area2D (to detect other characters)
---- CollisionShape
–Area2D (to detect objects)
---- Collision Shape

Unless the first node does not have to be an Area2D? If so, what else could I choose? A Node2D?

After some thinking, it might be better to do

Scene
– Area2D
---- CollisionShape
---- Sprite
– Area2D
---- Collision Shape

what do you think ?

x4rkz | 2019-09-08 18:46

What you choose for your root node depends on what you want it to do. In your example, it could be the Sprite. If it needs collision, maybe a KinematicBody2D, etc. Try it a few ways and see what makes the most sense for your needs.

kidscancode | 2019-09-08 19:10

Okay but it is still unclear to me if I need can do this:

Scene
– Area2D
---- CollisionShape
---- Sprite
– Area2D
---- Collision Shape

Or if I need a wrapper like this:

Scene
– Area2D (or KinematicBody or Sprite or anything)
---- Area2D
------ CollisionShape
------ Sprite
---- Area2D
------ Collision Shape

x4rkz | 2019-09-08 19:38

Well, there’s no point in yet another Area2D node without a shape or anything. Why not this:

Area2D (main)
---- Sprite
---- CollisionShape2D
---- Area2D (secondary)
-------- CollisionShape2D

You move the main Area2D that the Sprite is a child of, and everything else comes along. No need for extra nodes that don’t serve a purpose.

kidscancode | 2019-09-08 22:15