【问题标题】:Inaccurate touch with LibGDX and Box2dLibGDX 和 Box2d 的触摸不准确
【发布时间】:2014-03-17 01:02:32
【问题描述】:

虽然我真的很喜欢 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 轴上。

【问题讨论】:

    标签: touch libgdx box2d


    【解决方案1】:

    我假设你想要接触点的世界坐标?

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

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

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

    touch.x, touch.y
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多