xrash
xrash

Reputation: 961

Inaccurate touch with LibGDX and Box2d

Although I really liked the LibGDX+Box2d magic, it's being painful to align everything in my first project.

The problem I'm facing now is that even though my sprite and the DebugRenderer are aligned, when I touch the screen (testing on a Nexus 5), I got the wrong coordinates.

First example - the blue dot is the touch point

In this image, I am touching outside the shape, but the coordinates I get are actually inside the shape, and thus testPoint returns true:

TOUCH  126.25 253.0
ORIGIN 103.0  190.0
TIP    167.0  254.0
TEST   true

Second example - the blue dot is the touch point

In this image, I am touching inside the shape, but the coordinates I get are actually outside the shape, and thus testPoint returns false.

TOUCH  134.25 189.7
ORIGIN 103.0  190.0
TIP    167.0  254.0
TEST   false

The relevant code is pretty straightforward:

    // width and height of my Nexus 5 in portrait mode are 1080 x 1776
    public static final float VIRTUAL_WIDTH = 270.0f;
    public static final float VIRTUAL_HEIGHT = 444.0f;

Creating the game:

public void create() {
    camera = new OrthographicCamera();
    camera.setToOrtho(false, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
    camera.update();

    world = new World(new Vector2(0, 0), true);
    box = new Box(world, camera);
    ground = new Ground(world);
    leftWall = new Wall(world, Wall.LEFT);
    rightWall = new Wall(world, Wall.RIGHT);
    touchDebugger = new TouchDebugger();

    stage = new Stage();
    stage.setCamera(camera);
    stage.getSpriteBatch().setProjectionMatrix(camera.combined);
    stage.addActor(box);
    stage.addActor(ground);
    stage.addActor(leftWall);
    stage.addActor(rightWall);
    stage.addActor(touchDebugger);
    stage.addListener(box);
    stage.addListener(touchDebugger);
    Gdx.input.setInputProcessor(stage);

    debugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true);
}

The render code:

public void render() {
    // Update the camera.
    camera.update();
    camera.apply(Gdx.gl10); // Don't know about that, just found over the internet

    // Clear LibGDX OpenGL context.
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    // Physics world step.
    world.step(1/45f, 6, 6);

    // Scene2d rendering.
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();

    // Physics debugging.
    debugRenderer.render(world, camera.combined);
}

My box touchDown:

public void touchDown(float x, float y) {
    Vector2 touch = new Vector2(x, y);
    Vector3 cam = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(cam);

    boolean test = fixture.testPoint(touch.x, touch.y);

    Log.d("demo", "GDX    " + Gdx.input.getX() + "\t" + Gdx.input.getY());
    Log.d("demo", "CAM    " + cam.x + "\t" + cam.y);
    Log.d("demo", "TOUCH  " + touch.x + "\t" + touch.y);
    Log.d("demo", "ORIGIN " + (body.getPosition().x - width/2.0f) + "\t" + (body.getPosition().y - height/2.0f));
    Log.d("demo", "TIP    " + (body.getPosition().x + width/2.0f) + "\t" + (body.getPosition().y + height/2.0f));
    Log.d("demo", "TEST   " + test);
}

Tried that camera.unproject stuff but it gives me the same values as the arguments of touchDown.

Also got a handle method implemented:

public boolean handle(Event event) {
    String e = event.toString();
    if (e == "touchDown") {
        InputEvent ie = (InputEvent) event;
        touchDown(ie.getStageX(), ie.getStageY());
    } else if ( e == "touchUp") {
        InputEvent ie = (InputEvent) event;
        touchUp(ie.getStageX(), ie.getStageY());
    }

    return true;
}

(I removed every conversion from box2d and world coordinates in order to debug this, then 1px = 1m in this demo (I am obviously not doing this for real!))

OBS: The imprecision also happens in the X axis.

Upvotes: 3

Views: 431

Answers (1)

ADDER
ADDER

Reputation: 404

I am assuming you want the world coordinates from the touchpoint?

If so, you are unprojecting the wrong vector, you should use:

Vector3 touch = new Vector3(x, y, 0);
camera.unproject(touch);

This converts the screen coordinates to world coordinates. And then to get those converted coordinates:

touch.x, touch.y

Upvotes: 0

Related Questions