How to manually normalize vectors into unit vectors?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MarkoH
:warning: Old Version Published before Godot 3 was released.

In the Godot documentation on Vector Math this is suggested to be performed as follows:

var a = Vector2(2,4)
var m = sqrt(a.x*a.x + a.y*a.y)
a.x /= m
a.y /= m

However I do not get lengths (or magnitudes) of 1 as I should. I’m quite sure this is due to my poor math skills. Or maybe it’s not supposed to be 1?

Could someone explain this to me? I’ve tried looking at other tutorials on the subject but there seems to be many ways to do this and with all of them I end up with different results and non of them equal to 1.

:bust_in_silhouette: Reply From: molamar

expand your script with :
print(a) # the vector itself
print(a.length()) # vector´s length

results should be:
Vectorposition : 0.44 and 0.89
Vectorlength : 1
all fine :wink:

change the vectors starting position to (1,1) and take also a paper and pen and draw it into coordinate system. take a closer look. what would you suggest the vectors length, if x is pointing one to the right and y one up? More the one i guess.
Next Questing would be if your are normalizing this Vector(1,1) to a lenght of one, how would these affect x and y? Try to do this on your corrdinate system by your own just with (how is this in englisch called? ;)) (triangle) ruler.
maybe also helpfull would be to take a look at Pythagoras’ theorem and trigonometry in general before starting to do some vector math.

Now I understood it!

So I was looking for 1 in the wrong place which would be in both x and y and not the actual magnitude of the unit vector.

So I needed to check the magnitude of the results I got. My brain was stuck a step behind the answer. I’m familiar with pythagorean theorem but not enough I guess.

From now on I will keep a pen and paper beside me when I’m trying to figure out vectors. And yes the tool is triangle ruler :slight_smile:

So thank you for clarifying this for me and also thank you for the handy print script in the start!

MarkoH | 2016-11-20 12:46