This is much easier starting with the Area2D
s. You can query each Area2D
with overlaps_body
. Something like this:
var areas := [$Area, $Area2] # perhaps get the list from somewhere else
for area in areas:
if area.overlaps_body(self):
print("Overlapping " + area.name)
On the other hand, if you don't have references to the areas, you will have to do a physics query, which I believe would be like this:
var space := get_world_2d().direct_space_state
var query := Physics2DShapeQueryParameters.new()
query.collide_with_areas = true
query.collide_with_bodies = false
query.set_shape($CollisionShape2D.shape)
query.transform = $CollisionShape2D.global_transform
var result := space.intersect_shape(query)
for area_collision in result:
var area := area_collision.collider as Area2D
if is_instance_valid(area): #perhaps some other checks here
print("Overlapping " + area.name)
This is the physic query in C#:
var space = GetWorld2d().DirectSpaceState;
var query = new Physics2DShapeQueryParameters();
query.CollideWithAreas = true;
query.CollideWithBodies = false;
var collision = GetNode<CollisionShape2D>("CollisionShape2D");
query.SetShape(collision.Shape);
query.Transform = collision.GlobalTransform;
var result = space.IntersectShape(query);
foreach (Godot.Collections.Dictionary item in result)
{
GD.Print("All Dictionary: " + item.ToString());
var collider = item["collider"];
GD.Print("Found: " + collider.ToString());
GD.Print("Type: " + collider.GetType().ToString());
var area = collider as Area2D;
if (area!= null)
{
GD.Print("Some Random Area");
}
var treasure = collider as Treasure;
if (treasure != null)
{
GD.Print("Found some treasure");
}
}