Game Development Reference
In-Depth Information
Figure 5 illustrates the geometric interpretation of vector addition.
Figure 5: Vector addition. Notice how we
translate v parallel to itself so that its tail
coincides with the head of u ; then the sum is
the vector originating at the tail of u and
ending at the head of the translated v .
To add two vectors in code, we use the overloaded addition operator:
D3DXVECTOR3 u(2.0f, 0.0f, 1.0f);
D3DXVECTOR3 v(0.0f, -1.0f, 5.0f);
// (2.0 + 0.0, 0.0 + (-1.0), 1.0 + 5.0)
D3DXVECTOR3 sum=u+v;//=(2.0f, -1.0f, 6.0f)
Vector Subtraction
Similar to addition, two vectors are subtracted by subtracting their cor-
responding components. Again, the vectors must be of the same
dimension.
(
uvu v
uvuvuv
,
,
)
x
x
y
y
z
z
Figure 6 illustrates the geometric interpretation of vector subtraction.
Figure 6: Vector subtraction
To subtract two vectors in code, we use the overloaded subtraction
operator:
D3DXVECTOR3 u(2.0f, 0.0f, 1.0f);
D3DXVECTOR3 v(0.0f, -1.0f, 5.0f);
D3DXVECTOR3 difference=u-v;//=(2.0f, 1.0f, -4.0f)
Search WWH ::




Custom Search