Godot Version
4.4.1
Question
Hi!
I’m having an issue with collision detection in my scene in Godot (using GDScript). I have a scale_polygon function that temporarily scales a CollisionPolygon2D to check which other Area2D objects are overlapping with a given unit.
When I run the scene by itself, get_overlapping_areas() works fine and returns a list of neighboring Area2D objects, so everything seems OK.
The problem arises when I add this scene as a child to another scene — at that point, get_overlapping_areas() returns an empty array, even though the geometry is correct and everything is in the right place.
All Area2D nodes have monitoring and monitorable enabled. What could be causing get_overlapping_areas() not to work when the scene is loaded as a child?
Here’s the code:
func scale_polygon(scale_factor: float, node: Area2D) -> Array:
var collision_polygon = node.get_node("CollisionPolygon2D")
var polygon = collision_polygon.polygon
# Calculate the center point of the polygon
var center = Vector2.ZERO
for point in polygon:
center += point
center /= polygon.size()
# Scale each point relative to the center
var scaled_polygon = PackedVector2Array()
for point in polygon:
var direction = point - center
scaled_polygon.append(center + direction * scale_factor)
# Temporarily apply the scaled polygon to the collision shape
collision_polygon.polygon = scaled_polygon
await get_tree().physics_frame
await get_tree().physics_frame
# Print overlapping areas (neighbors)
print(node.get_overlapping_areas())
var neighbors := []
for collision in node.get_overlapping_areas():
neighbors.append(collision.name)
# Restore the original polygon and update visuals
collision_polygon.polygon = polygon
collision_polygon.queue_redraw()
await get_tree().process_frame
return neighbors
Scene structure: