# quaternion

I am still trying to rotate my draw using quaternions and still could’t succeed.

I have two rotations in Euler represantation

glRotatef(angx,1,0,0);
glRotatef(angy,0,1,0);

I create my local rotation quaternion by first converting those eulers to quaternion and then multiplying them. And it doesn’t work. Still I have gimbal lock problem. Here is the code. Can anyone show where I am doing wrong?

quaternion quarti1;
quaternion quarti2;
quaternion local;
quaternion total;

quarti1.quax=sin(rti1M_PI/360);
quarti1.quay=0;
quarti1.quaz=0;
quarti1.quaw=cos(rti1
M_PI/360);

quarti2.quax=0;
quarti2.quay=sin(rti2M_PI/360);
quarti2.quaz=0;
quarti2.quaw=cos(rti2
M_PI/360);

local=mult(normalize(quarti2),normalize(quarti1));

total.quax=0;
total.quay=0;
total.quaz=0;
total.quaw=1;

total=mult(total,local);
GLfloat Matrix[16];

Matrix[0]=1-2total.quaytotal.quay-2total.quaztotal.quaz;
Matrix[1]=2total.quaxtotal.quay+2total.quawtotal.quaz;
Matrix[2]=2total.quaxtotal.quaz-2total.quawtotal.quay;
Matrix[3]=0;
Matrix[4]=2total.quaxtotal.quay-2total.quawtotal.quaz;
Matrix[5]=1-2total.quaxtotal.quax-2total.quaztotal.quaz;
Matrix[6]=2total.quaytotal.quaz+2total.quawtotal.quax;
Matrix[7]=0;
Matrix[8]=2total.quaxtotal.quaz+2total.quawtotal.quay;
Matrix[9]=2total.quaytotal.quaz-2total.quawtotal.quax;
Matrix[10]=1-2total.quaxtotal.quax-2total.quaytotal.quay;
Matrix[11]=0;
Matrix[12]=0;
Matrix[13]=0;
Matrix[14]=0;
Matrix[15]=1;

glMultMatrixf(Matrix);

You still have a Gimbal Lock Problem because you still use Euler Angles(rti1 and rti2). Converting these to quaternions doesn’t help because it is too late, the “damage” has already been done.

Assuming you want to develop a “virtual trackball” you would probably want to find the rotation axis (perpendicular to both the view-vector and the “mouse-move-vector”) and angle (proportional to the length of the “mouse-move-vector”). Then construct a quaternion (or a matrix) out of that.

If anyone wants to here someone confesses he is a stupid and brainless guy, I can be the one. I am studying on quaternions for about four full days and I get totally lost in it and with my little English, I am still trying to understand these nonsense equations. I say I am still trying to the my best.

But that’s all. What does mouse movement vector mean? Is it that simple you describe in only two sentences? And how does view vector change? How can I know? I read approximately 20 tutorials. Where is it written?

Sorry for my honesty.
I really get tired.

Is it that simple you describe in only two sentences?
Not really I just didn’t go into too much detail since I am not sure where your Euler angles are coming from. I am just going to assume you are trying to do something like this.

If that’s the case then you would have 2 values describing the motion of the mouse (e.g. move the mouse left -> (-20,0) or move the mouse down a tiny bit -> (0,3) or something like that). Now you tack on an additional zero for the z-Axis which means that the mouse obviously didn’t move into the screen. That is what I called the Mouse-Move-Vector.

The view-vector is always going to be (0,0,1) (or (0,0,-1) depending on your convention).

The rotation axis is perpendicular to both of them, so you can just use the cross-product of the two (and normalize that).

The rotation angle is proportional to the length of the Mouse-Move-Vector, so just multiply that with some value that gives a nice feel.

This angle,axis pair can be converted into a quaternion rather easily. But since you are probably interested in a matrix to feed into OpenGL you can do that almost as easily.

Keep in mind that you should do these transformations incrementally. So either don’t LoadIdentity before you MultMatrix, or keep track of the complete current transformation yourself and multiply with that.

If something’s not entirely clear feel free to ask for clarifications.

Thanks Samv. Now something becomes clear. I will try to do the things you described. Thanks again. I hope I can solve.