Game Development Reference
In-Depth Information
/** Gets the squared magnitude of this vector. */
real squareMagnitude() const
{
return x*x+y*y+z*z;
}
/** Turns a non-zero vector into a vector of unit length. */
void normalize()
{
real l = magnitude();
if (l > 0)
{
(*this)*=((real)1)/l;
}
}
};
Notice that I've also added a function to calculate the square of the magnitude of
a vector. This is a faster process because it avoids the call to sqrt , which can be slow
on some machines. There are many cases where we don't need the exact magnitude
and where the square of the magnitude will do. For this reason it is common to see a
squared magnitude function in a vector implementation.
2.1.3
S CALAR AND V ECTOR M ULTIPLICATION
In the normalization equations I have assumed we can multiply a scalar (1 /d )bya
vector. This is a simple process, given by the formula
x
y
z
kx
ky
kz
=
k a
=
k
In other words we multiply a vector by a scalar by multiplying all the components of
the vector by the scalar.
To divide a vector by a scalar, we make use of the fact that
1
b
a
÷
b
=
a
×
so
1
k a
which is how we arrived at the normalization equation 2.2 from equation 2.1.
a
k =
Search Nedrilad ::




Custom Search