gdx.input.getY 被翻转

Gdx.input.getY is flipped(gdx.input.getY 被翻转)

本文介绍了gdx.input.getY 被翻转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 方法,您也永远不会使用它的结果.
  • You are never setting the batch projection matrix.
  • Even though you are using the 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:

  • https://github.com/libgdx/libgdx/wiki/Coordinate-systems
  • https://xoppa.github.io/blog/pixels/李>
  • https://github.com/libgdx/libgdx/wiki/Viewports

这篇关于gdx.input.getY 被翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:gdx.input.getY 被翻转

基础教程推荐