PDA

View Full Version : quaternion



kazmalak
02-08-2005, 05:23 AM
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(rti1*M_PI/360);
quarti1.quay=0;
quarti1.quaz=0;
quarti1.quaw=cos(rti1*M_PI/360);

quarti2.quax=0;
quarti2.quay=sin(rti2*M_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-2*total.quay*total.quay-2*total.quaz*total.quaz;
Matrix[1]=2*total.quax*total.quay+2*total.quaw*total.quaz;
Matrix[2]=2*total.quax*total.quaz-2*total.quaw*total.quay;
Matrix[3]=0;
Matrix[4]=2*total.quax*total.quay-2*total.quaw*total.quaz;
Matrix[5]=1-2*total.quax*total.quax-2*total.quaz*total.quaz;
Matrix[6]=2*total.quay*total.quaz+2*total.quaw*total.quax;
Matrix[7]=0;
Matrix[8]=2*total.quax*total.quaz+2*total.quaw*total.quay;
Matrix[9]=2*total.quay*total.quaz-2*total.quaw*total.quax;
Matrix[10]=1-2*total.quax*total.quax-2*total.quay*total.quay;
Matrix[11]=0;
Matrix[12]=0;
Matrix[13]=0;
Matrix[14]=0;
Matrix[15]=1;

glMultMatrixf(Matrix);

samv
02-08-2005, 06:00 AM
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.

kazmalak
02-08-2005, 08:52 AM
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.

samv
02-08-2005, 11:33 AM
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. (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=48)

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. (http://www.google.com/search?q=axis+angle+to+quaternion) But since you are probably interested in a matrix to feed into OpenGL you can do that almost as easily. (http://www.google.com/search?q=axis+angle+to+matrix)

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.

kazmalak
02-09-2005, 02:43 AM
Thanks Samv. Now something becomes clear. I will try to do the things you described. Thanks again. I hope I can solve.