Different values with direction_to and angle_to/rotated methods

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

I’m trying an alternative way to direct a shot in a football game using angle_to rather than direction_to:

Player.gd

var goal_target_position = ...

# current method:
var goal_direction = self.position.direction_to(goal_target_position)

# alternative method: calculate angle to target 
var goal_target_position_angle = self.position.angle_to(goal_target_position)
var goal_direction_from_angle = Vector2.UP.rotated(goal_target_position_angle)

# these are different?
print("goal_direction: ", goal_direction)
print("goal_direction_from_angle: ", goal_direction_from_angle)

…however, both goal_direction and goal_direction_from_angle are quite different and I don’t know why. Can anyone spot what I might be doing wrong? Thanks

:bust_in_silhouette: Reply From: Yuu

angle_to calculates the angle between two Vector2 assuming those vectors are lines, like this:
enter image description here

Whereas angle_to_point calculates the angle between two Vector2 assuming those vectors are points in space, like this:
enter image description here

So in your case, you probably want to use angle_to_point instead of angle_to.

Thank you, works beautifully now!

bizt | 2022-02-27 21:28