Gimbal Lock and Quaternions

Started by
22 comments, last by abolfoooud2 18 years, 7 months ago
Quote:
so would u mind timw to post pieces of the code in a new post or to add to ur kindness and give it to me :D ?


here is the source, the files your interested in is QuaternionTeapots.cpp. there is no documentation, I wrote a little header to point you to the right funcitons, I think it's small and simple enough to figure out..


this is vstudio 6.0 project file, you'll need to have the proper glut headers to compile tho....

http://ieng9.ucsd.edu/~twernett/QuaternionTeapots.rar

hope it helps,

Tim

btw the math library is from a graphics teacher of mine, look at his comments for more information. I highly recomend his book btw, it covers this kinda stuff extensively, and has a VERY thuro treatment of curves and surfaces, (no subdivision tho).
Advertisement
thank u tim for the code
i have solved the problem i am facing after understanding ur code.
the solution was sooooo simple actually. but, it was my misunderstanding of how to find the orientation of the object that sumbled me over.
there is a single bit of your code that i couldnt find an explanation of using it.
when u defined fNorm and fTheta as:

fNorm= fTheta = sqrt(iDx*iDx + iDy*iDy);
fTheta *= DTHETA; // where DTHETA = 0.012


and then u used them to define the rot quat as:

dQ.Set(iDy*fSin/fNorm, iDx*fSin/fNorm, 0, cos(fTheta));
//where iDx and iDy are the current orientations of the mouse

my questions are:
1) how did u calculate theta by using sqrt(iDx*iDx + iDy*iDy)? what is this procedure of finding the angle called?
2) what is iDy*fSin/fNorm for? is it the fields conversion needed when convrting a quaternion to matrix? and if so, why do u need to devide by fNorm?
3) what is fNorm? is it a normal? what normal? how did u find it using the length calculation in the sqrt? i know that u have to use cros prod to find norms.

thanx again mate
and thanx for all the guys out thr who participated in this post
it's been a while since I wrote that, so bear with me...

Quote:
1) how did u calculate theta by using sqrt(iDx*iDx + iDy*iDy)? what is this procedure of finding the angle called?


I don't think this has a name, you see there are many ways to solve this problem here is what I'm trying to accomplish here. basically our mouse funciton gets called and we calculate the deltaX, and deltaY. we want to rotate the object around the global X axis in proportion to deltaY, and we want to rotate the object about the Y axis in proportion to deltaX. now one way to do this, is to build 2 seperate rotations rotY, rotX. each of them with there rotation angles proportional to delta's given by the mouse. then multiply them together to obtain the final rotation, for example.

Quaternion qY, qX, qRot;(initialized to identity quaternion or somthing)

angleX = ((float)iDy)*DELTA; //you need to multiply by a small constant to scal the rotation, it's just to large otherwise.

angleY = ((float)iDx)*DELTA;

qX.set(1*sin(angleX), 0*sin(angleX), 0*sin(angleX), cos(angleX));
qY.set(0*sin(angleY), 1*sin(angleY), 0*sin(angleY), cos(angleY));

//set the final rotation
qRot = qX*qY;

ok, now the way I do it is slightly more efficent. instead of building two seperate rotations with angles proportional to iDx, and iDy I build a single rotation. the axis I rotate around is the axis (iDy, iDx, 0). this vector is closely related to the hat function, the hat vector is the 2d vector orthognal to the vector being hatted. another way to put it, is I rotate the object around the vector that is perpendicular to the line formued by the start and end points of the mouse's displacement.

...
HAT((x,y)) = (-y,x) because (x,y) DOT HAT((x,y)) = 0, thus the vector orthogonal.
...

the reason I didn't use a negative component in my fromulation. is strictly because of signess, my angles are strictly positive because of my use of the sqrt() otherwise I'd need to take sign in account, for example my angle is positive if the mouse goes right to left OR left to right, obviously those rotate the object in oposite directions, instead of using the angle to take care of this fact, I just use dx, which is positive left to right and negative right to left(hope that makes sense).

anyway, to calculate the angle(the magnitude of the rotation) I simply rotate the object in proportion to the distance travled by the mouse, that is to say
sqrt(dx^2 + dy^2) it's probably more efficent to use the distance squared, but that will give you uneaven rotations, on second thought, don't use the distance squared lol.

Quote:
2) what is iDy*fSin/fNorm for? is it the fields conversion needed when convrting a quaternion to matrix? and if so, why do u need to devide by fNorm?


no, this has nothing to do with a matrix, in my program, everything is kept as a quaternion, until the final rendering stage.

recal the definiton of a quaternion:

q = (a*sin(T), b*sin(T), c*sin(T), cos(T))

where (a,b,c) is the vector to rotate around and is NORMALIZED.

IFF the vector a,b,c is normalized, then the resulting quaternion x,y,z,w is normalized as well.

my division by the norm of of the direction vector (dy, dx, 0) is used to normalize it.

recal that
fNorm = fTheta = sqrt(iDx*iDx + iDy*iDy);

(although, strictly speaking, it is not necessary that a quaternion be normalized, the magnitude of quaternion DOESNT effect it's rotation, however it does matter for the matrix conversion, which relies on the fact that the quaternion is normalized, but you could formulate it differently if you wanted, it also matters that all your quaternions have the same magnitude when you're applying spherical linear interpolation, for simplicity sake we make the magnitude 1, this is the standard way of doing it)

Quote:
3) what is fNorm? is it a normal? what normal? how did u find it using the length calculation in the sqrt? i know that u have to use cros prod to find norms.

you've probably figured this one out by now, lol.

fNorm is the norm(ie magnitude) of the rotation vector (dy, dx, 0) and is used to normalize the rotation vector.


Tim
GOT IT GOT IT...
the naming of the fNorm caused me some misconception, but now i understand what it refers to. and also i got what fTheta = sqr(...) means. i didnt think of it the way u did by using one quat instead of two where u need to have the distance between ur prev and curr x and y locations.
i have my prob solved now.
thanx

Abolfoooud

This topic is closed to new replies.

Advertisement