OpenGL trouble with multiple rotations.

Problems with Canvas, OpenGL, etc...

OpenGL trouble with multiple rotations.

Postby JM33 » Thu May 12, 2011 11:14 pm

I am trying to make a simple airplane flight sim type of game. I am getting stuck with multiple rotations. I have tried many things over the last two weeks and always end up at the same place. Basically once I make one rotation around an axis/vector, it changes the up/right/forward vectors. I cannot get a second rotation around the new axis to work, it always wants to rotate around the original axis. My plan is to draw my airplane, then push the matrix, rotate for pitch, rotate for roll, and draw the land/sky. I have setup touchscreen "buttons" to control the plane with up = pitch up, down = pitch down, right = roll right, left = roll left.

Here are some ways I have tried:

Using glRotatef -

glRotatef(pitch, 0,0,1)
glRotatef(roll, 1,0,0)

This doesn't work because the first rotate changes the axis and the second rotation is incorrect. Swapping the order does not help because the second rotation is always on an incorrect axis.

Using matrices -

1st try - I built 3 rotation matrices for rotation around X,Y,Z axis. This results in the same as above because they are really the same matrices.

2nd try - I built one rotation matrix that premultiplies all 3 matrices. Same result.

3rd try -

I setup an initial Orientation matrix (which is actually the same as the identity). In the first frame I call glMultiMatrix(mOrientation, 0).

At the end of the drawing I add an increment of pitch & roll if user inputs up/down/left right.

I built a Rotation matrix for rotation around and arbitrary axis. First I get a rotation matrix for rotation of "pitch" degrees around mOrientation[0], mOrientation[1], mOrientation[2]. (This outputs a new matrix I call mArbRot) I tested this and it works fine as a single rotation around whatever axis I desire.

This is a rotation about the right vector, and the resulting matrix should have updated the up & forward vectors in column 2 & 3 respectively. So I call the arbitrary rotation around the new vectors. I rotate "roll" degrees about the new forward vector mArbRot[8], mArbRot[9], mArbRot[10]. Then I make mOrientation = mArbRot. The next frame should be rotating correctly around the right vectors when I call glMultiMatrix(mOrientation, 0).

But I get the same result. The axis for the second rotation is always incorrect. Hopefully someone has been through this and can give me a little direction. I'd be happy to post some code if it will help.

Thanks.
JM33
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 12, 2010 8:35 pm

Top

Re: OpenGL trouble with multiple rotations.

Postby Durza007 » Fri May 13, 2011 6:00 pm

I think you should be using something called quaternions, there is a good answer on stackoverflow with links on the subject: http://stackoverflow.com/questions/2758501/rotating-in-opengl-relative-to-the-viewport
Durza007
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 21, 2010 4:56 pm

Re: OpenGL trouble with multiple rotations.

Postby JM33 » Wed May 18, 2011 1:33 am

Well I got it working. No need for quaternions yet. Basically I had two problems. One was that I was increasing the pitch roll on each frame, actually adding to the value. This was not correct. I changed to just making pitch/ roll = 0.01f if the input was made by user, or = 0 if no input. The second problem was that I was not multiplying the matrices. Though I had tried it was giving me very strange rotations, for two reasons. One, because of the first problem of adding too much value to pitch/roll. The other because my matrix multiplication was transposed.

So I was actually able to eliminate the 2 rotations on arbitrary axes. Now I just use one matrix that is actually a concatenation/multiplication of a rotation on the x axis by "pitch" degrees, and "roll" degrees around the z axis. Then I multiply that matrix by my Orientation matrix. The result is used with glMultiMatrixf to give the right rotation.

I also added a matrix for translation from the values in column three of the rotation matrix, multiply it by the rotation matrix and then by the orientation matrix for a final affine transform matrix. Now this is used with the glMultMatrixf instead of the rotation only. Its working great, so now on to collision detection!
JM33
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 12, 2010 8:35 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google [Bot] and 1 guest