Hello Guest

setting up the viewport

  • 3 Replies
  • 9556 Views
setting up the viewport
« on: June 17, 2010, 01:16:44 »
Kind of new to lwjgl, but I have done some basic opengl in the past. I am using slick2d, and trying to utilize opengl for manipulating the camera. This is for a simple sidescroller game. The game resolution is 800x600.

In my render method which is called every frame, I set the viewport as so:

Code: [Select]
cameraX = player.getX();
if(cameraX <= 400.0f) {
    cameraX = 400.0f;
}
cameraX -= 400.0f;
if(cameraX > 0) {
    cameraX *= -1;
}
GL11.glViewport((int)cameraX, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
What I wish to do is have the camera's center point to the middle of the screen, and then move left/right as needed when the player moves. I initially set it up as 400 for cameraX, 300 for the cameraY. Once I moved my character, the whole screen shifted so the bottom left corner of what I drew was now in the center of the screen (so it went 300 down, 400 to the left). I'd like to have it so the width of the camera is the same as the screen, and that it points to the center.

The first if statement is so the camera doesn't come further left then halfway across the screen. I then subtract it by 400 since the camera points to 400x when i have cameraX = 0. The second if statement is to ensure cameraX is negative. If I leave it positive the camera moves in the opposite direction of the player.

I hope this is clear enough for describing my issue.

Re: setting up the viewport
« Reply #1 on: June 17, 2010, 06:56:32 »
If you use Slick2d then use g.translate() to scroll your game.
If you use OpenGL directly then use glTranslate() on the Model&View matrix.
But the view port is the wrong thing to use.

*

Offline kappa

  • *****
  • 1319
Re: setting up the viewport
« Reply #2 on: June 17, 2010, 08:21:27 »
agree with Matthias's post above. If you are using slick2d you should not be using any LWJGL methods, slicks provides all the api needed for scrolling (moving the camera), etc.

You should only use LWJGL methods if you absolutely know what your doing and how the underlying code in slick works. You'll probably only want to do this to pull of some crazy 3d effect or something.

Slicks provides everything you needs to make almost any type of 2d game and you shouldn't need to look beyond its api.
« Last Edit: June 17, 2010, 08:36:26 by javalwjgl »

Re: setting up the viewport
« Reply #3 on: June 17, 2010, 12:43:39 »
I had found a thread on using g.translate(), though I found it wasn't working for me. I'll double check my code on it, as I still have it commented out, but it was along the lines of:

sbg.getGraphics().translate(cameraX,cameraY);