# Quaternion - Axis/Angle

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

## 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 on other sites
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 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 on other sites
The correct tag for source code is [ source ], and [ /source ], (without the spaces, of course).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015434
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!