LibGDX Drawing sprites when moving orthographic camera

| | August 18, 2015

I’ve been having this problem for a long time and I just can’t seem to find the exact problem. I have a game where the map is 480×1600 and my camera has a view of 480×800. I have a button that when pressed, allows the user to place a platform on the map, and since the map is too big to fit on the screen, I made it so the user can move the camera up and down the map by dragging.

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    Vector3 pos = new Vector3(screenX, screenY, 0);
    cam.unproject(pos);
    cam.position.y = pos.y;
    return true;
}

Method that controls when the user tries to put a platform down

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector3 pos = new Vector3(screenX, screenY, 0);
    cam.unproject(pos);
    Gdx.app.log(Game.LOG, "X Coordinate: " + pos.x + " Y Coordinate: " + pos.y);

    if (GameScreen.createPlatform == true) {
        world.setPlatform(new Vector2(pos.x - 0.6f, pos.y - 0.1f), 1);
        GameScreen.createPlatform = false;
        return true;
    }

    ...
}

Where I render the platform sprites

public void render(float delta) { 
    ...
    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.begin();
    for (Sprite platformSprite: world.getPlatformSprites()) {
        platformSprite.draw(spriteBatch);
    }
    spriteBatch.end();
    ... 


}

The game works fine when I don’t move the camera. But when I do, the platforms aren’t placed where I click, they’re always either higher or lower where I actually clicked. Also, the platforms are always placed somewhere where the camera was originally looking before anything has been moved.

I think it’s doing this is because for some reason, the coordinates never actually change. So wherever I am on the map, the top of the screen is always 120 and the bottom is always 90. This also causes objects on the map to have different coordinates if I move the camera.

This problem has me completely lost and I would appreciate any help.

One Response to “LibGDX Drawing sprites when moving orthographic camera”

  1. cam.unproject(touchPos) will give you the coordinates as they relate to your screen and cam.project(touchPos) will give you the coordinates as they relate to the game world.

    gamedev.stackexchange.com/a/27793/20399

    so in here:

    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 pos = new Vector3(screenX, screenY, 0);
        cam.unproject(pos);
        Gdx.app.log(Game.LOG, "X Coordinate: " + pos.x + " Y Coordinate: " + pos.y);
    
        if (GameScreen.createPlatform == true) {
            world.setPlatform(new Vector2(pos.x - 0.6f, pos.y - 0.1f), 1);
            GameScreen.createPlatform = false;
            return true;
        }
    
        ...
    }
    

    make it like this:

    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 pos = new Vector3(screenX, screenY, 0);
        cam.project(pos); //changed
        Gdx.app.log(Game.LOG, "world X Coordinate: " + pos.x + " world Y Coordinate: " + pos.y);
    
        if (GameScreen.createPlatform == true) {
            world.setPlatform(new Vector2(pos.x - 0.6f, pos.y - 0.1f), 1);
            GameScreen.createPlatform = false;
            return true;
        }
    
        ...
    }
    

Leave a Reply