如何在所有设备中使用相同的分辨率在 LibGdx 中获取触摸坐标?
How do I get a touch coordinates in LibGdx using the same resolution in al devices?
所以我使用这段代码来获取触摸在屏幕上的坐标。
if(Gdx.input.justTouched()){
System.out.println("X= "+Gdx.input.getX()+"Y= "+Gdx.input.getY());
}
但是如果我有一个分辨率为 1280x960 的设备并且我有一个 800x600 的正交相机。
如何获取相机内部坐标而不是设备触摸坐标
"for example if I touch the limit of the screen on the x axis I want to get 800 instead of 1280"
正确的做法是用相机从屏幕坐标给你世界坐标。
例如...
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY());
camera.unproject(mousePos); // mousePos is now in world coordinates
这样一来,如果您更换相机(缩放/旋转/其他),一切仍然有效。
如果您使用的是视口,那么您应该改用视口的 unproject 方法,因为它可能需要处理其他问题(例如,允许 FitViewport 中的信箱)。
注意 - 在我给出的示例中,最好重用单个 Vector3 实例,但我希望示例尽可能简单。
所以我使用这段代码来获取触摸在屏幕上的坐标。
if(Gdx.input.justTouched()){
System.out.println("X= "+Gdx.input.getX()+"Y= "+Gdx.input.getY());
}
但是如果我有一个分辨率为 1280x960 的设备并且我有一个 800x600 的正交相机。
如何获取相机内部坐标而不是设备触摸坐标
"for example if I touch the limit of the screen on the x axis I want to get 800 instead of 1280"
正确的做法是用相机从屏幕坐标给你世界坐标。
例如...
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY());
camera.unproject(mousePos); // mousePos is now in world coordinates
这样一来,如果您更换相机(缩放/旋转/其他),一切仍然有效。
如果您使用的是视口,那么您应该改用视口的 unproject 方法,因为它可能需要处理其他问题(例如,允许 FitViewport 中的信箱)。
注意 - 在我给出的示例中,最好重用单个 Vector3 实例,但我希望示例尽可能简单。