I was unfortunate enough to attend a high school that did not care about preparing students for college, but I'm studying some game programming, and everything programming I get, but I'm taking a Game AI course and we're learning about FSM's and AI movements, and I came across "normalize", looked into it and it kind of makes sense, but how would I use this in game programming? Maybe I just don't understand the concept that well (never really took an algebra class, although I use way higher level mathematics everyday in other programming, however I learn as I go), but I just don't get why I would need to do that.
Vectors represent both length and direction. Sometimes it's handy to use a "unit vector" (with length one) for multiplication, because it's easy to control the result's length.
Multiply a one-pound weight by five, and what do you have? A five-pound weight.
Multiply a unit vector by five, and what do you have? A vector pointing five units in that direction.
As a practical example, let's say I'm writing a camera system for a simple game: two cars race against each other on a straight track. The camera should orbit around the player's car while looking in the direction of the opponent.
Since you tagged the question with unity3d, I'll offer C# code that would compile in Unity3D. The basic concepts are the same for any use, though.
Let's assume we're given two points:
Vector3 playerPos, enemyPos;
Some operations we might find useful:
//find separation between two points
Vector3 playerToEnemy = enemyPos - playerPos;
//find distance between them
float distance = playerToEnemy.magnitude;
//find direction of separation
Vector3 enemyDirection = playerToEnemy.normalized;
Finally, we position the camera:
Vector3 camPos = playerPos; //start at player's position
camPos -= enemyDirection * 10f; //move ten units away from enemy
camPos += Vector3.up * 5f; //move five units up
Transform tr = Camera.main.transform;
tr.position = camPos;
tr.LookAt(enemyPos);
You may notice that I used one of Unity3D's built-in unit vectors (including Vector3.up
, Vector3.right
, and Vector3.forward
). Every transform object in Unity also has local-space equivalents.
Another use: with vector dot products, you can check if two normalized vectors point in similar directions because the dot product's output will range from -1 (opposite directions) to 0 (perpendicular) to 1 (parallel).