How to deal with different aspect ratios in libGDX?

后端 未结 7 2215
粉色の甜心
粉色の甜心 2020-11-29 16:16

I have implemented some screens using libGDX that would obviously use the Screen class provided by the libGDX framework. However, the implementation for these s

7条回答
  •  一个人的身影
    2020-11-29 16:56

    I'm using that method from http://blog.acamara.es/2012/02/05/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/

    I made this function that returns the point on the screen touched, scaled down to the game position you want.

    public Vector2 getScaledPos(float x, float y) {
        float yR = viewport.height / (y - viewport.y);
        y = CAMERA_HEIGHT / yR;
    
        float xR = viewport.width / (x - viewport.x);
        x = CAMERA_WIDTH / xR;
    
        return new Vector2(x, CAMERA_HEIGHT - y);
    }
    

    Use this in the touchdown / up/ drag and you can check for touch in your game.

提交回复
热议问题