Gdx.input.getY is flipped(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 被翻转
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01