# euler to quaternion not working

## Recommended Posts

Hi, Trying to convert euler angles to quaternion (see code) but doesn't seem to work properly (a rotation of 90 degrees on any axis rotates an object about ~1 degree, but never more). Can anyone spot an error?
quat EulerToQuat(const float3 &rot)
{
const float p = PI / 180;
float c1, s1, c2, s2, c3, s3, c1c2, s1s2;
quat q;

c1 = cos( (rot.y * p) * 0.5f);
s1 = sin( (rot.y * p) * 0.5f);
c2 = cos( (rot.x * p) * 0.5f);
s2 = sin( (rot.x * p) * 0.5f);
c3 = cos( (rot.z * p) * 0.5f);
s3 = sin( (rot.z * p) * 0.5f);
c1c2 = c1 * c2;
s1s2 = s1 * s2;

q.w = c1c2 * c3 - s1s2 * s3;
q.x = c1c2 * s3 + s1s2 * c3;
q.y = s1 * c2 * c3 + c1 * s2 * s3;
q.z = c1 * s2 * c3 - s1 * c2 * s3;
return q;
}
Thanks.

##### Share on other sites
yes. your formulas for q.* look wrong, though I didn't check them thoroughly. See http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles, at the bottom of the page

##### Share on other sites
Hmm.

I got the code from this site (nice resource btw):

http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/

So I assume the method works correctly. The only thing I added is the conversion of degrees to radians.

Perhaps that's what I'm doing wrong?

* Can someone point me to a piece of code that does the conversion and also takes in account singularities?

[Edited by - remdul on May 20, 2006 7:50:31 AM]

##### Share on other sites
Ack, I've got it solved. It was caused by an unrelated problem (some mixup in my matrix class).

The code I posted above is correct though.

As far as singularities concerned...doesn't seem to be a problem either.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628282
• Total Posts
2981810

• 9
• 10
• 11
• 17
• 15