1
\$\begingroup\$

I'm having some trouble getting the camera to even work at times. Thankfully, its some-what working now. I'm trying to get the camera to follow the players movement but nothing happens.

(cut out the parts that are useless)

public class GameCore extends Game  {
private OrthographicCamera cam;
private Player player;
private ShapeRenderer shape;

@Override
public void create() {      
    cam = new OrthographicCamera(1280 ,720);
    cam.translate(Gdx.graphics.getWidth()/2, Gdx.graphics.getWidth()/2, 0);
    cam.update();


    shape = new ShapeRenderer();
    player = new Player();
    player.setPos((int)cam.position.x, (int)cam.position.y);

}

@Override
public void dispose() {

}

@Override
public void render() {      
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    super.render();
    player.render(shape, cam);
    player.update();

}

GameCore class (my main one)

public class Player {
    public int x,y;
    public int dx, dy;
    public int width, height;
    public double area;
    public int hp;

    public Player(){
        hp = 100;
        width = 1150;
        height = 500;
        area = width*height;
        x = Gdx.graphics.getWidth()/2;
        y = Gdx.graphics.getHeight()/2;
        dx = 5;
        dy = 5;

    }
    public void render(ShapeRenderer shape, OrthographicCamera camera){
        shape.setProjectionMatrix(camera.combined);
        shape.begin(ShapeType.Line);
        shape.circle(x, y, 32);
        shape.end();

    }
    public void update(){
        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            x -= dx;
    }
        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            x += dx;
    }
        if(Gdx.input.isKeyPressed(Input.Keys.S)){
            y -= dy;
    }
        if(Gdx.input.isKeyPressed(Input.Keys.W)){
            y += dy;
    }
}
    public void setPos(int x, int y){
        x = this.x;
        y = this.y;
    }


    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
}

Player class

Hopefully someone here can also point out if i'm doing something wrong with the camera -_-. The docs don't really provide much information on how these classes are used. Thank you! :D

\$\endgroup\$
2
\$\begingroup\$

This is very simple, actually. You don't need to translate the camera (that is dealing with Matrices, I think?) using the "translate" method. You can simple set the camera's position to your sprite's position (assuming you use the x-y coordinates where (0, 0) is the bottom-left corner. Or extend the "Sprite" class; either way should work).

In your camera object, use the "position" Vector3 (x and y only):

cam.position.x = player.getX();
cam.position.y = player.getY();
cam.update();

I'll rewrite your code for you:

public class GameCore extends Game  {
private OrthographicCamera cam;
private Player player;
private ShapeRenderer shape;

@Override
public void create() {      
    cam = new OrthographicCamera(1280 ,720);
    cam.update();

    shape = new ShapeRenderer();
    player = new Player();
    player.setPos((int)cam.position.x, (int)cam.position.y);
}

@Override
public void dispose() {
}

@Override
public void render() {      
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    super.render();

    //Here we set the camera's position to the player's position
    cam.position.x = player.getX();
    cam.position.y = player.getY();
    cam.update();

    //Then we render the player        
    player.render(shape, cam);
    player.update();
}

Your Player class should stay the same, I think. If all is well, your player should stick in the middle of the screen when it moves, and the camera should move with the player.

\$\endgroup\$
  • 1
    \$\begingroup\$ It worked perfectly, thank you! I couldn't believe the answer was so simple! \$\endgroup\$ – MrPork Sep 10 '14 at 8:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.