Archived

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

drdarkon

Moving an object with glTranslate and glRotate

Recommended Posts

Hello there, I just realised there is an entire forum dedicated to OpenGL so I guess this would be a good place to ask. Okay I have a problem. I am trying to steer and move a triangle I drew with the keyboard using the glRotate and glTransform functions. But I am having a problem moving the triangle on its own axis. I want the triangle to move in the direction it is facing. I figured all I would have to do is rotate it and then translate it and it would follow its own z axis (which I just rotated.) However, I can only get one of these to occur. When I translate and then rotate, the triangle will rotate around its own axis, but it will move down the origin''s z axis. When I rotate and then translate, the triangle rotates around the origin''s axis and moves down its own z axis. ahh! How can I have it rotate AND move on its own axis. Is there a better way? Any helps is greatly appreciated. - Jesse Barksdale

Share this post


Link to post
Share on other sites
Translate, then rotate, making sure that the triangles is drawn "flat" to the screen. This issue is also in the OpenGL Red Book, which you might glance at for reference.
If you''re going to do much work with OpenGL, you may consider downloading the latest version of POV-Ray and playing around with that. Freeware, and it works well. A little frightening at first, but it uses similar concepts to all other tri-D renderers.

-Sta7ic

Share this post


Link to post
Share on other sites
I ran into a similar problem recently (during a graphics class)
was trying to move a robot around via arrow keys (basic stuff, I know.. but I''m new)

Anyways, I transformed using translate/rotate, but translated to a cosine/sine function of the rotated angle - the net effect is the translation still ''happens'' in worldspace, but *appears* to happen relative to the object in question.

glTranslatef(bot_x,7.5,bot_z);
// Robot''s center is at worldspace (0, 7.5, 0)
glRotatef(bot_th, 0,1,0);
// this is the frame of reference for the robot as a whole

where bot_x, bot_z, and bot_th are globals, (bot_x and bot_z are floats, and bot_th is an int (in degrees)) handling the obvious - then to move the robot ''forward'' I used the transformation

bot_x = bot_x + sin(bot_th*d2r);
bot_z = bot_z + cos(bot_th*d2r);

where d2r is a constant (degrees to radians - pi/180)

and to rotate the robot, simply

bot_th = bot_th + 5;

Obviously not optimal, but it does work, and should be a decent starting point.

I''''m a man with a one-track mind/
So much to do in one lifetime

Kam

Share this post


Link to post
Share on other sites