Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx viewport fit different screens

I'm having a problem with fitting my game on different screens. I declared a viewport width and height in my stage class.

public static final int VIEWPORT_WIDTH = 20;
public static final int VIEWPORT_HEIGHT = 30;

And i set up the camera

camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);

And i set the screen width and height in desktop app to this

public static final int APP_WIDTH = 800;
public static final int APP_HEIGHT = 900;

On desktop everything looks fine but on my android device it looks stretched upwards. I read the documentation about viewports but how can I implement it here using these variables? I know similar questions have been asked but I would like to know viewports can be implemented using these specific variables, that way I might understand it better.

like image 602
DreamsInHD Avatar asked Dec 06 '25 08:12

DreamsInHD


1 Answers

You probably want to use an ExtendViewport. This extends the world 1 way to fill the entire screen.

your code would look something like this:

private Viewport viewport;
private Camera camera;

public void create() {
    camera = new OrthographicCamera();
    viewport = new ExtendViewport(VIEWPORT_WIDTH , VIEWPORT_HEIGHT, camera);
}


public void resize(int width, int height) {
    viewport.update(width, height);
}

Read more here: https://github.com/libgdx/libgdx/wiki/Viewports#extendviewport

like image 179
p.streef Avatar answered Dec 11 '25 08:12

p.streef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!