# Problem with Quaternion!!!!!!

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

## Recommended Posts

Regards, i am using the following function to do Quaternion rotation. i am caling the function twice to do the rotation once aronud the X axis and another around the Y. however, when i excute it and multiply the resultant matrix with the current one, the object drawn on screen keeps rotating around its Y axis not the global one. it works fine in the case of rotating around the global X axis, i dont know why!!! any suggestion??? tahnx in advance void Quaternion(GLfloat X , GLfloat Y , GLfloat Z , GLfloat degree , GLfloat *pMatrix ) { GLfloat w, x, z, y; // convert the degrees to radians GLfloat angle = GLfloat((degree / 180) * 3.14); // calculate the sin( theta / 2) GLfloat result = Math::Sin(angle / 2); // Calcualte the w value by cos( theta / 2 ) w = Math::Cos(angle / 2); // Calculate the x, y and z of the quaternion x = X * result; y = Y * result; z = Z * result; // pMatrix will hold the [16] quaternion matrix if(!pMatrix) return; // First row pMatrix[ 0] = w*w + x*x - y*y - z*z; pMatrix[ 1] = 2 * (x * y + z * w); pMatrix[ 2] = 2 * (x * z - y * w); pMatrix[ 3] = 0; // Second row pMatrix[ 4] = 2 * ( x * y - z * w ); pMatrix[ 5] = w*w - x*x + y*y - z*z; pMatrix[ 6] = 2 * (z * y + x * w ); pMatrix[ 7] = 0; // Third row pMatrix[ 8] = 2 * ( x * z + y * w ); pMatrix[ 9] = 2 * ( y * z - x * w ); pMatrix[10] = w*w - x*x - y*y + z*z; pMatrix[11] = 0; // Fourth row pMatrix[12] = 0; pMatrix[13] = 0; pMatrix[14] = 0; pMatrix[15] = 1; }

##### Share on other sites
If this is intended as OpenGL-compatible code, your comments should read 'first column', 'second column', and so on instead of 'first row'. As it happens, though, the matrix elements are assigned correctly.

As for the problem you describe, with Euler-angle sequences, generally only one of the rotations will actually appear to occur about a world axis.

In your case, you're doing an x rotation and a y rotation. If you rotate about x followed by y, the x rotation will appear to be about the local x axis, and the y axis about the world y axis. The order y->x will make y appear local and x appear to be a world axis.

If those aren't the results you want, you may want to employ some other method than Euler angles.

##### Share on other sites
thanx jyk for ur comment
now, if according to what u posted in ur reply "with Euler-angle sequences, generally only one of the rotations will actually appear to occur about a world axis...", why is Quaternion considered to be a great solution for rotation?

what u have explained is exactly the problem i am facing now, one is global and the other is local. what other method should i employ than Euler angles to have my app functioning according to global axis in both cases?

thanx again
Abolfoooud

##### Share on other sites
Quote:
 now, if according to what u posted in ur reply "with Euler-angle sequences, generally only one of the rotations will actually appear to occur about a world axis...", why is Quaternion considered to be a great solution for rotation?
The first thing to understand is that anything you construct from Euler angles will exhibit the same problems that Euler angles do. This holds for both matrices and quaternions. The idea that quaternions solve the problems introduced by Euler angles is at best an inaccuracy and at worst simply incorrect.

You say you want the object to always rotate about the world axes. I can't say this with absolute certainty, but I don't think there's any way to do this Euler angles. I can, however, tell you with certainty how it can be done.

First of all, for this problem it doesn't matter whether you use quaternions or matrices. For the sake of argument we'll discuss the problem in matrix form. You need to maintain a matrix from frame to frame that represents the orientation of your object. When you want to rotate the object about a world axis (or any axis), construct the appropriate rotation matrix and multiply it with your orientation matrix. The result will be your updated orientation.

One last thing. Whenever you perform incremental rotations, your orientation will slowly drift out of orthogonality due to floating-point error. To correct this, just orthogonalize your matrix periodically.

That may be a more complicated answer than you wanted, but I can't think of a simpler way to do what you want off the top of my head.

##### Share on other sites
that sound really disappointing :(
i thought that quaternions will solve my problems, but it seems that it is not a 100% solution.
i will try to go through the procedure u proposed, although i think it would be too expensive to the mobile platform i am targeting in my app. i will give it a try any ways.
thank u very much

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011722
• ### Who's Online (See full list)

There are no registered users currently online

×