Sign in to follow this  
amoeba

Translations/matrix problems

Recommended Posts

I really cannot get my head around translations. I want to have a spaceship, and fly it around a 'galaxy' thats larger than the screen. I want to fix the spaceship to the centre of the screen, and move everything else according to it. This is going to be in 2d, so in theory is easy? I get very confused with glPushMatrix() and glPopMatrix, and yet they seem central to what I am trying to do. I have looked at tutorials and things on the web - but still cant get it sussed.

Share this post


Link to post
Share on other sites
If you want to keep your spaceship in the middle of the screen you should point your camera to it :)

Push and Pop matrix add or remove a matrix from the matrix stack. You can use them to translate a object relative to another object(moon orbitting the earth, sword in the hand of a knight).

Share this post


Link to post
Share on other sites
Anychance of some psuedo-code for a real idiot please?

The way I try it;

gluLookAt( 0, 0, 50, 0,0,0 0,1,0 );

pushMatrix()
--draw spaceship
popmatrix()

pushmatrix()
-- draw a planet
popmatrix()

But it doesnt seem to draw anything, even though my spaceship is at 0,0,0. If I remove the glulookat, then its fine but I have trouble with the planets....

Share this post


Link to post
Share on other sites
You don't need gluLookAt with 2D.
Just make sure you use glOrtho or gluOrtho2D for your GL_PROJECTION matrix setup, and then draw everything using glVertex2* function calls.
To draw the ship just do something like:
<code>
glLoadIdentity();
glPushMatrix();
glTranslate(shipPosX,shipPosY,0);
drawShip();
glPopMatrix();
</code>
shipPosX and shipPosY are obviously the location of the ship on the screen. Since you have your own world space (a galaxy or whatever) you need the ship to be centered.
Try adding (before all other translations but after the load identity call) a translate by the negative of the ship pos. Something like:
<code>
glLoadIdentity();
glTranslatef(-shipPosX,-shipPosY,0);
glPushMatrix();
//any transforms on the ship, but no movement cuase of centering
drawShip();
glPopMatrix();
ini i;
for (i=0;i<numThings;i++) {
glPushMatrix();
glTranslatef(thingPosX[i],thingPosY[i],0);
drawThing(thing[i]);
glPopMatrix();
}
</code>
Or whatever...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this