LibGDX: Move camera with Touch
Asked Answered
S

2

5

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.

Segno answered 18/2, 2016 at 12:27 Comment(0)
M
2

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

Marimaria answered 18/2, 2016 at 14:1 Comment(3)
@Segno Accept this answer if it helped you out and consider upvoting it since I think it's a good answer.Salamis
screenX and screenY parameters are absolute values.Braw
This answer is wrong due to the fact that camera.position.set(camera.position.x + screenX, camera.position.y + screenY); sends the camera flying away since screenX and screenY return where the mouse is on the screen... Instead of screenX, some sort of "change in X" should be put in, and same for the Y...Buhr
G
18

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;
}
Geary answered 30/4, 2017 at 22:24 Comment(0)
M
2

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

Marimaria answered 18/2, 2016 at 14:1 Comment(3)
@Segno Accept this answer if it helped you out and consider upvoting it since I think it's a good answer.Salamis
screenX and screenY parameters are absolute values.Braw
This answer is wrong due to the fact that camera.position.set(camera.position.x + screenX, camera.position.y + screenY); sends the camera flying away since screenX and screenY return where the mouse is on the screen... Instead of screenX, some sort of "change in X" should be put in, and same for the Y...Buhr

© 2022 - 2024 — McMap. All rights reserved.