Archived

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

j_fish_

Moving objects with OpenGl

Recommended Posts

j_fish_    122
I am trying to write a simple game with OpenGL where panels with openings come up from the bottom of the screen and the user controls a ball at the top and has to guide the ball through the openings or else get smashed at the top of the screen. Its called falldown or something like that. Well the actual question is how do I move the ball around the screen without moving the camera. As is now, I am using the glTranslatef() command but when I place other things in the scene, they all move around too (which I undertand why they do). What other functions are there for movement of individual objects because the only thing I want the user to control is the ball. Thanks in advance! j_fish_

Share this post


Link to post
Share on other sites
Palidine    1315
glPushMatrix();
glTranselatef(...);
//draw your ball
glPopMatrix();

now only the ball is translated. typically you store a matrix for each object in the scene and draw it wrapped in glPushMatrix()/glPopMatrix() calls. they do just what they say, they push a matrix onto the openGL matrix stack so that you can pop it off without changing the underlying camera transforms done by gluLookAt(...) or whatever you are using to set the camera matrix.

[EDIT: btw, if you're playing around with openGL for the first time the NeHe tutorials (http://nehe.gamedev.net) are pretty good. and ALWAYS look up problems in the openGL bible, The Red Book http://fly.cc.fer.hr/~unreal/theredbook/)]

-me

[edited by - Palidine on March 23, 2004 6:34:49 PM]

Share this post


Link to post
Share on other sites
j_fish_    122
Hey thanks for the tip I''ll give that a try. I appreciate the speedy responses around here! Hopefully sometime I''ll be able to help someone too. Thanks again.

j_fish_

Share this post


Link to post
Share on other sites
lonesock    807
You are doing it right. It is just that glTranslate is just like glRotate or glScale (or almost anything else) in that it will modify the matrix OpenGL is using for transformation. So all subsequent objects will go through the same transformations.

If everything in your game is in real world coords except the ball, then draw all of that first, then glTranslate, then draw the ball. Alternatively, you can save the matrix state before the translation (glPushMatrix) then restore it afterward (glPopMatrix). Or, you could do the (almost) same thing by doing a glTranslatef in the opposite direction (i.e. -dx,-dy,-dz). This is less stable, but won''t be noticible for just a few objects.

lonesock

Piranha are people too.

Share this post


Link to post
Share on other sites