1
\$\begingroup\$

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 :)

\$\endgroup\$
1

2 Answers 2

1
\$\begingroup\$

I was able to figure out how to get the whole "camera" to work through the question I posed and subsequently answered on stackoverflow: https://stackoverflow.com/questions/34385383/having-the-camera-positioning-on-the-player-in-java

\$\endgroup\$
0
\$\begingroup\$

I am unfamiliar with slick, but given that your camera has no dependencies on it, I believe you are missing the update call to the camera. You never move the camera to where the player is, so it can not be centered on the player.

Maybe this works in the end of your Game class' Update method:

camera.playerX = playerX;
camera.playerY = playerY;
camera.update();
\$\endgroup\$
6
  • \$\begingroup\$ I have just tried that and I ran the game and checked the camera x and y coords and it's really wierd, x for the camera can only go up to 64.0 and the y value for the camera only stays at 0 \$\endgroup\$
    – BlueJet
    Commented Dec 21, 2015 at 12:23
  • \$\begingroup\$ You are restricting only your camX in your Camera update function, and not camY. You need to check that your offset restrictions are correct. \$\endgroup\$
    – Lasse
    Commented Dec 21, 2015 at 12:26
  • \$\begingroup\$ I think I see what is happening to the camera; essentially, the camera isn't actually the whole Viewport, the camera is just a tiny part of the actually viewing screen, but I don't know how I am going to fix that; aside, I made modifications to the code but it is still the same as the code snippets I have posted above \$\endgroup\$
    – BlueJet
    Commented Dec 21, 2015 at 12:27
  • \$\begingroup\$ The camera is just a point in the world that defines the origin of your drawn stuff. \$\endgroup\$
    – Lasse
    Commented Dec 21, 2015 at 12:34
  • \$\begingroup\$ Yes I understand that, but it is not acting like it. Though I think I got it, I changed the graphics.translate to be graphics.translate(0, 0); sprite.draw(playerX, playerY); graphics.translate(-Main.WIDTH, -MAIN.HEIGHT); So now it puts the player at the center of the screen, but I think that defeats the purpose of having the camera mve as the player moves though :/. \$\endgroup\$
    – BlueJet
    Commented Dec 21, 2015 at 12:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .