Gdx.input.getY被翻转 [英] Gdx.input.getY is flipped

查看:41
本文介绍了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:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