libgdx坐标系渲染和触摸输入之间的差异 [英] libgdx coordinate system differences between rendering and touch input

查看:151
本文介绍了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坐标系渲染和触摸输入之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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