Game Development Reference
In-Depth Information
Here's the code that multiplies two Quaternion s, q1 and q2 :
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,
q1.n*q2.v.x + q1.v.x*q2.n
+ q1.v.y*q2.v.z - q1.v.z*q2.v.y,
q1.n*q2.v.y + q1.v.y*q2.n
+ q1.v.z*q2.v.x - q1.v.x*q2.v.z,
q1.n*q2.v.z + q1.v.z*q2.n
+ q1.v.x*q2.v.y - q1.v.y*q2.v.x);
}
Scalar Multiplication: The * Operator
This operator simply multiplies each component in the quaternion by the scalar s . There
are two forms of this operator, depending on the order in which the quaternion and
scalar are encountered:
inline Quaternion operator*(Quaternion q, float s)
{
return Quaternion(q.n*s, q.v.x*s, q.v.y*s, q.v.z*s);
}
inline Quaternion operator*(float s, Quaternion q)
{
return Quaternion(q.n*s, q.v.x*s, q.v.y*s, q.v.z*s);
}
Vector Multiplication: The * Operator
This operator multiplies the quaternion q by the vector v as though the vector v were a
quaternion with its scalar component equal to 0. There are two forms of this operator,
depending on the order in which the quaternion and vector are encountered. Since v is
assumed to be a quaternion with its scalar part equal to 0, the rules of multiplication
follow those outlined earlier for quaternion multiplication:
inline Quaternion operator*(Quaternion q, Vector v)
{
return Quaternion( -(q.v.x*v.x + q.v.y*v.y + q.v.z*v.z),
q.n*v.x + q.v.y*v.z - q.v.z*v.y,
q.n*v.y + q.v.z*v.x - q.v.x*v.z,
q.n*v.z + q.v.x*v.y - q.v.y*v.x);
}
inline Quaternion operator*(Vector v, Quaternion q)
{
return Quaternion( -(q.v.x*v.x + q.v.y*v.y + q.v.z*v.z),
q.n*v.x + q.v.z*v.y - q.v.y*v.z,
q.n*v.y + q.v.x*v.z - q.v.z*v.x,