我在 LibGDX 中有一个问题,当我调用 Gdx.input.getY() 时,它会选择相对于屏幕中心位于应用程序另一侧的像素.
I have an issue in LibGDX where when i call upon Gdx.input.getY(), it selects a pixel that's on the other side of the application relative to the center of the screen.
public class Main extends ApplicationAdapter {
private SpriteBatch batch;
private Texture img;
private OrthographicCamera camera;
int xPos;
int yPos;
private Vector3 tp = new Vector3();
BitmapFont font;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("crosshair.png");
camera = new OrthographicCamera();
camera.setToOrtho(false, 1280, 720);
font = new BitmapFont();
}
@Override
public void render () {
yPos = Gdx.input.getY();
xPos = Gdx.input.getX();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.unproject(tp.set(xPos, yPos, 0));
batch.begin();
font.draw(batch,xPos + " , " + yPos, Gdx.input.getX() - 25, Gdx.input.getY() - 5);
batch.draw(img, xPos, yPos);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
}
用触摸位置减去视口高度是行不通的,因为那会用触摸坐标减去世界坐标.(即使对于像素完美投影,它也是 height - 1 - y
).而是使用 unproject 方法将触摸坐标转换为世界坐标.
Subtracting the viewport height with the touch location won't work, because that would be subtracting world coordinates with touch coordinates. (and even for a pixel perfect projection it would be height - 1 - y
). Instead use the unproject method to convert touch coordinates to world coordinates.
你的代码有两个问题:
unproject
方法,您也永远不会使用它的结果.unproject
method, you are never using its result.所以改为使用以下内容:
So instead use the following:
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
camera.unproject(tp.set(Gdx.input.getX(), Gdx.input.getY(), 0));
font.draw(batch,tp.x+ " , " + tp.y, tp.x - 25, tp.y - 5);
batch.draw(img, tp.x, tp.y);
batch.end();
}
我建议阅读以下页面,其中详细描述了这一点及其背后的原因:
I would suggest to read the following pages, which describe this and the reasoning behind it in detail:
这篇关于gdx.input.getY 被翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!