###

Question:

Im trying to do a simple rotation of a cube about the x and y axis:

I want to always rotate the cube over the x axis by an amount x and rotate the cube over the yaxis by an amount y independent of the x axis rotation

first i naively did :

`glRotatef(x,1,0,0); glRotatef(y,0,1,0); `

then

but that first rotates over x then rotates over y i want to rotate over the y independently of the x access.

I started looking into quaternions, so i tried :

`Quaternion Rotation1; Rotation1.createFromAxisAngle(0,1, 0, globalRotateY); Rotation1.normalize(); Quaternion Rotation2; Rotation2.createFromAxisAngle(1,0, 0, globalRotateX); Rotation2.normalize(); GLfloat Matrix[16]; Quaternion q=Rotation2 * Rotation1; q.createMatrix(Matrix); glMultMatrixf(Matrix); `

that just does almost exactly what was accomplished doing 2 consecutive glRotates ...so i think im missing a step or 2.

is quaternions the way to go or should i be using something different? AND if quaternions are the way to go what steps can i add to make the cube rotate independently of each axis. i think someone else has the same issue: Rotating OpenGL scene in 2 axes

###

Solution:1

I got this to work correctly using quaternions: Im sure there are other ways, but afeter some reseatch , this worked perfectly for me. I posted a similar version on another forum. http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=280859&#Post280859

first create the quaternion representation of the angles of change x/y then each frame multiply the changing angles quaternions to an accumulating quaternion , then finally convert that quaternion to matrix form to multiply the current matrix. Here is the main code of the loop:

`Quaternion3D Rotation1=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(-1.0f,0,0), DEGREES_TO_RADIANS(globalRotateX)); Quaternion3DNormalize(&Rotation1); Quaternion3D Rotation2=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(0.0f,-1.0f,0), DEGREES_TO_RADIANS(globalRotateY)); Quaternion3DNormalize(&Rotation2); Matrix3D Mat; Matrix3DSetIdentity(Mat); Quaternion3DMultiply(&QAccum, &Rotation1); Quaternion3DMultiply(&QAccum, &Rotation2); Matrix3DSetUsingQuaternion3D(Mat, QAccum); globalRotateX=0; globalRotateY=0; glMultMatrixf(Mat); `

then draw cube

###

Solution:2

It would help a lot if you could give a more detailed explanation of what you are trying to do and how the results you are getting differ from the results you want. But in general using Euler angles for rotation has some problems, as combining rotations can result in unintuitive behavior (and in the worst case losing a degree of freedom.)

Quaternion slerp might be the way to go for you if you can find a single axis and a single angle that represent the rotation you want. But doing successive rotations around the X and Y axis using quaternions won't help you avoid the problems inherent in composing Euler rotations.

The post you link to seems to involve another problem though. The poster seems to have been translating his object and then doing his rotations, when he should have been rotating first and then translating.

###

Solution:3

It is not clear what you want to achieve. Perhaps you should think about some points and where you want them to rotate to -- e.g. vertex (1,1,1) should map to (0,1,0). Then, from that information, you can calculate the required rotation.

Quaternions are generally used to interpolate between two rotational 'positions'. So step one is identifying your start and end 'positions', which you don't have yet. Once you have that, you use quaternions to interpolate. It doesn't sound like you have any time-varying aspect here.

###

Solution:4

Your problem is not the gimbal lock. And effectively, there is no reason why your quaternion version would work better than your matrix (glRotate) version because the quaternions you are using are mathematically identical to your rotation matrices.

If what you want is a mouse control, you probably want to check out arcballs.

**Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com**

EmoticonEmoticon