how to rotate a vector then rotate it back without it gaining extra value?

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

So what I am doing is rotation an area2d then rotation a vector2 based on the rotation of the area2d, then i rotate the vector back acording to the rotation of the area2d * -1. But the final result has has changed from what it was firts. So I dont know if thats just something on how the math works or what.
How can I fix this so that the final vector2 is the same as the first one after it has been rotated and rotated back again by the same amount?

Hopefully the video can explain it better than I can :wink:

the code-
extends Area2D

var vector1 = Vector2(0, -100)
var vector2 = Vector2(0, 0)
var turn = 0.0

Called when the node enters the scene tree for the first time.

func _ready():

print("ready")

func _process(delta):

if Input.is_action_pressed("right"):
	turn = clamp(turn + 1, -1, 1)

if Input.is_action_pressed("left"):
	turn = clamp(turn + -1, -1, 1)

rotation += turn
turn = 0.0

vector2 = vector1.rotated(get_rotation())
#print(vector2)
print(vector2.rotated(get_rotation() * -1))
:bust_in_silhouette: Reply From: jgodfrey

It’s not entirely clear what the question here is, but I think you’re commenting on the following phenomenon:

  • Your vector1 var starts out as 0, -100
  • Now, you rotate it by 0 radians with vector2 = vector1.rotated(get_rotation())
  • That results in the vector having a small, floating point error ((-0.000004, -100) )

So, if the question is why is a vector, rotated by 0 radians not exactly the original vector…

That’s just an extremely small floating point error - typical in anything involving floating point values.

That’s effectively the original vector and should not cause you any actual issues. However, you never want to, for example, make any direct equality checks against floating point numbers - especially if those values are the result of mathematical functions.

There are mechanisms available to assist you with such equality comparisons. For example, see is_equal_approx().