JeremeiBou
JeremeiBou

Reputation: 61

How to put the player in the center of a libgGDX camera

I just recently started using libGDX to make games, and I am trying to implement an orthographic camera to fallow the player around when he moves (making a 2D side scroller). For some reason that I don't understand when I do everything that should make the camera draw the player in the center of the screen, the player is drawn slightly shifted towards the up-right corner.

I been searching all day trying different things, but I haven't found any solutions.

If it helps my player sprite is a square 128 x 128 image and my monitor size is 1366x768

Here is my gameScreen code :

 package com.inertiafall.inertiaengine.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector3;
import com.inertiafall.inertiaengine.InertiaEngine;
import com.inertiafall.inertiaengine.InputHandler;
import com.inertiafall.inertiaengine.entities.Player;

public class GameScreen implements Screen {
    InertiaEngine game;
    SpriteBatch batch;
    Player player;

    OrthographicCamera cam;
    float width, height;
    ShapeRenderer sr;

    public GameScreen(InertiaEngine game){
        this.game = game;
    }

    public void show() {    
        player = new Player(5, 5);  
        player.init();
        width = Gdx.graphics.getWidth();
        height = Gdx.graphics.getHeight();

        cam = new OrthographicCamera(width, height);
        cam.setToOrtho(false);
        cam.position.set(player.getX(), player.getY(), 0);
        cam.update();

        batch = new SpriteBatch();
        batch.setProjectionMatrix(cam.combined);


        sr = new ShapeRenderer();
    }

    public void update(float delta){
        checkKeys();
        player.update(delta);
        cam.position.set(player.getX(), player.getY(), 0);
        cam.unproject(new Vector3(player.getX(), player.getY(), 0));
        cam.update();
        batch.setProjectionMatrix(cam.combined);    
        sr.setProjectionMatrix(cam.combined);
    }

    public void render(float delta) {
        update(delta);  
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        batch.begin();
        player.render(batch);
        batch.end();        

        sr.begin(ShapeType.Line);
        player.renderDebug(sr); 
        sr.end();
    }

    public void resize(int width, int height) {

    }

    //check key events  
    private void checkKeys(){       
        if(InputHandler.keyPress(InputHandler.getExitKey())){
            exit();
        }
    }

    //key events
    private void exit(){
        Gdx.app.exit();
    }

    public void hide() {
        dispose();
    }

    public void pause() {}

    public void resume() {}

    public void dispose() {
        batch.dispose();
        player.dispose();
        sr.dispose();
    }

}

Upvotes: 0

Views: 1396

Answers (1)

Louis Fellows
Louis Fellows

Reputation: 544

I think it might be where you're setting the camera to player.getX() or getY(). That X and Y value points to the corner of the player sprite. Try setting the camera to

player.getX() + (player.getWidth() / 2)

and

player.getY() + (player.getHeight() / 2)

which should be the center of the player sprite.

Upvotes: 2

Related Questions