It’s possible that I’ve skipped too far ahead in trying to understand game math and c# (and need some more basics before really “getting” this), but I’m having trouble understanding the value in normalizing vectors and what they are useful for. I’ve been following a “Math for Game Devs” YouTube series, and it seems well-done (though it is done in C++, not C#), but I’m still stuck without a solid foundation on the value of normalized vectors. youtubeDOTcom/watch?v=hh-3xLawoYo&list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My&index=6
So a normalized vector apparently turns a magnitude of x into a magnitude of 1 (or 0). And apparently this is useful to get things like a character’s direction. But why bother normalizing it, why not just use the actual magnitude? Is the simplification useful?
Maybe somebody can give me a simple but meaningful answer, like in the context of a game like a platformer. Thanks in advance.
As with all things, it depends on what you’re trying to do.
A less mathematically-critical case you could make for normalizing a Vector, is that it becomes easily digestible and clear to understand. Let’s say for a moment that I have two vectors in the same direction but of different magnitude
A = Vector3.new(0,-1,0.5)
B = Vector3.new(0,-87.4,43.7)
Visualizing A and manipulating A is less cumbersome than B.
There are some cases when you break away from the standard Cartesian business where normalizing vectors has another suite of conveniences, but googling that can tell you more than I am willing to type.
To sum it up…
When I only care about direction, it wouldn’t mean much to me if you told me that you have a line going 7 km South. Just tell me South and destroy the magnitude. After doing this over and over and over you can see why people termed the action “normalizing” vectors.
Normalized vectors are used a lot in lighting equations. Where distance may have no meaning.
Also it can be an optimization when needing calculate distance. As mention earlier if you want to move from a to b by .5.
You would need to know the magnitude of the vector to calculate this. Which would require a sqrt.
As well as a division and multiply. Now if you do more math with this vector over and over you must repeat this. Even if you saved the distance you must do a multiply and divide every time.
When you could just do the division once and normalize the vector.
Then you only need to do a multiply.
It also is easier to give to other code as a standard and the library would not have to redo extra work.
My guess is you just haven’t done enough with them to see the usefulness. But keep going!
Also a lot of the math gets simplified when you can just divide by 1.
Okay so, when a Vector is normalized, its magnitude is equal to 1. I know that magnitude isn’t important in the case of normalization - direction is - but will that magnitude of 1 then be, in the case of Unity, 1 unit length of Unity space (since normalized vectors are also called "unit-length vectors), whatever the direction it is facing? EDIT: Or maybe it actually does make sense? If you have a magnitude of 5, would you want 1/5 of that magnitude but keep the same direction? At first I was thinking it might be possible for the 1 to be relative to that Vector (like 1 “part” of the Vector, if you divided it into x parts), but thinking some more, I don’t think that really makes sense.
(Again I appreciate the attempts to help me; I don’t think I’ll really get it until I need it, so I dunno if it’s worth keeping the conversation going or not. This video is good though:
That’s exactly what happens. You could manually do the math and divide all the components of a vector by its magnitude. That would scale the components to values that make up the normalized vector.
Examples have already been given, and the first one will be useful in a lot of situations.
You can find this scheme everywhere, for example, when you’ve been given a price for N pieces and want to calculate it for 1 piece or X pieces.
This should be familiar:
20 pieces => $15
1 piece => $15/20 = $0.75
x pieces => x * $0.75
So imagine you’ve got a direction and you could calculate what it takes to move a distance of 1 unit into that direction - that’s normalized. Once you’ve done that, you can scale this to any length.
So here is just one basic and practical example of many situations where this becomes extremely useful:
Writing movement logic:
Key1 makes the character move in X direction
Key2 makes the character move in Z direction
Key1 OR Key2 is pressed => (1,0,0) OR (0,0,1).
That’s fine, we’re moving 1 unit * movement speed.
BOTH are pressed => (1,0,1)
Whoops - your character does move faster, as it travels a distance of sqrt(2) * movement speed.
Solution: Normalize the direction before you do any other movement logic so that you calculate your character’s position based on a direction vector of magnitude 1.