Game Development Reference
In-Depth Information
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);
}
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,
q.n*v.z + q.v.y*v.x - q.v.x*v.y);
}
MakeQFromEulerAngles
This function constructs a quaternion from a set of Euler angles.
For a given set of Euler angles, yaw (ψ), pitch (τ), and roll (φ), defining rotation about
the z-axis, then the y-axis, and then the x-axis, you can construct the representative
rotation quaternion. You do this by first constructing a quaternion for each Euler angle
and then multiplying the three quaternions following the rules of quaternion multipli‐
cation. Here are the three quaternions representing each Euler rotation angle:
q roll = [cos(φ/2), (sin(φ/2)) i + 0 j + 0 k ]
q pitch = [cos(τ /2), 0 i + (sin(τ /2)) j + 0 k ]
q yaw = [cos(ψ /2), 0 i + 0 j + (sin(ψ /2)) k ]