Jump to content
  • Advertisement


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


Moving an object with glTranslate and glRotate

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

If you intended to correct an error in the post then please contact us.

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.


Share this post

Link to post
Share on other sites
My problem is not limited to triangles... I also want to this with cubes, etc.. I just am not sure how to correctly move something like this.


Share this post

Link to post
Share on other sites
Okay I just tried it with a cube. Same thing. I am not sure exactly what you suggested I do... sorry...



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.

// 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


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!