Why do we need a Unit Vector (in other words, why do we need to normalize vectors)?

27,649

Solution 1

You don't have to normalize vectors, but it makes a lot of equations a little simpler when you do. It could also make API's smaller: any form of standardization has the potential to reduce the number of functions necessary.

Here's a simple example. Suppose you want to find the angle between two vectors u and v. If they are unit vectors, the angle is just arccos(uv). If they're not unit vectors, the angle is arccos(uv/(|u| |v|)). In that case, you end up computing the norms of u and v anyway.

Solution 2

As John D. Cook says - mainly you're doing this because you care about the direction, not the vector itself. Depending on context, you more than likely don't want / need the magnitude information - just the direction itself. You normalize to strip away the magnitude so that it doesn't skew other calculations, which in turn simplifies many other things.

In terms of AI - imagine you take the vector V between P1(the AI bad guy) and P2 (your hero) as the direction for the bad guy to move. You want the bad guy to move at a speed N per beat - how do you calculate this? Well, we either normalize the vector each beat, multiply by N to figure out how far they moved, or we pre-normalize the direction in the first place, and just multiply the unit vector by N each time - otherwise the bad guy would move further if it were further away from the hero! If the hero doesn't change position, that's one less calculation to worry about.

In that context, it's not a big deal - but what if you have a hundred bad guys? Or a thousand? What if your AI needs to deal with combinations of bad guys? Suddenly it's a hundred or thousand normalizations you're saving per beat. Since this is a handful of multiplies and a square root for each, eventually you reach the point where not normalizing the data ahead of time means you're going to kill your AI processing rate.

More broadly - math for this is really common - people are doing here what they do for things like 3D rendering - if you didn't unitize, for instance, the normals for your surfaces, you'd have potentially thousands of normalizations per rendering which are completely unnecessary. You have two options: one - make each function perform the calculation, or two - pre-normalize the data.

From the framework designer's perspective: the latter is inherently faster - if we assume the former, even if your user thinks to normalize the data, they're going to have to go through the same normalization routine OR you're going to have provide two versions of each function, which is a headache. But at the point you're making people think about which version of the function to call, you may as well make them think enough to call the correct one, and only provide it in the first place, making them do the right thing for performance.

Solution 3

You are often normalizing a vector because you only care about the direction the vector points and not the magnitude.

A concrete scenario is Normal Mapping. By combining light striking the surface and vectors that are perpendicular to the surface you can give an illusion of depth. The vectors from the surface define the parallel direction and the magnitude to the vector would actual make calculations wrong.

Solution 4

We must we turn a vector into units before we do anything with it.

This statement is incorrect. All vectors are not unit vectors.

The vectors that form the basis for a coordinate space have two very nice properties that make them easy to work with:

  1. They're orthogonal
  2. They're unit vectors - magnitude = 1

This lets you write any vector in a 3D space as a linear combination of unit vectors:

alt text
(source: equationsheet.com)

I can choose to turn this vector into a unit vector if I need to by dividing each component by the magnitude

alt text
(source: equationsheet.com)

If you don't know that coordinate spaces or basis vectors are, I'd recommend learning a little more about the mathematics of graphics before you go much further.

Share:
27,649
numerical25
Author by

numerical25

Updated on June 20, 2020

Comments

  • numerical25
    numerical25 almost 4 years

    I am reading a book on game AI.

    One of the terms that is being used is to normalize a vector which is to turn a vector into a unit. To do so you must divide each dimension x, y and z by its magnitude.

    We must turn vector into a unit before we do anything with it. Why?

    And could anyone give some scenarios where we must use a unit vector?

    Thanks!

  • numerical25
    numerical25 about 14 years
    would you recommend any resources or books I should look into. Thanks
  • Afriza N. Arief
    Afriza N. Arief about 14 years
    I believe you need to normalize quaternion for rotation though.
  • duffymo
    duffymo about 14 years
    Any book on the fundamentals of 3D vectors will do. All computer graphics books will review them as well. It's more than just vectors. Unit vectors have to do with defining coordinate systems, and transformations are common, so you'll need to know a little about matricies.
  • zebrabox
    zebrabox about 14 years
    +1 And also means those unit vectors (i,j,k) form an orthonormal basis whether in local space or world space.
  • zebrabox
    zebrabox about 14 years
    @afriza : A quaternion is a different beast to a unit vector however you are right that the set of unit quaternions gives you the set of all rotations
  • Hannes Ovrén
    Hannes Ovrén about 14 years
    Just to be nit-picky: You can have a coordinate space where the unit vectors (the bases) are not orthogonal. You are referring to an orthogonal space, which is really a special case. Although it is a very nice and comfortable special case ;)
  • Hannes Ovrén
    Hannes Ovrén about 14 years
    and by orthogonal space I obviously mean orthonormal space, which is even more nice and comfortable :)
  • duffymo
    duffymo about 14 years
    @kiguarai, all the coordinate systems that I know about are orthogonal - rectangular, circular, elliptical, hyperbolic, etc. If I found one that wasn't orthogonal, I'll use Gram-Schmidt to make it so. Can you cite a common one that isn't orthogonal?
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft about 14 years
    @numerical25: Since no one seems to have explained the terminology: two (or more) vectors are orthogonal/perpendicular if the angle between them (all) is 90-degrees; they are orthonormal if they are orthogonal and also both (all) normalized. As for what a basis is... don't worry about that for now :)
  • Hannes Ovrén
    Hannes Ovrén about 14 years
    @duffymo: Not sure I could mention any "common" coordinate system. But to take a simple example: Let's assume you have a piece of metal in the shape of a parallellogram. If you want to adress a specific point on the surface I think using a base that is parallell to the sides, and thus non-orthogonal, would be abetter choice than first orthogonalizing the base.
  • Hannes Ovrén
    Hannes Ovrén about 14 years
    @BlueRaja: To be even more nit-picky, orthogonality is not defined by the angle, but from the innner product. In a euclidian vector space that is the same as the dot product being zero which means the angle is pi/2 = 90 degrees. So in that case, you are of course correct.
  • duffymo
    duffymo about 14 years
    @kigurai - Even that non-orthogonal example you cite would be better served with an orthongonal system and a Jacobean transformation, the way that finite elements use local and global coordinate systems.
  • Tone
    Tone about 11 years
    This answered this question the best for me. Great explanation!
  • MonsterMMORPG
    MonsterMMORPG almost 8 years
    hello where does John D. Cook say that i need to refer him