Trouble Orienting an airplane in a flight sim

Started by
3 comments, last by tenmar 18 years ago
Anybody, i just ran into a problem when building a simple flight sim. All the code is ready, with the physics, but I can't seem to get the airplane to orient itself correctly according to the heading and lift vectors. At present, i've obtained the "heading" vector and the perpendicular "lift" vector, (both are normalized) The cross product of the above mentioned vectors gives me the normalized axis around which my airplane will pitch up and down, when i apply the neccessary torque everything works find, the above vectors are rotated properly, BUT... I am unable to orient the airplane accordingly, it orients for a little while, but as i keep rotating my airplane,it disorients, in a different direction, i'm currently using... ... glRotatef (ori.x, vAxis.x, vAxis.y, vAxis.z); glRotatef (ori.z, heading.x, heading.y, heading.z); where "vAxis" is the pitch axis, and heading is the heading vector (roll axis) "ori" is the orientation vector which changes when a torque is applied I ignore ori.y, because at present I'm not applying any torque around the Y axis of the airplane Any help would be much appreciated. If i'm doing it wrong please tell me the right way.
Advertisement
..............

...yeah...

That really isnt the approach you want to use for positioning the airplane graphics model...

You already have the airplanes Local Axis - you know its forward 'heading' vector, its 'lift' vector (where the top of the airplane points?) and the vector to one side (you seem to use it as the pitch axis)

Anyway, those 3 vectors define an orientation, and can be combined to create an 'orientation matrix'. This kind of matrix can instantly transform your graphics model to the correct position in one step. Using the glMultMatrix() function, instead of separate glRotate()s.

basically you take your 3 Local Axis, and put them side by side to form the rows and columns of a matrix.
rather than call them heading, lift, and ?whatever you call it?
its more traditional to call them x,y,z (not to be confused with the X,Y,Z World Coordinate system)

the Orientation Matrix looks like this:
x.1 x.2 x.3 0y.1 y.2 y.2 0z.1 z.2 z.3 00   0   0   1or like thisx.1 y.1 z.1 0x.2 y.2 z.3 0x.3 y.3 z.3 00   0   0   1depending on if you use a row or column major system.... I forget which one you need for opengl, so try it both ways.


yes, so take your 3 vectors, rename them x,y,z for clarity.
then copy their component values into a float[16] array as the Matrix illustrated. and pass that into the glMultMatrix() function
it should handle the rotation of your airplane graphic model nicely....

reference for glMultMatrix()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_0xmg.asp
HEY THANKS A MILLION THAT WORKED!

i used the major row form.

and initially it was all black, so i used the

glScalef(-1, -1, -1);

but then, i negated the components and removed the scale function and it worked fine, i had to do the cross product (for pitch axis) in opposite direction because it was rotating opposite to direction vectors.

BUT FINALLY IT WORKED, TNX A LOT, can u tell me some articles to better understand 3D transforms and rotations.

THANKS AGAIN MAN,

