Search code examples
collision-detectiongodot

Godot, how to get the CollisionShape2D node from an Area2D node?


I am trying to calculate one of the intersection points between 2 Area2D. I am following these instructions:

So I have to use the method collide_and_get_contacts from a Shape2D node.

But right now I only have the 2 Area2D nodes.

I am detecting the intersection on the signal Area2D.entered_area and this signal is only sending me the other Area2D, not the Shape2D.

What is the way of getting the CollisionShape2D from a random Area2D? (It is ok if the Area2D has many CollisionShape2D and I get the first one)


Solution

  • Instead of area_entered you can use area_shape_entered. I quote from the documentation:

    area_shape_entered ( RID area_rid, Area2D area, int area_shape_index, int local_shape_index )

    Emitted when one of another Area2D's Shape2Ds enters one of this Area2D's Shape2Ds. Requires monitoring to be set to true.

    area_rid the RID of the other Area2D's CollisionObject2D used by the Physics2DServer.

    area the other Area2D.

    area_shape_index the index of the Shape2D of the other Area2D used by the Physics2DServer. Get the CollisionShape2D node with area.shape_owner_get_owner(area_shape_index).

    local_shape_index the index of the Shape2D of this Area2D used by the Physics2DServer. Get the CollisionShape2D node with self.shape_owner_get_owner(local_shape_index).