LibGDX触摸协调混乱 [英] LibGDX touch coordination confusion

查看:84
本文介绍了LibGDX触摸协调混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,libGDX坐标系是默认设置的,因此(0,0)位于左下角,如下图所示:

as I understood, libGDX coordinate system set by default, so the (0,0) is located at the bottom left corner, like in the following picture:


(来源: gyazo.com )


(source: gyazo.com)

是否可以使其像默认默认位置位于左上角的JFrame一样工作?

Is it possible to make it work like JFrame, where the default position is at the top left corner?

无论如何,回到我的实际问题,android(或至少是我的手机)触摸坐标系正在全屏工作(当手机放在侧面时),因此默认(0,0)位置位于顶部右下角,如下图所示:

Anyhow, back to my actual question, android (OR at least my phone) touch coordinate system is working by full screen (when phone is laying on it's side) so the default (0, 0) position is located at the top right corner, like in the following picture:


(来源: gyazo.com )


(source: gyazo.com)

那么当我简单地这样做时会发生什么:

So what happens when I simply do this:

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    System.out.println("X: " + screenX  + " Y: " + screenY);
    this.instance.player.setLocation(screenX, screenY);
    return false;
}

我只是获得触摸屏的X,Y(基于android的全屏坐标系),然后在完全不同的libgdx坐标系上使用它,

I simply get the X, Y of touched screen (based on android's fullscreen coordinate system) and then use it on libgdx coordinate system which is totally different,

因此,如果我触摸右上角,我的蓝色矩形将显示在左下角.

so if I touched at the top right corner, my blue rectangle will appear at bottom left corner.

是否可以像在PC上那样使所有内容在左上角工作?

Is there a way to make everything work by the top left corner like on PC?

推荐答案

您将需要使用摄像头来执行此操作.如果您不使用相机,我强烈建议您添加一个,因为这样会使事情变得容易得多.只需调用camera.setToOrtho(true),它将把LibGDX的坐标系切换到您想要的坐标系,即左上角的(0,0).

You'll need to use a camera to do this. If you're not using a camera, I'd highly recommend adding one in, as it makes things a lot easier. Simply call camera.setToOrtho(true), which will switch LibGDX's coordinate system to the one you want, (0,0) in the top left.

要使用除Gdx.graphics.getWidth/Height()之外的特定视口宽度和高度来执行此操作,可以调用camera.setToOrtho(true, viewportWidth, viewportHeight).

To do this with a certain viewport width and height other than what you get from Gdx.graphics.getWidth/Height(), you can call camera.setToOrtho(true, viewportWidth, viewportHeight).

这篇关于LibGDX触摸协调混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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