Jump to content
  • Advertisement
Sign in to follow this  

Quaternion - Axis/Angle

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
[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;
}
};


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!