I need some help with getting my camera to work in java for a game that I am making. The problem is is that I've never made a camera in a game before and this is my first time trying to make one in a game, but whenever I test it to see if it works, the screen doesn't center on the player. I've tried many times and checked a couple of resources online but I still haven't figured it out. The code is here:
Camera.class:
public class Camera {
private float playerX, playerY;
private float mapX, mapY;
private float offsetMaxX = mapX - Main.WIDTH;
private float offsetMaxY = mapY - Main.HEIGHT;
private float offsetMinX = 0;
private float offsetMinY = 0;
private float camX;
private float camY;
public Camera (float playerX, float playerY, float mapX, float mapY) {
super ();
this.playerX = playerX;
this.playerY = playerY;
this.mapX = mapX;
this.mapY = mapY;
update();
}
public Camera () {
}
public void update (){
camX = playerX - Main.WIDTH / 2;
camY = playerY - Main.HEIGHT / 2;
if (camX > offsetMaxX) {
camX = offsetMaxX;
} else if (camX < offsetMinX) {
camX = offsetMinX;
}
}
public float getCamX() {
return camX;
}
public float getCamY() {
return camY;
}
public float getMapX() {
return mapX;
}
public float getMapY() {
return mapY;
}
public float getPlayerY() {
return playerY;
}
public float getPlayerX() {
return playerX;
}
}
and my main class is here:
public class Game extends BasicGameState {
// Player variables
public SpriteSheet playerSS = null;
protected InputHandler inputHandler;
public float playerX = Main.WIDTH/2, playerY = Main.HEIGHT/2;
private Animation sprite, runningAnimationLEFT, runningAnimationRIGHT, runningAnimationUP, runningAnimationDOWN;
private Animation standLEFT, standRIGHT, standUP, standDOWN; //THIS IS TO PREVENT ANIMATIONS FROM CONTINUING AFTER THE PLAYER STOPS
private int duration = 300;
public boolean isMoving, isLeft, isRight, isUp, isDown;
//Map
private Image testMap; //This is a test map
private float mapX = 0, mapY = 0;
//Camera
public Camera camera;
public Game (int stateID) {}
@Override
public int getID() {
return Main.game; //GAMESTATE = 1
}
@Override
public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException {
playerSS = new SpriteSheet("res/characters/player_sprite_sheet.png", 50, 75);
testMap = new Image ("res/grassMap.png");
camera = new Camera();
// ANIMATIONS
runningAnimationDOWN = new Animation();
runningAnimationDOWN.setAutoUpdate(true);
runningAnimationDOWN.addFrame(playerSS.getSprite(1, 0), duration);
runningAnimationDOWN.addFrame(playerSS.getSprite(0, 0), duration);
runningAnimationDOWN.addFrame(playerSS.getSprite(2, 0), duration);
runningAnimationLEFT = new Animation();
runningAnimationLEFT.setAutoUpdate(true);
runningAnimationLEFT.addFrame(playerSS.getSprite(1, 1), duration);
runningAnimationLEFT.addFrame(playerSS.getSprite(0, 1), duration);
runningAnimationLEFT.addFrame(playerSS.getSprite(2, 1), duration);
runningAnimationRIGHT = new Animation();
runningAnimationRIGHT.setAutoUpdate(true);
runningAnimationRIGHT.addFrame(playerSS.getSprite(1, 2), duration);
runningAnimationRIGHT.addFrame(playerSS.getSprite(0, 2), duration);
runningAnimationRIGHT.addFrame(playerSS.getSprite(2, 2), duration);
runningAnimationUP = new Animation();
runningAnimationUP.setAutoUpdate(true);
runningAnimationUP.addFrame(playerSS.getSprite(1, 3), duration);
runningAnimationUP.addFrame(playerSS.getSprite(0, 3), duration);
runningAnimationUP.addFrame(playerSS.getSprite(2, 3), duration);
standDOWN = new Animation();
standDOWN.setAutoUpdate(true);
standDOWN.addFrame(playerSS.getSprite(1, 0), duration);
standLEFT = new Animation();
standLEFT.setAutoUpdate(true);
standLEFT.addFrame(playerSS.getSprite(1, 1), duration);
standRIGHT = new Animation();
standRIGHT.setAutoUpdate(true);
standRIGHT.addFrame(playerSS.getSprite(1, 2), duration);
standUP = new Animation();
standUP.setAutoUpdate(true);
standUP.addFrame(playerSS.getSprite(1, 3), duration);
sprite = standDOWN;
}
@Override
public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta) throws SlickException {
inputHandler.updateKeyboardRelay(gameContainer);
isMoving = false;
if (inputHandler.keyUP) {
isUp = true;
isDown = false;
isLeft = false;
isRight = false;
isMoving = true;
playerY -= delta * 0.1f;
} else if (inputHandler.keyDOWN) {
isDown = true;
isUp = false;
isLeft = false;
isRight = false;
isMoving = true;
playerY += delta * 0.1f;
} else if (inputHandler.keyLEFT) {
isLeft = true;
isUp = false;
isDown = false;
isRight = false;
isMoving = true;
playerX -= delta * 0.1f;
} else if (inputHandler.keyRIGHT) {
isRight = true;
isUp = false;
isDown = false;
isLeft = false;
isMoving = true;
playerX += delta * 0.1f;
}
if (isMoving) {
if (inputHandler.keyUP) {
sprite = runningAnimationUP;
sprite.update(delta);
} else if (inputHandler.keyDOWN) {
sprite = runningAnimationDOWN;
sprite.update(delta);
} else if (inputHandler.keyLEFT) {
sprite = runningAnimationLEFT;
sprite.update(delta);
} else if (inputHandler.keyRIGHT) {
sprite = runningAnimationRIGHT;
sprite.update(delta);
}
} else {
if (isDown) {
sprite = standDOWN;
sprite.update(delta);
} else if (isUp) {
sprite = standUP;
sprite.update(delta);
} else if (isLeft) {
sprite = standLEFT;
sprite.update(delta);
} else if (isRight) {
sprite = standRIGHT;
sprite.update(delta);
}
}
}
@Override
public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
testMap.draw(mapX, mapY);
graphics.translate(camera.getCamX(), camera.getCamY());
sprite.draw(playerX, playerY);
graphics.translate(-camera.getCamX(), -camera.getCamY());
graphics.drawString("Player X: " + playerX, 400, 400);
graphics.drawString("Player Y: " + playerY, 400, 380);
}
}
Any help with getting the camera to function properly would be very helpful :)