Why is my angle calculation inconsistent in _process(delta)?

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

Hello,
I’m trying to make it so that a line aims at the mouse position while still rotating around a pivot that is not part of the line. To do so, I’ve made my own look_at_mouse() function that calculates the angle between the x axis and the mouse, and then the angle between the line aiming at the mouse through the pivot and the line aiming at the mouse through the line. The sum of the two gives me the exact angle that makes it so that the line aims at my mouse. However, when moving the mouse somewhat close to the line’s center, the angle starts going crazy and rotates back and forth for some reason.

I’ve got a MRE right here if you’d like to test it out: MRE

:bust_in_silhouette: Reply From: wyattb

Seems to work without the offset_angle added to rotation.

extends Sprite

func look_at_mouse():
	var mouse_pos = get_global_mouse_position()
	var mouse_angle = Vector2.RIGHT.angle_to(global_position - mouse_pos) # Angle between x axis and the line drawn from the pivot and the mouse_pos
	var offset_angle = (global_position - mouse_pos).angle_to($Line.global_position - mouse_pos) # The angle between (the line drawn from the pivot and the mouse_pos) and (the line drawn from the line center adn the mouse_pos)
	
	rotation = mouse_angle + offset_angle # Applying the sum of the two angles so that the line is the one actually aiming at the mouse and not the pivot

func _process(delta):
	look_at_mouse()

I need the line to meet with the mouse though, the offset_angle is the one that corrects the trajectory and incidentally, is the one that causes problems.

jaregax432 | 2021-06-15 22:52