Game Development Reference

In-Depth Information

2.1.6

T
HE
C
OMPONENT
P
RODUCT

The most obvious product is the least useful: the component product, written in this

book as

(it does not have a universal standard symbol the way the other products

do). It is used in several places in the physics engine, but despite being quite obvious,

it is rarely mentioned at all in topics about vector mathematics.

This is because it doesn't have a simple geometric interpretation: if the two vectors

being multiplied together represent positions, then it isn't clear geometrically how

their component product is related to their locations. This isn't true of the other types

of product.

The component product is formed in the same way as vector addition and

subtraction—by multiplying each component of the vector together:

◦

⎡

⎣

⎤

⎦
◦

⎡

⎣

⎤

⎦
=

⎡

⎣

⎤

⎦

a
x

a
y

a
z

b
x

b
y

b
z

a
x
b
x

a
y
b
y

a
z
b
z

a

◦

b

=

Note that the end result of the component product is another vector. This is exactly

the same as for vector addition and subtraction, and for multiplication by a scalar: all

end up with a vector as a result.

Because it is not commonly used, we will implement the component product as a

method rather than an overloaded operator. We will reserve overloading the
*
opera-

tor for the next type of product. The method implementation looks like this:

Excerpt from include/cyclone/core.h

class Vector3

{

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

/**

* Calculates and returns a component-wise product of this

* vector with the given vector.

*/

Vector3 componentProduct(const Vector3 &vector) const

{

return Vector3(x * vector.x, y * vector.y, z * vector.z);

}

/**

* Performs a component-wise product with the given vector and

* sets this vector to its result.

*/

void componentProductUpdate(const Vector3 &vector)

{