Camera movement. How do I move camera?
I currently have an open gl renderer set up which displays a 2d square and another 2d shape.开发者_如何学C By using the keys the user is able to move the square up down left and right. This is done through translating the square based on values altered by whether the user presses up down left right eg if the user presses right it would mean a translation of gl.glTranslatef(rightdisplacement, 0, 0); etc. The movement works fine but I cant figure out how to get the camera to follow the square as it continues moving. I would like to move the square and have the camera move in the same direction.
If you're doing 2D rendering you are probably making a call to glOrtho somewhere to define your coordinate system. Total speculation, but your code might look something like:
glOrtho( 0, screenWidth, 0, screenHeight, -1, 1 );
This is where you define your camera position. Create camera position x and y position variables and instead call
glOrtho( camX - screenWidth/2, camX + screenWidth/2, camY - screenHeight/2, camY + screenHeight/2, -1, 1 );
Make sure that this is called every frame as the camera position will obviously change. Your render code might look something like
// clear framebuffer
glMatrixMode( GLES10.GL_PROJECTION );
glLoadIdentity();
glOrtho( camX - screenWidth/2, camX + screenWidth/2, camY - screenHeight/2, camY + screenHeight/2, -1, 1 );
glMatrixMode( GLES10.GL_MODELVIEW );
glLoadIdentity();
// draw your stuff
精彩评论