Game Development Reference
In-Depth Information
9.4.11
U PDATING BY THE A NGULAR V ELOCITY
The final operation we need to do is to update the orientation quaternion by applying
the angular velocity for a specified duration of time. In section 9.3 we saw that this is
handled by the equation
δt
2 ω
θ
ˆ
=
θ
ˆ
+
θ
ˆ
ˆ
where ω
ˆ
is the quaternion form of the angular velocity and t is the duration to update
by. This can be implemented as
Excerpt from include/cyclone/core.h
/**
* Holds a three degree of freedom orientation.
*/
class Quaternion
{
// ... other Quaternion code as before ...
/**
* Adds the given vector to this, scaled by the given amount.
* This is used to update the orientation quaternion by a rotation
* and time.
*
* @param vector The vector to add.
*
* @param scale The amount of the vector to add.
*/
void addScaledVector(const Vector3& vector, real scale)
{
Quaternion q(0,
vector.x * scale,
vector.y * scale,
vector.z * scale);
q *= *this;
r += q.r * ((real)0.5);
i += q.i * ((real)0.5);
j += q.j * ((real)0.5);
k += q.k * ((real)0.5);
}
};