Hello.

First of all, I am aware that there would ideally be easier ways to operate what I am trying to do, but I feel like in my precise case, they wouldn't be practical, so please bear with me.

My issue is the following:

I currently have code that creates a bunch of rectangle2D in a chessboard pattern and draws them on screen. Rectangle 2d's are handy because they have both a position value and an end value, both of which are Vector2D's.

I would like to write code that allows me to know when the mouse is clicked inside one of those rectangles, and which of these it was.

More precisely, what I have in mind is a way to tell if the value returned by get_viewport().get_mouse_position() is located "inside" the position and end values of a given rectangle. I just can't seem to wrap my head around the maths to do that. Help would be appreciated.

Thanks in advance.

There's some simple math involved in finding out if point is inside a rectangle. However you don't even need that as Godot will do it for you. Rect2 class implements has_point() function that does precisely that.

var rect = Rect2(0.0, 0.0, 1.0, 1.0)  #initialize rect to needed values
var mouse = get_viewport().get_mouse_position()
if rect.has_point(mouse):
	print("INSIDE")
a year later