Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGDX: Move camera with Touch

I create a2D android game with LibGDX and I use an orthographic camera to move around the world.

In order to move the camera, the player should touch and drag the screen. So if you touch the screen and drag rightwards, the camera should move left. So it should be just like moving the section of a zoomed picture in the gallery. I hope you are following me.

This is the code:

public class CameraTestMain extends ApplicationAdapter {

    SpriteBatch batch;
    Texture img;
    OrthographicCamera camera;

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
        camera = new OrthographicCamera(1280, 720);
        camera.update();
    }

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

        handleInput();
        camera.update();

        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        batch.draw(img, 0, 0);
        batch.end();
    }

    public void handleInput() {

        // That is my problem

    }

}

I don't know what to write in the handleInput() method. I know that there is an interface called InputProcessor with the method touchDragged(int x, int y, int pointer) where you can handle Touch Drag but I have no idea how to use it.

Thanks for your ideas and your help.

like image 417
erik4thewinners Avatar asked Feb 18 '16 12:02

erik4thewinners


2 Answers

This question is old but, as Sierox said, the accepted answer will send the camera flying away, so here's another solution

Gdx.input.getDeltaX() will return the difference between the current pointer location and the last pointer location on X axis.

Gdx.input.getDeltaY() will return the difference between the current pointer location and the last pointer location on Y axis.

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    float x = Gdx.input.getDeltaX();
    float y = Gdx.input.getDeltaY();

    camera.translate(-x,y);
    return true;
}
like image 162
centenond Avatar answered Nov 02 '22 09:11

centenond


The InputProcessor is the interface that allows you to define what to do when some action (like for example touching screen) is being performed.

So the first thing is to implement the interface in your class:

    public class CameraTestMain extends ApplicationAdapter implements InputProcessor

then in create() method set the class as InputProcessor by calling:

    //create() method
    Gdx.input.setInputProcessor(this);

The second one is to implement touchDragged method. Notice that it's x and y parameters are just relative to last pointer position so to get actual position of pointer you should save it in some global variable in touchDown method. Of course you do not need here the absolute pointer position since you can just modify camera position instead of setting this.

    //touchDragged method
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        camera.position.set(camera.position.x + screenX, camera.position.y + screenY);

Remember to call camera.update() at the beginning of render() method!

To get more information take a look at this tutorial

like image 30
m.antkowicz Avatar answered Nov 02 '22 10:11

m.antkowicz