Changing origin/plane rotation of 2D node when using the angle_to_point() function

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

Hi,

I’ve been trying to create a system where, depending on the location and angle of the player’s mouse position relative to the sprite, the right or left arm is selected and is able to rotate.

Like in this gif:

The code is use is basically along the lines of:

If mouse is closer to left arm or right arm.

Select arm for rotation. (and how rotated the mouse is from the player) by using this line:

  var angle = rad2deg(self.global_position.angle_to_point(get_global_mouse_position()))

if angle >= 10 or angle <=-10:

  `arms_state = ARMS_STATE.ROTATE`

Rotate by using following function:

func arm_rotate_toward(arm,location: Vector2):

arm.rotation = lerp_angle(arm.rotation,arm.global_position.angle_to_point(get_global_mouse_position()),0.7)

This function is ran in the main loop of the node.

The issue I have is that, if the player is rotated, when the angle_to_point() finds an angle, it is as if the player’s rotation is still 0. I think this is because angle_to_point() uses the global origin plane of the scene rather than the plane the player is rotated at.

This gif tried to show whats going on:

By the time the player has rotated 180 degrees, it is as if the arm control is completely inverted (holding mouse right side of character will move left arm, and vice versa).

I have tried alerting this line

var angle = rad2deg(self.global_position.angle_to_point(get_global_mouse_position()))

to this line:

 var angle = rad2deg(self.global_position.angle_to_point(get_global_mouse_position()))+self.global_rotation_degrees

Which has helped me a little, but it hasn’t fixed the issue properly.

Does anyone know what I need to do to make angle_to_point take into account the rotation of a node?

Massive thanks in advance.