Public Group

# Rotation problem - gimbal lock?

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

## Recommended Posts

Hi, I've got some issues with a rotation I'm trying to accomplish with quaternions. The spaceship in the game has a direction vector, a yaw axis, and a pitch axis (local Z, Y and X respectively.) I'm attempting to maneuver the ship with roll and pitch adjustments about these axes. My rotations work perfectly as far as changing the direction (camera view direction is always the same as the ship's orientation, and this works as expected.) However, when I render the ship, then it gets funky. I've zipped up the executable and supporting files here. http://www.myraytrace.org/GLAsteroids.zip Arrow keys rotate up and down and left to right (roll and pitch.) 'O' and 'L' accelerate and decelerate along the direction axis. Camera always looks in the direction of the ship's correct orientation. The rendering unfortunately doesn't render the ship the way it should be. Can anyone give me any ideas?

##### Share on other sites
Quaternion to Matrix conversion:

void qMatrix (Quaternion q, float *Matrix){

q.normalize();
Matrix[0] = 1-2*(q.V.y*q.V.y + q.V.z*q.V.z);
Matrix[1] = 2*(q.V.x*q.V.y + q.w*q.V.z);
Matrix[2] = 2*(q.V.x*q.V.z - q.w*q.V.y);
Matrix[3] = 0;

Matrix[4] = 2*(q.V.x*q.V.y - q.w*q.V.z);
Matrix[5] = 1-2*(q.V.x*q.V.x + q.V.z*q.V.z);
Matrix[6] = 2*(q.V.y*q.V.z + q.w*q.V.x);
Matrix[7] = 0;

Matrix[8] = 2*(q.w*q.V.y + q.V.x*q.V.z);
Matrix[9] = 2*(q.V.y*q.V.z - q.w*q.V.x);
Matrix[10] = 1-2*(q.V.y*q.V.y + q.V.x*q.V.x);
Matrix[11] = 0;

Matrix[12] = 0;
Matrix[13] = 0;
Matrix[14] = 0;
Matrix[15] = 1;

}

[Edited by - AirForceTeacher on April 29, 2006 3:42:38 PM]

##### Share on other sites
Bump.

Is this not an appropriate question for this subforum?

##### Share on other sites
Quote:
 Original post by AirForceTeacherIs this not an appropriate question for this subforum?
It's appropriate - sometimes posts just slip by for whatever reason. I'm taking a look at the executable now and will post back if I spot anything.

##### Share on other sites
Your quat-to-matrix functions looks correct (for an OpenGL-style matrix). So that's probably ok, unless you're mismatching conventions somehow.

I ran the app, and there were a couple of things that made it a little hard to tell what was going on. First of all, although the asteroid movement is smooth, the player seems to rotate in discrete increments, making for 'jerky' motion that's hard to follow. Maybe I have something set up wrong on my PC that's causing this? I also noticed that the speed of the ship's rotation seemed inconsistent; large increments at some times and small at others.

As for the ship, should it always appear to have the same orientation onscreen? If the camera is always supposed to be behind it, I would expect this would in fact be the case.

I don't have any particular ideas beyond that, as there are many things that could be causing the problem. Perhaps you could post the code where you set up the view matrix, and where you set up the matrix for and render the ship. That might help shed some light on the problem.

##### Share on other sites
Quote:
 Original post by jykI ran the app, and there were a couple of things that made it a little hard to tell what was going on. First of all, although the asteroid movement is smooth, the player seems to rotate in discrete increments, making for 'jerky' motion that's hard to follow. Maybe I have something set up wrong on my PC that's causing this? I also noticed that the speed of the ship's rotation seemed inconsistent; large increments at some times and small at others.As for the ship, should it always appear to have the same orientation onscreen? If the camera is always supposed to be behind it, I would expect this would in fact be the case.I don't have any particular ideas beyond that, as there are many things that could be causing the problem. Perhaps you could post the code where you set up the view matrix, and where you set up the matrix for and render the ship. That might help shed some light on the problem.

The ship is supposed to have the same view. That's what's got me wondering - the ship really shouldn't move at all. The jerky motion is the problem. As I mentioned, the view of every other object is correct, it's just the rotation of the ship that's messed up. I've tried both using the quats and regular OpenGL rotation functions. I'll try to post the code tomorrow - getting ready to leave on a business trip. Once I can get my matrices right to move the ship, then I can do my collision testing, but I've got to get my transforms right.

Thanks. I appreciate you looking and I'll get the code posted tomorrow or tonite.

##### Share on other sites
Quote:
 Original post by jyk Perhaps you could post the code where you set up the view matrix, and where you set up the matrix for and render the ship. That might help shed some light on the problem.

Code:

// Select The Modelview Matrix
glMatrixMode(GL_MODELVIEW);

.
.
.

// draw ship
glPushMatrix();
glDisable(GL_TEXTURE_2D);

glMaterialfv ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
glMaterialfv ( GL_FRONT, GL_SPECULAR, white);
glMaterialf ( GL_FRONT, GL_SHININESS, 128);
glColor3f(1.0f, 0.0f, 1.0f);

// Rotate around direction axis to match roll
// glRotatef(roll*180/PI, 0,0,1);

qRollArc.set (standard, roll);

// Set up rotation matrix from the standard OpenGL orientation to the current orientation of the object

direction.normalize();
standard.normalize();
cross = standard.Cross(direction);
dot = standard.Dot(direction);
// cross = direction.Cross(standard);
// dot = direction.Dot(standard);
s = (double) sqrt((1 + dot)*2);

qShortestArc.V.x = cross.x/s;
qShortestArc.V.y = cross.y/s;
qShortestArc.V.z = cross.z/s;
qShortestArc.w = s / 2.0f;

qRotate = qShortestArc.multiply(qRollArc);

qMatrix (qRotate, shipRotate);
glMultMatrixf (shipRotate);

glmDraw(ship, GLM_SMOOTH ); // library function for displaying .obj files

End Code

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 39
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631717
• Total Posts
3001875
×