When to Normalize a Vector?
I am learning XNA and in almost all of the educational kits found on http://creators.xna.com/en-US/. I always see a call to Normalize() on a vector. I understand that normalize basically converts the vector into unit length, so all it gives is direction.
Now my question is When to normalize and what exactly does it help me in. I am doing 2D programming so please explain in 2D concepts and not 3D.
EDIT: Here is code in the XNA kit, so why is the Normalize being called?
if (currentKeyboardState.IsKeyDown(Keys.Left)
|| currentGamePadState.DPad.Left == ButtonState.Pressed)
{
catMovement.X -= 1.0f;
}
if (currentKeyboardState.IsKeyDown(Keys.Right)
|| currentGamePadState.DPad.Right == ButtonState.Pressed)
{
catMovement.X += 1.0f;
}
if (currentKeyboardState.IsKeyDown(Keys.Up)
|| currentGamePadState.DPad.Up == ButtonState.Pressed)
{
catMovement.Y -= 1.0f;
}
if (currentKeyboardState.IsKeyDown(Keys.Dow开发者_StackOverflown)
|| currentGamePadState.DPad.Down == ButtonState.Pressed)
{
catMovement.Y += 1.0f;
}
float smoothStop = 1;
if (catMovement != Vector2.Zero)
{
catMovement.Normalize();
}
catPosition += catMovement * 10 * smoothStop;
In your example, the keyboard presses give you movement in X or Y, or both. In the case of both X and Y, as when you press right and down at the same time, your movement is diagonal. But where movement just in X or Y alone gives you a vector of length 1, the diagonal vector is longer than one. That is, about 1.4 (the square root of 2).
Without normalizing the movement vector, then diagonal movement would be faster than just X or Y movement. With normalizing, the speed is the same in all 8 directions, which I guess is what the game calls for.
One common use case of vector normalization when you need to move something by a number of units in a direction. For example, if you have a game where an entity A moves towards an entity B at a speed of 5 units/second, you'll get the vector from A to B (which is B - A
), you'll normalize it so you only keep the direction toward the entity B from A's viewpoint, and then you'll multiply it by 5 units/second. The resulting vector will be the velocity of A and you can then simply multiply it by the elapsed time to get the displacement by which you can move the object.
It depends on what you're using the vector for, but if you're only using the vectors to give a direction then a number of algorithms and formulas are just simpler if your vectors are of unit length.
For example, angles: the angle theta
between two unit vectors u
and v
is given by the formula cos(theta) = u.v
(where .
is the dot product). For non-unit vectors, you have to compute and divide out the lengths: cos(theta) = (u.v) / (|u| |v|)
.
A slightly more complicated example: projection of one vector onto another. If v
is a unit vector, then the orthogonal projection of u
onto v
is given by (u.v) v
, while if v
is a non-unit vector then the formula is (u.v / v.v) v
.
In other words: normalize if you know that all you need is the direction and if you're not certain the vector is a unit vector already. It helps you because you're probably going to end up dividing your direction vectors by their length all the time, so you might as well just do it once when you create the vector and get it over with.
EDIT: I assume that the reason Normalize is being called in your example is so that the direction of velocity can be distinguished from the speed. In the final line of the code, 10 * smoothStop
is the speed of the object, which is handy to know. And to recover velocity from speed, you need to multiply by a unit direction vector.
You normalize any vector by dividing each component by the magnitude of the vector, which is given by the square root of the sum of squares of components. Each component then has a magnitude that varies between [-1, 1], and the magnitude is equal to one. This is the definition of a unit vector.
精彩评论