0 votes

Placing a Label at the center of a 2D-window with the following code does not give me the angle I expect. It's obviously me not understanding something, so I would appreciate if someone could point me in the right direction.

extends Label

func _process(delta):
    var angle_to_mouse = get_rect().position.angle_to(get_local_mouse_position())
    text = str(rad2deg(angle_to_mouse))

What I expected was an angle of ~0 when the mouse pointer is to the right of the label, and 180 degrees when to the left.

What I get is 0 degrees when the mouse pointer is below and to the right of the label, and 180 degrees when the pointer is up to the left of the label, i.e. towards (0,0). Straight to the left of the label its at 150 degrees and -30 degrees straight to the right.

Why is is?

Thanks in advance,


in Engine by (23 points)

Could it be due to the Y-axis being flipped?

I doubt it, that'd switch it a full 180 degrees, not offset it by 30.

Try printing getrect().position, getlocalmouseposition() and angle in degrees for both situations (and maybe screenshots for them) and post it here, it'll be easier to debug.

Try to output the mouse position and rect position via a timer (or something else which is slower than each _process). Just to verify that the two coordinate systems are compatible. Maybe there's some scaling/sizing going on or relative coordinates of the label.

1 Answer

0 votes
Best answer

Ok, I figured out what I was doing wrong:

First, the from is Vector2(0,0), since I wanted the angle to the mouse from a component itself.
Secondly, I need to use angle_to_point, not angle_to.

by (23 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.