How do you get a list of the Area2D nodes that a rigid body is currently inside?

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

So here is the scenario.

I have a space sim, and I’ll have several overlapping Area2D:

  1. The Sun’s sphere of influence (SOI)
  2. A planet’s SOI
  3. A planet’s Atmosphere

I will have many objects that can interact with these gravity fields, but I want them to be able to do some orbital calculations as well.

In order to do these calculations I need to know the objects position, the objects velocity relative to the area2d centroid, and the gravity present in the area2d as well as the gravity_distance_scale.

I also need to be able to tell if the area is an Atmosphere or a gravity well.

I need to do this in priority order.

So the trick is, I need the bodies to track the area2d they are inside.

The signals that Area2D provides aren’t enough. I can get body_entered but this does not pass the Area2D to the body for me to keep track of. It also doesn’t give me the first Area2d I start in onready.

Any advice for listing objects of a certain type in the scene or some other way to do this?

:bust_in_silhouette: Reply From: klaas

put the areas into groups. Get them with get_Tree().get_nodes_in_group ( String group )


You can use body_entered:
connect the signal to the area itself … then …


func _on_body_entered( node ):
    node.i_just_entered_this_area( self )

Good suggestions, thanks!

Ryan Peach | 2020-08-19 01:29