   converting a quaternion to euler angles
  HelloI know that this subject's already been on the board, and I've tried using the equations posted in other posts as well as those that can be found here Euclidean space.As you can probably guess - I couldn't get them to work for me.My code looks like this:D3DXQUATERNION q;   D3DXQuaternionRotationYawPitchRoll(&q, DEG2RAD(124), DEG2RAD(269), DEG2RAD(351));   float sqx = q.x * q.x;   float sqy = q.y * q.y;   float sqz = q.z * q.z;   float yaw   = atan2( 2.f * ( q.y * q.w - q.x * q.z ), 1.f - 2.f * ( sqy + sqz ) );   float pitch = asin ( 2.f * ( q.x * q.y + q.z * q.w ) );   float roll  = atan2( 2.f * ( q.x * q.w - q.y * q.z ), 1.f - 2.f * ( sqx + sqz ) );   float gimbaLockTest = q.x * q.y + q.z * q.w;   if ( gimbaLockTest > 0.499f )   { yaw = 2.f * atan2( q.x, q.w ); roll = 0;   }   else if ( gimbaLockTest < -0.499f )   { yaw = -2.f * atan2( q.x, q.w ); roll = 0;   }   yaw = RAD2DEG(yaw);   pitch = RAD2DEG(pitch);   roll = RAD2DEG(roll);   CPPUNIT_ASSERT_DOUBLES_EQUAL(124.f, yaw, 1e-3);   CPPUNIT_ASSERT_DOUBLES_EQUAL(264.f, pitch, 1e-3);   CPPUNIT_ASSERT_DOUBLES_EQUAL(351.f, roll, 1e-3);The results I get are: [yaw = 115.00134, pitch = 0.15642539, roll = -90.987679].If I conjugate the quaternion before calculating the angles, I get the following results: [yaw = 178.03929, pitch = -64.983093, roll = -92.336105].Could you please help me spot the error?Thanks :)Paksas   
  Are you trying to get the output to equal (within numerical limits) 124, 264, 351?If so, first of all, be aware that since multiple Euler-angle triples can represent the same orientation, you may not always get the same angles out that you put in. Also, in order for the output to be as expected, the same conventions (e.g. axis order) that were used to create the quaternion in the first place must be used when extracting the Euler angles.I'll go ahead and ask my usual question here: what do you need the Euler angles for?   ;  >> I'll go ahead and ask my usual question here: what do you need the Euler angles for?I want to make an editor where I can display the value of a quaternion in the form of Euler angles.>>  Are you trying to get the output to equal (within numerical limits) 124, 264, 351?Yes - I want to have a constructor in my EulerAngles class that accepts a quaternion as a param. As a simple test, I wanted to create a quaternion using EulerAngles and roll that operation back by instancing my class.Any ideas what am I doing wrong here with the equations?Cheers,Paksas   ;Quote:Any ideas what am I doing wrong here with the equations?I find Euler-angle conversion code difficult (or maybe just tedious) to proofread visually, but here's a couple of tips.First, as I mentioned previously, make sure that you're using the same axis order when extracting the Euler angles that you used when building the quaternion from them. Do you know what axis order(s) you're using?Second, there are more references available for Euler<->matrix conversions than for Euler<->quaternion conversions, so you might have better luck converting your quaternion to a matrix and then extracting the angles from the matrix. This may involve a little unnecessary work, but that's unlikely to matter in practice (in most cases, at least). In any case, with a quaternion->Euler conversion, you're really doing a partial quaternion->matrix conversion in place anyway, and using an existing quaternion->matrix conversion function will eliminate that duplicate code and reduce the potential for error.Here are two places you can look for references on matrix<->Euler conversions. Dave Eberly's math library (at gometrictools.com) includes (or at least included at one point) code for converting between Euler angles and matrices using any (non-repeating?) axis order. You could also check out the CML (linked in my signature), which includes code for quaternion<->Euler and matrix<->Euler conversions using any axis order.   ;  I used the code from the book 3D Math Primer for Graphics and Game Development. Game Math. They have the code available for download on the site. They convert the quaternion to matrix and then to euler angles, but it's very easy. IIRC, you need to normalize the quaternion or it won't work.   