0 votes

I need to be able to detect if two sprites overlap. I don't think this needs any complicated physics colliders, I figured I'd be able to just use the Rect2D intersect function. The problem is, one of the sprites is in the scene via the editor, but the other sprite is added procedurally. Both are added under the same node, but the sprite that is added procedurally doesn't seem to have it's item_rect translated when the node is translated. Also, do the rects only work in local space, or will the intersection happen in viewport space?

Full Sene: Node with the transparent blue rectangle is one of the nodes of interest, it was placed via the editor.

Second Node Here is the second node of interest. That arm is added procedurally, and the parent node (of both) is where that blue rectangle is.

The transparent blue rectangle is the getitemrect drawn, and the solid blue is the get_item_rect of the scripted node. As you can see, the transparent rectangle is translated and rotated the same way as the node. But the solid one just stays where it was created, even if the actual node is translated. Help?

asked Nov 13, 2016 in Engine by Suchaaver Chahal (23 points)

Not a solution but just a comment:
Area2D (Node for overlap detection) does not use physics by default and have a wide range of signals to work with other areas, making them easy to work with, and could be faster than a custom rect2 or a CanvasItem property mess.
Plus the possibility to have many shapes per area, many layers and masks.

Using areas you can simplify a lot the logic (also, areas can be children of sprites if you want).

So if I create an Area2D as a child in each of the nodes, how would I proceed? I create the node in the script, create the Area2D as the child, but I would need to set the Area2D size to be the same as the texture right? And then I would connect some signals to make the magic happen?

Your options could be:

Sprite
|-Area2D

Or (most common one)

Area2D
|-Sprite

Area will need a shape (or many) to detect other areas, and that shape can be of any type (rectangle, circle, polygon) and size.

Then you will have to connect the area to the node with the script (for area_enter or exit).

That Area+Sprite+script+connections structure could be a packed scene with everything ready to instance, then modify with procedurally generated data (modify texture, position, shapes) and add to the tree or self modify when enters the tree, depending the design.

Alright, so I actually created a test scene just to get understand basic area functionality, but I think I'm failing.

I've created a hierarchy like so: Hierarchy

And the code for the top area2D is very simply:

extends Area2D

func _ready():
    connect("mouse_enter", self, "eee")

func eee(who):
    print(who + "goo goo ")

and for reference, the editor scene is: scene

But for whatever reason, eee is not called on mouse enter of the collisionShape (the collision shape has a rectangleShape as its shape property in the editor). And since I can't get this to work, certainly nothing else works, hehe.

[1]:

My bad, the above works just fine. mouse_enter doesn't pass a who parameter with my problem. It's been a long day of figuring out basic collisions ....

So, in the end it was my control nodes that were really messing up my interactions with my physics areas. Not sure why, but if they overlap it causes problems. Thanks @eons for explaining to me some of the structure of areas to start off.

Please log in or register to answer this question.

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.