LibGDX和Box2d的触摸不准确

3

虽然我非常喜欢LibGDX+Box2d的魔力,但在我的第一个项目中,对齐所有内容变得很痛苦。

我现在面临的问题是,即使我的精灵和DebugRenderer对齐了,当我触摸屏幕(在Nexus 5上测试)时,我得到了错误的坐标。

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

相关的代码非常简单:

    // 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;

创建游戏:
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);
}

渲染代码:
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);
}

我的机箱触摸下降:

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);
}

尝试使用camera.unproject,但它给我与touchDown参数相同的值。

还实现了一个handle方法:

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;
}

(为了调试,我删除了box2d和世界坐标系之间的所有转换,因此在此演示中,1像素=1米(显然我不是真的这样做!))

注意:X轴上也存在不精确性。

1个回答

0

我假设您想要从触摸点获取世界坐标?

如果是这样,您正在反投影错误的向量,应该使用:

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

这将屏幕坐标转换为世界坐标。 然后获取那些转换后的坐标:

touch.x, touch.y

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接