虽然我真的很喜欢 LibGDX+Box2d 的魔力,但在我的第一个项目中对齐所有内容还是很痛苦的。
我现在面临的问题是,即使我的 sprite 和 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 的参数相同的值。
还实现了句柄方法:
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 轴上。
最佳答案
我假设您想要接触点的世界坐标?
如果是这样,你正在反投影错误的向量,你应该使用:
Vector3 touch = new Vector3(x, y, 0);
camera.unproject(touch);
这会将屏幕坐标转换为世界坐标。 然后获取那些转换后的坐标:
touch.x, touch.y
关于touch - 与 LibGDX 和 Box2d 的接触不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22445298/