Game Development Reference

In-Depth Information

b
, and so is often

called the “cross product.” The vector product is calculated with the formula

The vector product is indicated by a multiplication sign,
a

×

⎡

⎣

⎤

⎦
×

⎡

⎣

⎤

⎦
=

⎡

⎣

⎤

⎦

a
x

a
y

a
z

b
x

b
y

b
z

a
y
b
z
−

a
z
b
y

a

×

b

=

a
z
b
x

−

a
x
b
z

[2.5]

a
x
b
y
−

a
y
b
x

This is implemented in our vector class in this way:

Excerpt from include/cyclone/core.h

class Vector3

{

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

/**

* Calculates and returns the vector product of this vector

* with the given vector.

*/

Vector3 vectorProduct(const Vector3 &vector) const

{

return Vector3(y*vector.z-z*vector.y,

z*vector.x-x*vector.z,

x*vector.y-y*vector.x);

}

/**

* Updates this vector to be the vector product of its current

* value and the given vector.

*/

void operator %=(const Vector3 &vector)

{

*this = vectorProduct(vector);

}

/**

* Calculates and returns the vector product of this vector

* with the given vector.

*/

Vector3 operator%(const Vector3 &vector) const

{

return Vector3(y*vector.z-z*vector.y,

z*vector.x-x*vector.z,

x*vector.y-y*vector.x);