与LibGDX和Box2d不准确的接触

时间:2014-03-17 01:02:33

标签: touch libgdx box2d

虽然我真的很喜欢LibGDX + Box2d魔法,但是在我的第一个项目中对齐所有东西都很痛苦。

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

  

First example - the blue dot is the touch point

     

在这张图片中,我在形状外触摸,但我得到的坐标实际上在形状内,因此testPoint返回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

     

在这张图片中,我在形状内触摸,但我得到的坐标实际上在形状之外,因此testPoint返回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);
}

我的方框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);
}

尝试了camera.unproject的东西,但它给了我与touchDown的参数相同的值。

还实现了一个句柄方法:

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和世界坐标中删除了每个转换以便调试它,然后在此演示中1px = 1m(我显然不是这样做的真实!)

OBS:不精确也发生在X轴上。

1 个答案:

答案 0 :(得分:0)

我假设你想从接触点获得世界坐标?

如果是这样,你正在取消错误的向量,你应该使用:

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

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

touch.x, touch.y