Why is the angle off by 30 degrees?

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

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,

Per

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

Ertain | 2019-10-22 06:10

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

permal | 2019-10-22 07:05

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

gmaps | 2019-10-22 08:02

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.

wombatstampede | 2019-10-22 08:34

:bust_in_silhouette: Reply From: permal

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.