0
\$\begingroup\$

I am making a top-down rpg game with the LWJGL but I can't figure out how to make the camera follow the player. I've tryed using GLU.gluLookAt() but it seems to be designed for 3D and when I try it with my game everything currently on the screen disappears. I have no idea where the camera gets moved to. Please explain simply how to use GLU.gluLookAt() or a better alternative to move the camera.

public class Camera {

    public float x = 0;
    public float y = 0;

    public void moveCamera(float toX, float toY){
    GLU.gluLookAt(x+toX, y+toY, 0.0f, 0.0f, 0.0f, 0.0f, 0f, 1f, 0f);
    }
}
\$\endgroup\$
0
\$\begingroup\$

The first three arguments of gluLookAt are position of your camera. The next three is the point the camera is looking at. So you have to move looking at point as well as the position of camera.

GLU.gluLookAt(x+toX, y+toY, z, x+toX, y+toY, 0.0f, 0f, 1f, 0f);

Don't forget to move away camera on z axis (but I'm not sure if it will be problem).

Another way is to make translation (glTranslatef) before you draw your game.

\$\endgroup\$
  • \$\begingroup\$ Sorry for such a simple question but what should the value of z variable be? The game's 2D so I assumed there was no z axis. \$\endgroup\$ – icedvariables Feb 8 '14 at 18:31
  • \$\begingroup\$ You are holding your camera in 3D space. Your game is fake 2D because you are using only x and y (z=0). I am not sure but it could be a problem if your camera is at the same value of z. Just check. \$\endgroup\$ – Gregory Feb 8 '14 at 18:51
  • \$\begingroup\$ Keep in mind that glTranslate is fixed function and has been deprecated in modern OpenGL. \$\endgroup\$ – Lysol Feb 8 '14 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.