DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Build A Quaternion From A Rotation About An Arbitrary Axis

07.17.2007
| 8374 views |
  • submit to reddit
        // Build a quaternion from a rotation about an arbitrary axis

/*!
* Compute a quaternion from axis and angle.
*
* \param c Computed quaternion (this is just a float[4])
* \param axis Rotation axis (this is just a float[3])
* \param angle Angle of rotation, radians.
*/
void lib3ds_quat_axis_angle(Lib3dsQuat c, Lib3dsVector axis, float angle)
{
    double omega, s;
    double l = sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);

    omega = -0.5 * angle;
    s = sin(omega) / l;

    c[0] = (float)s * axis[0];
    c[1] = (float)s * axis[1];
    c[2] = (float)s * axis[2];
    c[3] = (float)cos(omega);
}