views:

392

answers:

5

Just curious, I am reading a book on game AI. And one of the terms that is being used is to normalized a vector. Which is to turn a vector into units. To do so you must divide each dimension x,y and or z by its magnitude. We must we turn a vector into units before we do anything with it. Why ??. and could anyone give a scenario's . Thanks!

A: 

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.

smaclell
+2  A: 

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

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

alt text

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.

duffymo
would you recommend any resources or books I should look into. Thanks
numerical25
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.
duffymo
+1 And also means those unit vectors (i,j,k) form an orthonormal basis whether in local space or world space.
zebrabox
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 ;)
kigurai
and by orthogonal space I obviously mean orthonormal space, which is even more nice and comfortable :)
kigurai
@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?
duffymo
@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 :)
BlueRaja - Danny Pflughoeft
@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.
kigurai
@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.
kigurai
@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.
duffymo
+5  A: 

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(u*v). If they're not unit vectors, the angle is arccos(u*v/(|u| |v|)). In that case, you end up computing the norms of u and v anyway.

John D. Cook
I believe you need to normalize quaternion for rotation though.
afriza
@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
zebrabox
A: 

I need to get back into college, lol

numerical25
A: 

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.

James