#### Archived

This topic is now archived and is closed to further replies.

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

## Recommended Posts

i've learned how to use quaternions to avoid the gimbal lock, but now after implementation of quat's, somethings strange are happening, all i'm asking for is source code/pseudo code for the function Render (the one which draws/renders whatever) using quaternions to avoid gimbal lock and moving in 3dspace, ofcourse it should rotate around the objects local coordinates . believe me , i'm not that lazy but i've gone mad and i don't trust anything now (neither mycode nor tutorials) all i want is to know how to use quat's to 1)avoid gimbal lock 2)move something in 3d with rotation for X,Y,Z axii . briefly, a very simple space sim with a cube as a ship and 2 other stationary cubes to feel you're rotationg. or a link to a space sim will do it. I've achieved the second target b4 quat's, but after using quat's it didn't work. thanks, i'll paste my code here so that you can c what i've reached .

bool Render(void)
{
Vector axis;
Quaternion currentQuat;
float angle;

glTranslatef(0, -2.0, -7.0);

character->Draw();

glPushMatrix();

currentQuat = SetQuaternionFromAxisAngle(&world.right, &world.up, &world.direction, - deltax, - deltay, - deltaz);
temp = cameraQuat;
currentQuat = NormalizeQuaternion(¤tQuat);
cameraQuat = MultiplyQuaternions(&temp, ¤tQuat);
cameraQuat = NormalizeQuaternion(&cameraQuat);

GetAxisAngleFromQuaternion(&cameraQuat, &axis, &angle);
axis = NormalizeVector(&axis);
glRotatef(angle, axis.x, axis.y, axis.z);

deltax = deltay = deltaz = 0;

glTranslatef(- world.position.x, - world.position.y, - world.position.z);
level->Render();
glPopMatrix();
return true;				// Everything Went OK

}

xee.. [edited by - xeee on June 1, 2002 9:05:21 PM]

##### Share on other sites
is your angle in degrees or radians when you get it from GetAxisAngle?

"take a look around" - limp bizkit

##### Share on other sites
GetAxisAngle converts the angle to degrees after extracting it from the quat .
tell me , does the code seem right??

xee..

##### Share on other sites
How are you storing the rotation for an object? It looks like you are storing the 3 euler angles. You should probably change the way you think about rotation: you can store the whole orientation of an object using one quaternion. This quaternion is initialized in the first frame, and after that you can just leave it, or modify it by multiplying it with other quaternions. Multiplying quaternions is essentially the same as matrix(3x3) multiplications. so say you have an object with an orientation Q (which is the quaternion I was talking about) and you want to rotate it around a certain axis with a certain angle, you construct a quaternion Q2 using the axis and angle, and multiply the two quaternions.

Now back to your opengl problem. You have camera quaternion called cameraQuat, and a currentQuat for the object.
Now there is a method to convert quaternions to matrices. Use this method:

Quaternion quat = cameraQuat * currentQuat
Matrix mat = QuaternionToMatrix(quat)
glSetMatrix(mat)

You could also use the push and pop matrices for what they are meant for. This means you have to start using glMultMatrix, etc. to do the multiplication of matrices. This way you could push the camera matrix just once, and push, pop and mult all object matrixes on the matrix stack afterwards.

Cheers,
Nick

• 18
• 11
• 16
• 9
• 49
• ### Forum Statistics

• Total Topics
631395
• Total Posts
2999781
×