PDA

View Full Version : Camera follow object.



vantage
11-15-2007, 06:50 PM
can anyone give me some clues on how i make the camera follow an object as i move the object left or right in 2D?

k_szczech
11-16-2007, 03:38 AM
object position: (objx, objy)
camera pisition: (camx, camy)


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho...
glTranslatef(-camx, -camy, 0.0f);

glMatrixModw(GL_MODELVIEW);
glLoadIdentity();

DrawStaticWorld();

glPushMatrix();
glTranslatef(-camx, -camy, 0.0f);
DrawObject();
glPopMatrix();

That's it - now you must only make camx,camy follow objx,objy in any way you want. Simplest would be:
camx = objx;
camy = objy;

Xmas
11-16-2007, 05:00 AM
That's better:

glMatrixModw(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-camx, -camy, 0.0f);

DrawStaticWorld();
The camera position and orientation defines the transformation from world to view space, thus it should be part of the modelview matrix.

If you're using the GL matrix stack with separate projection and modelview matrices at all then at least separate them the right way.