LWJGL Orthographic Sidescroller Camera

Hi,

For my game I am making I am trying to implement an orthographic camera to sidescroll, currently the camera just stays still and I cant seem to work out the best way to sidescroll (ideally attached to a entity(e.g. the player))

This is the only camera code I have right now:

GL11.glOrtho(0.0f, width, height, 0.0f, 1.0f, -1.0f);

Thanks,

  • Dan

Just translate everything to player.x - viewport.width/2, player.y - viewport.height/2, example:


glTranslatef(player.x - vp.width/2, player.y - vp.height/2, 0);

[icode]glOrtho()[/icode] just defines the projection matrix to use an orthographic projection. To translate, you need to use the [icode]glTranslatef()[/icode] function.

I’d leave the camera alone and move the world data.

Yeah I tried that too, didnt seem to work the way I was doing it for some reason :confused:

package Logic;

import org.lwjgl.input.Keyboard;

public class WorldLogic {

	public static double x;
	public static double y;
	public static double xspeed;

	public WorldLogic() {
		glTranslatef(player.x - vp.width/2, player.y - vp.height/2, 0);
		x = 20;
		y = 0;
		//World scrolling speed
		xspeed = 0.4;
	}
	
	public void logic() {
		x+=xspeed;
		input();
	}
	
	
	public static void input() {
		if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
			xspeed = xspeed * 0.4;
		}
		System.out.println("Press Left");
		if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
			xspeed = xspeed * 0.4;
		}
		System.out.println("Press Right");
	}
}

Thanks,

  • Dan

You don’t have the variables player and vp defined. Make a player class with member variable floats x, y and make a viewport class with member variable floats width and height.

Also your logic slows down the player when they press the keys. Shouldn’t you be speeding him up?

Yeah instead of a viewport im just using the Display, but it still doesn’t like it, like this:

GL11.glTranslated(MainPlayer.x - Display.getWidth()/2, MainPlayer.y - Display.getHeight()/2, 0);

Also just a side note, my player physics are upside down :confused: Player:
http://pastebin.java-gaming.org/c2afe35318c

Thanks,

  • Dan

@GamerIDGoesHere

Why are you using [icode]static[/icode] for your player class methods?

Cant remember, but it was something to do with when trying to draw the player, the code got mad at me for not having them static, so I made them static :confused:

Sorry for my lack of elaboration, it was a while ago when I did it, I haven’t needed to try and use the class until now,

Thanks,

  • Dan

where did you put the translate code?

In WorldLogic:

package Logic;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import Player.MainPlayer;

public class WorldLogic {

	public static double x;
	public static double y;
	public static double xspeed;

	public WorldLogic() {
		GL11.glTranslated(MainPlayer.x - Display.getWidth()/2, MainPlayer.y - Display.getHeight()/2, 0);
		x = 20;
		y = 0;
		//World scrolling speed
		xspeed = 0.4;
	}
	
	public void logic() {
		x+=xspeed;
		input();
	}
	
	
	public static void input() {
		if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
			xspeed = xspeed * 0.4;
		}
		System.out.println("Press Left");
		if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
			xspeed = xspeed * 0.4;
		}
		System.out.println("Press Right");
	}
}

Should it be somewhere else?

Thanks for your help, and Renoria is looking pretty neat :),

  • Dan

My player stays still :cranky: , can this be explained a bit more in depth.