0 votes

So I'm trying to make a way to select multiple objects in my project. I want it basically like Windows, where you can click and drag to create a box that you can use to select items that are in that box. I got it to create the selection box using a Polygon2D, and making it show up correctly, following the mouse, etc. So then I added an Area2D and a CollisionPolygon2D to that Area2D. I found out that I need to use get_overlapping_areas() on the area. When I do that though, it returns an empty array, unless I start the selection in an area. I had also tried using the area_entered signal, but it doesn't work either. I made the box stationary, and moved areas into it, and then it worked correctly. I've also verified the collision shape is correct by enabling show collision shapes.

To update the box, I'm just updating the PoolVector2Array of points. I'm wondering if maybe changing the shape of the collision object is preventing it from detecting if an area is inside. I was hoping get_overlapping_areas() would actually do the math to check if an area is inside another, but it appears like it just retrieves the list that is updated everytime an area enters.

Does anyone have any suggestions or clarifications if I'm not understanding correctly?

TL;DR I want to get all areas inside another while I'm changing it's shape, and it's not working.

in Engine by (60 points)

The list of overlapping_areas is only updated each physics frame. See here:
https://docs.godotengine.org/en/stable/classes/class_area2d.html#class-area2d-method-get-overlapping-areas

Returns a list of intersecting PhysicsBody2Ds. For performance reasons
(collisions are all processed at the same time) this list is modified
once during the physics step, not immediately after objects are moved.
Consider using signals instead.

Ok, the docs don't make clear if the check is also performed (each physics frame) when just the points of the collision shape are updated but the object does not move. You'd have to try it out. (Doesn't hurt to enable monitoring on all colliding areas for that test.)

If you're working with rectangular areas then maybe the using the Rect2 class with the methods encloses() or intercepts() might be helpful? (Similar to AABB in 3D).

That moment when you find a post that you think is going to help, because the problem is described exactly the same as yours...

And then you see this was your own earlier post.

1 Answer

+4 votes
Best answer

It seems like it only updates if the Areas change position. So I simply tried setting the position to (0, 0) (unchanged) everytime I want to get_overlapping_areas(), and that seems to force it to update, because now it works!!!

by (60 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.