Game Development Reference
In-Depth Information
FLOAT D3DXVec3Length( // Returns the magnitude.
CONST D3DXVECTOR3* pV // The vector to compute the length of.
);
D3DXVECTOR3 v(1.0f, 2.0f, 3.0f);
float magnitude = D3DXVec3Length( &v ); // = sqrt(14)
Normalizing a Vector
Normalizing a vector makes a vector's magnitude equal to one, which is
called a unit vector. We can normalize a vector by dividing each compo-
nent by the vector's magnitude, as shown here:
u
u uuuu
u
u
ˆ
y
x
,
,
z
We denote a unit vector by putting a hat over it: û .
Example : Normalize the vectors u = (1, 2, 3) and v = (1, 1).
Solution: From equations (2) and (3) we have u
14 and v
2, so:
u
1
2
3
ˆ
u
,
,
14
14
14
14
v
1
1
ˆ
v
,
2
2
2
Using the D3DX library, we can normalize a vector using the following
function:
D3DXVECTOR3 *D3DXVec3Normalize(
D3DXVECTOR3* pOut, // Result.
CONST D3DXVECTOR3* pV // The vector to normalize.
);
Note: This function returns a pointer to the result so that it can be
passed as a parameter to another function. For the most part, unless
otherwise stated, a D3DX math function returns a pointer to the result.
We will not explicitly say this for every function.
Vector Addition
We can add two vectors by adding their corresponding components
together; note that the vectors being added must be of the same
dimension:
uv
(
uvuvuv
,
,
)
x
x
y
y
z
z
Search WWH ::




Custom Search