rotating an object in its frame of reference

Started by
7 comments, last by speciesUnknown 17 years, 6 months ago
Hi, Im having trouble with a flight simulator. Im writing a physics engine and, to test what i have so far, im using it in a flight simulator. THe problem is that i can't rotate an object in its frame of reference. Ive read page after page on the topic and havent solved my problem yet. Im using opengl as my renderer. When the plane is facing forwards it rols, pitches and yaws correctly. However, when i roll the model 90 degrees clockwise, the y axis should be lying along where the x axis used to be, but it isnt. Im using euler rotation. I am already familiar with matrices and am using these to translate the camera in the correct direction, but the plane model does not rotate with the camera. here is the method from my physics object class which i am using to rotate the object and then draw it: void phys_object::draw() { #define radians 0.0174532925 float ya = yrot*radians; float xa =xrot*radians; float za =zrot*radians; float Ax= cos(ya)*cos(za), Bx= -cos(ya)*sin(za), Cx= sin(ya); float Ay= sin(xa)*sin(ya)*cos(za) + cos(xa) * sin(za), By= -sin(xa)*sin(ya)*sin(za) + cos(xa) * cos(za), Cy= -sin(xa)*cos(ya); float Az= -cos(xa)*sin(ya)*cos(za) + sin(xa) * sin(za), Bz= cos(xa)*sin(ya)*sin(za) + sin(xa) * cos(za), Cz= cos(xa)*cos(ya); glPushMatrix(); glTranslatef(xpos,ypos,zpos); glRotatef(yrot,Bx,By,Bz); glRotatef(zrot,Cx,Cy,Cz); glRotatef(xrot,Ax,Ay,Az); model->draw(); glPopMatrix(); return; } ////////////////////////////////////////////////////// model->draw calls a display list. should i do one of the following things: 1) Extend my euler rotation system to solve my problem 2) Learn to use a different system of rotation than euler, for example quaternions? Thanks p.s. im aware that this is not the most efficient code, since it repeatedly calls sin() and cos() for the same variables, but i will optomize it once i get it working. My game engine is running at around 90fps without these optomizations so no problem there as yet
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Advertisement
I would suggest quaternions. They tend to result in code that is shorter and simpler.
is there simply some quick modification to the code in my original post which i could use instead? I know that it is possible to use euler rotation for the above problem.
Note that rotating objects efficiently is not the ONLY thing i want to do, since I am busy doing other things to bring my game engine into operation, and i dont want to spend a long time learning something new.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Quote:Original post by speciesUnknown
is there simply some quick modification to the code in my original post which i could use instead?
I doubt it. If I'm understanding the type of behavior you want, you'll need to use incremental rotations, either using vectors, matrices, or quaternions as suggested above. Again, this is assuming you want 6DOF motion.

If you just need to get something working quickly, I have some out-of-the-box code available which implements 6DOF motion and OpenGL matrix management. If you think it would be helpful I can post a link.
Thanks,
Ill look int 6dof (6 deg' of freedom?) rotation tonight. If you could post a link to that code i would greatly appreciate it, since it would be a big help in getting the test build running.

thanks again
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Here is some code that implements 6dof motion (yeah, 6 degrees of freedom) that you can use for reference (or as-is). Let me know if you have any questions about it.
Thanks for the code, just a few questions:
is editing the opengl view matrix directly equivelant to usign glRotatef() and glTranslatef()? Ive never seen it done that way before. is the view matrix 4x4 by any chance?
also, will each axis of the plane still point in the correct direction, e.g. y axis sticks out of the top of the plane, z axis points out of the nose cone and x axis goes along the wingspan. Supposing the plane is rolled onto its right side, i need the y axis to still point out of the top of the plane, even though it now behaves like the x axis.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Quote:Original post by speciesUnknown
Thanks for the code, just a few questions:
is editing the opengl view matrix directly equivelant to usign glRotatef() and glTranslatef()? Ive never seen it done that way before. is the view matrix 4x4 by any chance?
If you're referring to glLoadMatrix*() and glMultMatrix*(), these functions are similar to glRotate*() and glTranslate*() in that they modify the currently active OpenGL matrix, but are more flexible in that you can submit any type of matrix, not just those that can be constructed via OpenGL's helper functions.

In the OpenGLObject class I'm not doing anything that couldn't be done with glRotate*(), gluLookAt(), and so on, but for various reasons I chose to construct the matrix manually and submit it via glLoad/MultMatrix*().
Quote:also, will each axis of the plane still point in the correct direction, e.g. y axis sticks out of the top of the plane, z axis points out of the nose cone and x axis goes along the wingspan. Supposing the plane is rolled onto its right side, i need the y axis to still point out of the top of the plane, even though it now behaves like the x axis.
If you use the movement mode MODE_6DOF, then yes, the axes will rotate with the object (which I think is what you're describing).
thats great, sounds like just what ive been looking for over the last 5 days. Thanks for your help
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!

This topic is closed to new replies.

Advertisement