Game Development Reference

In-Depth Information

Quaternion Functions and Operators

The functions and overloaded operators that follow are useful when you are performing

operations with two quaternions, or with a quaternion and a scalar, or a quaternion and

a vector. Here, the quaternions are assumed to be of the type
Quaternion
, and vectors

of the type
Vector
, as discussed in
Appendix A
.

Quaternion Addition: The + Operator

This operator performs quaternion addition by simply adding the quaternion
q1
to

quaternion
q2
on a component-by-component basis:

inline Quaternion operator+(Quaternion q1, Quaternion q2)

{

return Quaternion( q1.n + q2.n,

q1.v.x + q2.v.x,

q1.v.y + q2.v.y,

q1.v.z + q2.v.z);

}

Quaternion Subtraction: The − Operator

This operator performs quaternion subtraction by simply subtracting the quaternion

q2
from quaternion
q1
on a component-by-component basis:

inline Quaternion operator-(Quaternion q1, Quaternion q2)

{

return Quaternion( q1.n - q2.n,

q1.v.x - q2.v.x,

q1.v.y - q2.v.y,

q1.v.z - q2.v.z);

}

Quaternion Multiplication: The * Operator

This operator performs quaternion multiplication according to the following formula:

q p
= n
q
n
p
−
v
q
•
v
p
+ n
q
v
p
+ n
p
v
q
+ (
v
q
×
v
p
)

Here,
n
q
n
p
−
v
q
•
v
p
is the scalar part of the result, while
n
q
v
p
+
n
p
v
q
+ (
v
q
×
v
p
) is the

vector part. Also note that
v
q
and
v
p
are the vector parts of
q
and
p
, respectively, • is the

vector dot-product operator, and × is the vector cross-product operator.

Quaternion multiplication is associative but not commutative; thus:

q
(
ph
) = (
qp
)
h

qp
≠
pq