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

查看:30
本文介绍了渲染和触摸输入之间的 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) 中传递这个向量.使用这个向量的 xy 来绘制你的角色.

Now I pass this vector in camera.unproject(vector3). Use x and y of this vector to draw your character.

这篇关于渲染和触摸输入之间的 libgdx 坐标系差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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