my code now looks like this, (i'm still using the weird names because i kinda got used to it)

void CPlane::Render(void)
{
if(!modelLoaded)
{
return;
}

float mat[16];

mat[0] = -vAxis.x;
mat[1] = -vAxis.y;
mat[2] = -vAxis.z;
mat[3] = 0;

mat[4] = -lift.x;
mat[5] = -lift.y;
mat[6] = -lift.z;
mat[7] = 0;

mat[8] = -heading.x;
mat[9] = -heading.y;
mat[10] = -heading.z;
mat[11] = 0;

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


glPushMatrix();
glTranslatef(pos.x, pos.y, pos.z);
glMultMatrixf(mat);

//glRotatef (ori.x, vAxis.x, vAxis.y, vAxis.z);
//glRotatef (ori.z, heading.x, heading.y, heading.z);
model.Render(0, 0, 0);
glPopMatrix();
}
You may not be aware, but OpenGL internally represents all transformations (such as translations, rotations, and scales) as 4x4 matrices. There is a very straightforward procedure for multiplying a matrix by a vector (automatically performed each time you specify a vector in OpenGL), which will yield a transformed result vector.

In our OpenGL program, we begin with the identity (do-nothing) matrix (when we glLoadIdentity()), and from there, each subsequent call to glTranslatef or glRotatef or what-have-you will automatically generate a new matrix to accomplish the specified transformation, and multiply it by the currently set matrix, to replace it. The result is the concatenation of the transformations, meaning that the multiplied transformations will be performed in order -- but rather than have to multiply each vertex by a series of matrices, we can just multiply it by a single one, which will perform all the transformations specified so far. So you see it's both flexible and efficient.

What your original code had been doing was performing two sequential rotations, which would never be able to achieve your goal. The simple fact of the matter is, one rotation and then another just isn't an adequate model of reality -- that's not what's going on. You just have one orientation, and that's it. In order to meet your goal, you'd need to do it in a single rotation, which haphazardlynamed's technique achieves by manually specifing the rows and columns of the transformation matrix.

I won't at this time go into full explanatory details (it's late), but I will say that you'll want to study linear algebra, which is essentially the mathematics of matrices and vectors in depth. (BTW I can see that you already know some about linear algebra due to your awareness of vectors and cross products, so by no means do I mean to belittle your awareness, however I know all too well how much of this information can be picked up on-the-fly rather than studied in depth. To study in depth, it's linear algebra all the way.) Study how a transformation matrix can contain basis vectors for a reference frame (noting how the identity matrix contains the three cardinal vectors of a default reference frame), and from there you may begin to see how specifying the vectors of a rotated reference frame (your relative forward, up, and side vectors) can produce a matrix that transforms vectors into that rotated reference frame.

Now that you know some terminology, wikipedia and google are your friends. Good luck in your journey.

PS -- you can also accomplish this using gl's gluLookAt (http://www.hmug.org/man/3/gluLookAt.php), in which you can essentially supply a location for the camera, a point to look towards (essentially the same info as a "forward" vector), and an up direction (GL will figure the right direction from the cross product), and will generate this same matrix for you. If you toy with that you should be able to get the same result.
- Hai, watashi no chichi no kuruma ga oishikatta desu!...or, in other words, "Yes, my dad's car was delicious!"
Thanks a MILLION Bucket_head,

That really helped, and tnx for taking into consideration for not belittling my knowledge on vectors.

In truth,

I knew how the essence of 3D and api's such as openGL and directX work on 3D which is as u said using 4d matrices, i've learnt linear algebra and solving homogenous equations using matrices last year in school but we had learnt nothing about rotations, transforms and the like. We learn matrices and all, but we don't learn anything about 3D and how it can be used in 3D representations and in vector engines, so what i did was, that i read through all the matrix and vector articles in gamedev and to my fullest knowledge, i've learnt all i can understand, finally i've understood what happened in that rotation haphazardlynamed told, the 1st row (3 elements) in the matrix represents the xaxis rotation and the 2nd and 3rd refer to the remaining orthogonal axes (y,z )with respect to the orientation of the plane and the identity matrix corresponds to the default x,y,z axes of the plane. yep, what u told has enlightened me. But i'm still angry at the fact that they never taught 3D in school, they should have, it would have made my life easier anyway, but then again, i'm learning it now.

the remaining of what i've understood by what u told me is,

performing 2 glRotates caused a wrong orientation because of the fact that the axes itself changed with respect to the original orientation by the time the first glRotate was performed and conseqently my second glRotate would cause an incorrect orientation.

So in theory and practice knowing minimal programming u can still make an excellent game only the maths and physics is important and the understanding of the subjects will help u in making games and also make it more enjoyable.

I think the problem was, school maths deals with only working out problems but not actually creating your own and solving them, but instead they give u an incoherent question to which u must provide an answer based on how u were taught to work out the problem with minimal understanding. But game developement involves a truly higher plane of thinking.

But that is beside the point, Thank you again bucket_head and haphardlynamed the little words you used has helped me in large amounts.

Please tell me if there are any mistakes in my understanding of matrices as stated above

This topic is closed to new replies.

Advertisement