libgdx coordinate system differences between rendering and touch input(渲染和触摸输入之间的 libgdx 坐标系差异)
问题描述
我有一个渲染 PNG 图像的屏幕(BaseScreen 实现 Screen 接口).单击屏幕时,它会将角色移动到触摸的位置(用于测试目的).
I have a screen (BaseScreen implements the Screen interface) that renders a PNG image. On click of the screen, it moves the character to the position touched (for testing purposes).
public class DrawingSpriteScreen extends BaseScreen {
private Texture _sourceTexture = null;
float x = 0, y = 0;
@Override
public void create() {
_sourceTexture = new Texture(Gdx.files.internal("data/character.png"));
}
.
.
}
在屏幕渲染过程中,如果用户触摸屏幕,我会抓取触摸的坐标,然后用这些坐标来渲染角色图像.
During rendering of the screen, if the user touched the screen, I grab the coordinates of the touch, and then use these to render the character image.
@Override
public void render(float delta) {
if (Gdx.input.justTouched()) {
x = Gdx.input.getX();
y = Gdx.input.getY();
}
super.getGame().batch.draw(_sourceTexture, x, y);
}
问题是绘制图像的坐标从左下角开始(如 LibGDX Wiki 中所述),而触摸输入的坐标从左上角开始.所以我遇到的问题是我点击右下角,它将图像移动到右上角.我的坐标可能是 X 675 Y 13,触摸时它会在屏幕顶部附近.但是字符显示在底部,因为坐标从左下角开始.
The issue is the coordinates for drawing the image start from the bottom left position (as noted in the LibGDX Wiki) and the coordinates for the touch input starts from the upper left corner. So the issue I'm having is that I click on the bottom right, it moves the image to the top right. My coordinates may be X 675 Y 13, which on touch would be near the top of the screen. But the character shows at the bottom, since the coordinates start from the bottom left.
为什么是什么?为什么坐标系颠倒了?我是否使用了错误的对象来确定这一点?
Why is what? Why are the coordinate systems reversed? Am I using the wrong objects to determine this?
推荐答案
为了检测碰撞,我使用 camera.unproject(vector3)
.我将 vector3
设置为:
To detect collision I use camera.unproject(vector3)
. I set vector3
as:
x = Gdx.input.getX();
y = Gdx.input.getY();
z=0;
现在我在 camera.unproject(vector3)
中传递这个向量.使用这个向量的 x
和 y
来绘制你的角色.
Now I pass this vector in camera.unproject(vector3)
. Use x
and y
of this vector to draw your character.
这篇关于渲染和触摸输入之间的 libgdx 坐标系差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:渲染和触摸输入之间的 libgdx 坐标系差异
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01