Game Development Reference

In-Depth Information

Conjugate: The ~ operator

The conjugate of the product of quaternions is equal to the product of the quaternion

conjugates, but in reverse order:

~(
qp
) = (~
p
)(~
q
)

Here's the code that computes the conjugate for our
Quaternion
class:

Quaternion operator~(void) const { return Quaternion( n,

-v.x,

-v.y,

-v.z);}

QVRotate

This function rotates the vector
v
by the unit quaternion
q
according to this formula:

p
' = (
q
)(
v
)(~
q
)

Here, ~
q
is the conjugate of the unit quaternion,
q
:

inline Vector QVRotate(Quaternion q, Vector v)

{

Quaternion t;

t = q*v*(~q);

return t.GetVector();

}

This operator takes the conjugate of the quaternion, ~
q
, which is simply the negative of

the vector part. If
q
= [
n
,
x
i
+
y
j
+
z
k
], then ~
q
= [
n
, (−
x
)
i
+ (−
y
)
j
+ (−
z
)
k
].

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

Here's the code that multiplies two
Quaternion
s,
q1
and
q2
: