Game Development Reference
In-Depth Information
Figure 8.9
Using vectors for blood spray.
by a scalar, the resulting vector length will be the same as the scalar value. If you
have a vector of some unknown length and you want it to be length 6, you can
normalize the vector and then multiply by 6.
public Vector Normalize(Vector v)
{
double r = v.Length();
if (r != 0.0)
// guard against divide by zero
{
return new Vector(v.X / r, v.Y / r, v.Z / r); // normalize and return
}
else
{
return new Vector(0, 0, 0);
}
}
This code is not technically correct; it should be impossible to normalize a zero
vector, but the code simply does nothing if a zero vector is normalized. Vectors
are normalized by calculating their length and then dividing each element by that
length. The effect of normalizing a vector can be seen in Figure 8.10.
In game programming, direction is often very important and normal vectors are
used to define directions. You may have heard the term normal mapping. This is
a texture where each pixel represents a normal vector, the texture is stretched
over a 3D model, and the lighting calculations take into account the normal
vector on each pixel of the rendered model. Normal mapping gives the surface of
the final model a lot more detail than it would otherwise have.
 
Search WWH ::




Custom Search