Game Development Reference
In-Depth Information
Similarly, we can test if two vectors are not equal using the overloaded
not equals operator:
if( u != v ) return true;
Note: When comparing floating-point numbers, care must be taken
because, due to floating-point imprecision, two floating-point numbers
that we expect to be equal may differ slightly; therefore, we test if they
are approximately equal. We do this by defining an EPSILON constant,
which is a very small value that we use as a “buffer.” We say two val-
ues are approximately equal if their distance is less than EPSILON .In
other words, EPSILON gives us some tolerance for floating-point
imprecision. The following function illustrates how EPSILON can be
used to test if two floating-point values are equal:
const float EPSILON = 0.001f;
bool Equals(float lhs, float rhs)
{
// if lhs == rhs their difference should be zero
return fabs(lhs - rhs) < EPSILON ? true : false;
}
We do not have to worry about doing this when using the D3DXVEC-
TOR3 class, as its overloaded comparison operations will do this for us,
but comparing floating-point numbers properly is important to know in
general.
Computing the Magnitude of a Vector
Geometrically, the magnitude of a vector is the length of the directed
line segment. Given the components of a vector, we can algebraically
compute its magnitude with the following formula:
(1)
2
2
2
u
u
u
u
x
y
z
The double vertical bars in u denotes the magnitude of u .
Example: Find the magnitude of the vectors u = (1, 2, 3) and v = (1, 1).
Solution:For u we have:
(2)
2
2
2
u
1
2
3
1
4
9
14
Generalizing formula (1) to two dimensions, for v we have:
2
2
(3)
v
1
1
2
Using the D3DX library, we can compute the magnitude of a vector
using the following function:
Search WWH ::




Custom Search