How to handle mouse clicks on 2d object after its rotation around some point?

Godot Version

3.6

Question

So, I’m creatin my ColorRect object, then I’m rotatin it around some other point at some random angle, and then my mouse clicks are not workin. But I want to be able to click on this rect after rotation. How? I guess, I should somehow update rect’s position. I know I can use RotationMatrix for this, but, it’s not workin also. My code:

extends Node2D

var color_rect: ColorRect = null


func _ready():
	color_rect = ColorRect.new()
	color_rect.color = Color.red
	color_rect.rect_size = Vector2(100, 100)
	color_rect.rect_position = Vector2(200, 200)
	# choose some rotation point not inside our rect:
	var rect_pivot_vec: Vector2 = Vector2(500, 500)
	color_rect.rect_pivot_offset = rect_pivot_vec
	add_child(color_rect)


func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed:
			var mouse_pos: Vector2 = event.position
			if color_rect.get_rect().has_point(mouse_pos):
				print("clicked on rect!")


func _physics_process(delta):
	var deg = color_rect.get_rotation_degrees()
	if deg < 45:
		color_rect.rect_rotation += 20 * delta

I tried your code in Godot 4 and I got it working by using gui_input signal. I think it should work in Godot 3.6 like this (I marked changes with comments):

extends Node2D

var color_rect: ColorRect = null


func _ready():
	color_rect = ColorRect.new()
	color_rect.color = Color.red
	color_rect.rect_size = Vector2(100, 100)
	color_rect.rect_position = Vector2(200, 200)
	# choose some rotation point not inside our rect:
	var rect_pivot_vec: Vector2 = Vector2(500, 500)
	color_rect.rect_pivot_offset = rect_pivot_vec
	add_child(color_rect)
	# Connected gui_input signal
	color_rect.connect("gui_input", self, "_on_gui_input")

# Renamed this function
func _on_gui_input(event):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed:
			var mouse_pos: Vector2 = event.position
			# Removed this line
			#if color_rect.get_rect().has_point(mouse_pos):
			print("clicked on rect!")


func _physics_process(delta):
	var deg = color_rect.get_rotation_degrees()
	if deg < 45:
		color_rect.rect_rotation += 20 * delta
1 Like

Big thanks! It’s indeed also working in godot 3.6!

1 Like

Yeah, it’s working. But can I ask you again one thing, please? What if, I have several such ColoRect’s and I want to get the clicked one inside _on_gui_input func? Cause I need to do some stuff with clicked one.

Okay, I think, I figured it out:

extends Node2D

var color_rect: ColorRect = null


func _ready():
	color_rect = ColorRect.new()
	color_rect.color = Color.red
	color_rect.rect_size = Vector2(100, 100)
	color_rect.rect_position = Vector2(200, 200)
	# choose some rotation point not inside our rect:
	var rect_pivot_vec: Vector2 = Vector2(500, 500)
	color_rect.rect_pivot_offset = rect_pivot_vec
	add_child(color_rect)
	# Connected gui_input signal
	color_rect.connect("gui_input", self, "_on_gui_input", [color_rect])


func _on_gui_input(event, color_rect):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed:
			print("clicked on rect!")
			color_rect.color = Color.webgreen


func _physics_process(delta):
	var deg = color_rect.get_rotation_degrees()
	if deg < 45:
		color_rect.rect_rotation += 20 * delta
1 Like