Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Quaternion - Axis/Angle

Started by Calefaction Mar 1, 2005 at 7:05 PM 2 replies 1.9k views
Original Post
Calefaction
Calefaction
Is it possible to take a rotation defined as a quaternion and extract the equivalent axis/angle rotation definition? If so, how would you go about doing this?
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
wendigo23
wendigo23
It is quite easy.

axis = quat.x, quat.y, quat.z
angle = 2 * arcos(quat.w)

You may want to normalize the axis, depending on your needs.
minorlogic
minorlogic
[code=cpp]

/**
create a unit quaternion rotating by axis angle representation
*/
inline void unit_from_axis_angle(const vector3& axis, const float& angle){
vector3 v(axis);
v.norm();
float half_angle = angle*0.5f;
float sin_a = (float)sin(half_angle);
set(v.x*sin_a, v.y*sin_a, v.z*sin_a, (float)cos(half_angle));
};
//-----------------------------------
/**
convert a quaternion to axis angle representation,
preserve the axis direction and angle from -PI to +PI
*/
inline void to_axis_angle(vector3& axis, float& angle)const {
float vl = (float)sqrt( x*x + y*y + z*z );
if( vl > TINY )
{
float ivl = 1.0f/vl;
axis.set( x*ivl, y*ivl, z*ivl );
if( w < 0 )
angle = 2.0f*(float)atan2(-vl, -w); //-PI,0
else
angle = 2.0f*(float)atan2( vl, w); //0,PI
}else{
axis = vector3(0,0,0);
angle = 0;
}
};


James Trotter
James Trotter
The correct tag for source code is [ source ], and [ /source ], (without the spaces, of course).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.