Android和桌面上的坐标是否相反? (LibGDX)

时间:2015-08-19 21:06:52

标签: java android libgdx

我的Android和桌面上的坐标相反。换句话说,(0,0)是Android的左上角,左下角是桌面。这是因为我设置了:

    cam.setToOrtho(false, GateRunner.WIDTH, GateRunner.HEIGHT);

(注意错误)

我已经以这种方式创建了我的整个屏幕,因此我无法将其更改为true。基本上,我有一个按钮,但是当我在Android设备上的正确位置点按它时,即使它在桌面上有效,它也无法正常工作。在Android上,我必须在不同的地方点击它。

我的touchDown():

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        float pointerX = InputTransform.getCursorToModelX(GateRunner.WIDTH, screenX);
        float pointerY = InputTransform.getCursorToModelY(GateRunner.HEIGHT, screenY);

        if(playButtonSprite.getBoundingRectangle().contains(pointerX, pointerY)) //Play button
        {
            game.setScreen(new PlayScreen(game));
            dispose();
        }

        return true;
    }

我的InputTransform:

public class InputTransform {
    public static float getCursorToModelX(int screenX, int cursorX)
    {
        return (((float)cursorX) * GateRunner.WIDTH) / ((float)screenX);
    }

    public static float getCursorToModelY(int screenY, int cursorY)
    {
        return ((float)(screenY - cursorY)) * GateRunner.HEIGHT / ((float)screenY) ;
    }
}

如何解决这个问题,以便坐标系保持不变 - 左下角为(0,0) - 桌面和Android?

2 个答案:

答案 0 :(得分:1)

您不能直接使用触摸坐标。您需要使用绘制按钮的SpriteBatch将您使用的相机取消投影。

void manageInput(char box[][width]){
    char move[4];
    char input[16]; 
    while(1){

            scanf("%s", input);
            int i = 0;
            while(input[i] != 0){

                    if(input[i] != ' ' && input[i] != "\n"){
                            move[i] = input[i];
                    }
                    i++;
            }
            printf("%s\n", move);

            makeMove(box, move);
            printBox(box, height, width);
            // TODO
            if(move[0] == 'x'){
                    exit(0);
            }
    }

现在private final Vector3 tmpVec3 = new Vector3(); public boolean touchDown(int screenX, int screenY, int pointer, int button) { tmpVec3.set(screenX, screenY); camera.unproject(tmpVec3); } 包含相机坐标中的触摸位置。

答案 1 :(得分:-1)

不幸的是,这是多平台支持的代价。

我建议您创建一个合适的抽象,隐藏相机设置逻辑,并为Android和桌面提供两种不同的实现。希望它有效。