Game Development Reference

In-Depth Information

}

};

A final, useful version of this is to update a vector by adding a scaled version of

another vector. This is simply a combination of vector addition and the multiplication

of a vector by a scalar:

⎡

⎤

⎡

⎤

⎡

⎤

+

a
x

a
y

a
z

b
x

b
y

b
z

a
x

cb
x

⎣

⎦
+

⎣

⎦
=

⎣

⎦

+

=

a

c
b

c

a
y
+

cb
y

a
z
+

cb
z

We could do this in two steps with the preceding functions, but having it in one place

is convenient:

Excerpt from include/cyclone/core.h

class Vector3

{

// ... Other Vector3 code as before ...

/**

* Adds the given vector to this, scaled by the given amount.

*/

void addScaledVector(const Vector3& vector, real scale)

{

x += vector.x * scale;

y += vector.y * scale;

z += vector.z * scale;

}

};

2.1.5

M
ULTIPLYING
V
ECTORS

Seeing how easy it is to add and subtract vectors may lull you into a false sense of

security.Whenwecometomultiplytwovectors together, life gets considerably more

complicated.

There are several ways of multiplying two vectors, and whenever we produce a

formula involving vector multiplication, we have to specify which type of multiplica-

tion to use. In algebra, for scalar values we can denote the product (multiplication)

of two values by writing them together with no intervening symbol (i.e.,
ab
means

a

×

b
). With vectors this usually denotes one type of multiplication that we need not

cover (the vector direct product—see a good mathematical encyclopedia for infor-

mation). I will not write
ab
but will show the kind of product to use with a unique

operator symbol.