I'm making a 2D game (non tile based) and want to make my camera follow my character. The following is how the game is implemented:
The Game is pixel based and so this is how it looks:
The window is sized 840x490 as you can see, and the map is 1260x735. My Camera
class holds the X and Y positions of the Camera, and also its size. The code:
public class Camera{
public Level level;
public GameContainer gc;
public static final int width = Game.displayWidth; // 840
public static final int height = Game.displayHeight; // 490
public static float posX = 0;
public static float posY = 0;
public static int maxX = 0;
public static int maxY = 0;
public Camera(Level level){
this.level = level;
this.maxX = this.level.worldMap.getWidth();
this.maxY = this.level.worldMap.getHeight();
}
}
Right now I move the camera by using the arrow keys (to test) and the cameras XY position is used to g.translate()
the Graphics context before we draw everything else, so thats how I achieve moving with arrows. This is my Render method:
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
int offSetX = 10;
int offSetY = 10;
g.translate(-camera.posX, -camera.posY);
worldMap.draw(0 ,0);
for(GameObject o : objects){
o.drawObj(container, game, g);
if(quit == true){
Shape s = o.getBoundsBox();
g.draw(s);
}
}
}
I want it so the player is always being followed by the camera if he steps close to whichever edge he does. But how can I detect this? I would ideally have a coordinate system inside the camera and detect it that way by checking the X and Y values, but I cant find a way to have a coordinate system inside the Camera window.
Any other ideas in to how to approach this?
I actually found the solution to this while waiting for replies here haha. Well its the start of the solution but I got it following my player at the first try.
Basically I just made a method to move the camera in my MoveController
class, which is where I do the movements of GameObjects
, and this method is called every time my player moves and it updates based on which move direction it is.
The method looks exactly like my moveCamera method, except the var holds the camera object. So in my moveMob method, I have this condition:
if(mob instanceof Player){ moveCameraPos(dir,(Player)mob,delta); }
And then this method looks like this:
public void moveCameraPos(int dir, Player p, int delta){
switch(dir){
default:
break;
case Play.UP:
if(camera.posY>0)
camera.posY -= (delta * .1f) * p.speed /2;
break;
case Play.DOWN:
if(camera.posY < 735 - 490)
camera.posY += (delta * .1f) * p.speed /2;
break;
case Play.LEFT:
if(camera.posX > 0)
camera.posX -= (delta * .1f) * p.speed /2;
break;
case Play.RIGHT:
if(camera.posX < 1260 - 840)
camera.posX += (delta * .1f) * p.speed /2;
break;
case Play.UPRIGHT:
if(camera.posX>0 && camera.posY>0){
camera.posY -= ((delta * .1f) * p.speed)/2;
camera.posX += ((delta * .1f) * p.speed)/2;
}
break;
case Play.UPLEFT:
if(camera.posX>0 && camera.posY>0){
camera.posY -= ((delta * .1f) * p.speed)/2;
camera.posX -= ((delta * .1f) * p.speed)/2;
}
break;
case Play.DOWNRIGHT:
if(camera.posX < 1260 -840 && camera.posY<735-490){
camera.posY += ((delta * .1f) * p.speed)/2;
camera.posX += ((delta * .1f) * p.speed)/2;
}
break;
case Play.DOWNLEFT:
if(camera.posX > 0 && camera.posY<735-490){
camera.posY += ((delta * .1f) * p.speed)/2;
camera.posX -= ((delta * .1f) * p.speed)/2;
}
break;
}
}
I Still have questions about how I would go on to have a coordinates system in the camera ?