Game Development Reference

In-Depth Information

x *= vector.x;

y *= vector.y;

z *= vector.z;

}

};

2.1.7

T
HE
S
CALAR
P
RODUCT

By far the most common product of two vectors is the “scalar product.” It is different

from any of our previous vector operations in that its result is not a vector but rather

a single scalar value (hence its name). It is written using a dot symbol:
a

b
,andso

is often called the “dot product.” For reasons beyond this topic it is also called, more

mathematically, the “inner product”—a term I will not use again.

Thedotproductiscalculatedwiththeformula

·

⎡

⎣

⎤

⎦
·

⎡

⎣

⎤

⎦
=

a
x

a
y

a
z

b
x

b
y

b
z

a

·

b

=

a
x
b
x
+

a
y
b
y
+

a
z
b
z

[2.3]

In my vector class I have used the multiplication operator
*
to represent the dot prod-

uct (it looks quite like a dot, after all). We could overload the dot operator, but in most

C-based languages it controls access to data within an object, and so overloading it is

either illegal or a dangerous thing to do.

The scalar product methods have the following form:

Excerpt from include/cyclone/core.h

class Vector3

{

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

/**

* Calculates and returns the scalar product of this vector

* with the given vector.

*/

real scalarProduct(const Vector3 &vector) const

{

return x*vector.x + y*vector.y + z*vector.z;

}

/**

* Calculates and returns the scalar product of this vector

* with the given vector.

*/