Game Development Reference
In-Depth Information
Vec3 unit = v / length;
cout
Length=
<< length << newline;
cout
Unit vector: (X,Y,Z)=(
<< unit.x <<
,
<< unit.y <<
,
<< unit.z <<
)
<< newline;
The output generated would be:
Length=5.0
Unit vector (X,Y,Z): (0.6,0.8,0.0)
When we talk about dot-and-cross products, their inputs are almost always unit vec-
tors (also called normalized vectors). The formulas certainly work on any arbitrary
vector, but the results are relatively meaningless unless at least one of them is a unit
vector. Take the same formulas and apply unit vectors to them, and you ' ll find some
interesting results that you can use to calculate critical angles and directions in your
3D world.
A dot product of two vectors is a single floating-point number, sometimes called a
scalar. The cross product of two vectors is another vector. Remember these two
important facts, and you
ll never get one confused with the other again. Another
way to say this is dot products calculate angles and cross products calculate direction.
The dot product is calculated with the following formula:
'
float dotProduct = ( v1.x * v2.x ) + ( v1.y * v2.y ) + (v1.z * v2.z);
Unit vectors never have any coordinate with an absolute value greater than 1.0. Given
that, you
ll notice that the results of plugging various numbers into the dot product
formula have interesting effects. Assuming V1 and V2 are unit vectors:
'
n V1 equals V2: If you calculate the dot product of a vector with itself, the value
of the dot product is always 1.0.
n V1 is orthogonal to V2: If the two vectors form a right angle to each other and
they are the same length, the result of the dot product is always zero.
n V1 points in the opposite direction to V2: Two vectors of the same length
pointing exactly away from each other have a dot product of
1.0.
If this relationship between vectors, right angles, and the range [-1.0, 1.0] is stirring
some deep dark memory, you
re correct. The dark memory is trigonometry, and the
function you are remembering is the cosine. It turns out that you can use the dot
product of two unit vectors to calculate the angle between two vectors. For two unit
vectors a and b, the formula for calculating the angle between them is
'
ab
j a jj b j
¼ cos 1
 
Search WWH ::




Custom Search