Why do Rect2 intersections stop working when I rotate a Control node?

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

Control nodes screenshot

I’m trying to create an inventory like in Resident Evil 4 with Control nodes. So I’m trying to implement rotation with the drag and drop functions in Control nodes

I keep a reference to the itemPreview and then do a rotation by 90 degrees everytime a button is pressed:

itemPreview.set_rotation(itemPreview.get_rotation() + deg2rad(90))

Now in the location where I’m dropping the item, I do some intersect tests between the item i’m dragging and all the other item rects. Everything works correctly before I rotate anything. After rotating, it seems to still be using the old rect positions(?) I’m not sure exactly what’s going on. I hope the screenshot I included helps. And here’s the code for testing whether I can drop an item or not:

func can_drop_data(_pos, data):
var canDrop = true

var items = get_children()

for item in items:
	if item == data.item:
		continue
	if item.get_global_rect().intersects(data.itemPreview.get_global_rect()):
		canDrop = false

return canDrop

screenshot link if it didn’t work: https://imgur.com/a/nwS3x2m

:bust_in_silhouette: Reply From: exuin

Looking at the docs for Rect2, rotation isn’t mentioned at all. I think the rectangles aren’t capable of being rotated. Unfortunately you might have to use Area2Ds for this.

Yeah the rotation seems to be part of the Control nodes. Although the rotation variable is listed in the Rect section of the editor when using Control nodes…

I did find a workaround by switching to TextureRects and just having two textures. A vertical one and a horizontal one. When you press the rotate button I just swap between the two. I don’t rotate anything, the Rect size just changes to fit the texture. I then change the Rect size to be (0, 0) so the box shrinks to match the size of the texture. This fixes all of my intersection issues.

I am considering just using Node2Ds instead for the inventory though… I would have to do the drag and drop stuff myself instead of using the Control functions but that’s fairly easy. And I guess I can just instance a popup menu button on the Area2Ds for an options menu.

Anyways, thank you for answering.

lifeman100 | 2021-05-25 09:46