Godot Version
4.2
Question
I’m having problems to position the object’s center to the mouse cursor. It’s like the paper game “Battlefields”: you have a grid and place ships on the grid. They need to be horizontal or vertical, diagonals are not allowed. The ship class is a Node2D because I draw a rectangle on it. On that rectangle is the Sprite2D with the actual ship and an Area2D with Collider to get the mouse events. When I click one ship, it gets selected and follows the mouse (this works so far).
But now, when I hit the space bar I rotate the ship:
func rotate_ship(angle: int):
print("Rotation: ", a)
print("Global Position: ", global_position)
print("Position: ", position)
rotation = deg_to_rad(a)
global_position.x = get_global_mouse_position().x #- (x * PX / 2)
global_position.y = get_global_mouse_position().y #- (y * PX / 2)
print("Mouse position: ", get_global_mouse_position())
“angle” starts with 0 and is incremented with each space bar hit by 90, so “Rotation” prints 90, 180, 270, 0. This works as well, but the ship is no longer centered to the mouse cursor. As per the debug messages the global_position (I tried “position” also) is not changed, I even tried “+400” and it didn’t change.
Debug messages (e.g.):
Rotation: 180
Global Position: (242, 265)
Position: (242, 265)
Mouse position: (258, 377)
Rotation: 270
Global Position: (242, 265.0002)
Position: (242, 265.0002)
Mouse position: (258, 377)
Rotation: 0
Global Position: (242, 265)
Position: (242, 265)
Mouse position: (258, 377)
Any ideas how I can make the ship to always be centered to the current position of the mouse cursor? Only Rotation = 0 is ok, in all other rotations the Area2D doesn’t get the mouse clicked event. And just for my understanding: why can’t I just write:
position = get_global_mouse_position() + Vector2(x/2, y/2) #Offset