How to Deal with Different Aspect Ratios in Libgdx

How to deal with different aspect ratios in libGDX?

How to do it nowadays:

Since this is one of the most famous questions on libgdx here, I'll give it a little update:

LibGDX v1.0 introduced Viewport to handle this problem. It is a lot easier to use and the scaling strategy is pluggable, which means a single line can change the behaviour and you can play with it and see which one fits your game the best.

Everything you need to know about it can be found here.

How to setup libgdx game for different screen sizes?

You need to use a viewport. There are different kinds of viewports you can use to make sure your game looks good on all screen sizes.

links:

LibGDX wiki

Aspect Ratios & Viewports tutorial by Brent Aureli

Aspect Ratio for ShapeRender in LibGDX (OpenGL)

Before shapeRenderer().begin, call shapeRenderer().setProjectionMatrix(camera.combined); This is also necessary for SpriteBatches.

Multiple Stages on Same screen with different viewport LIBGDX

this answer worked for me. Just for reference

You would need a second viewport, probably ExtendViewport with the
same virtual dimensions as your FitViewport.

//create:
fitViewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
extendViewport = new ExtendViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

//resize:
fitViewport.update(width, height);
extendViewport.update(width, height);

//render:

fitViewport.apply();
batch.setProjectionMatrix(fitViewport.getCamera().combined);
batch.begin();
//draw game
batch.end();

extendViewport.apply();
batch.setProjectionMatrix(extendViewport.getCamera().combined);
batch.begin();
//draw stuff in border
batch.end();

If you want to be sure the border stuff doesn't overlap your game, you
could swap the draw order above.

Or you could just use ExtendViewport to begin with for everything.

LibGDX - how do I make my game work on all screen resolutions?

Having a static width on your camera is perfectly ok as it gives your game a single fixed dimension that you can work with reliably. This works because the camera is used to define world coordinates which are not always one to one with screen coordinates.

Your issues come from the fixed camera height. Having a fixed height will cause your screen to stretch taller or shorter depending on the aspect ratio of the device screen. If you want to account for the different aspect ratios, you'll need to multiply your camera height (currently 480) by the display ratio. You can get the screen ratio by dividing the height by the width by the height. This would look something like:

float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();

OrthographicCamera camera = new OrthographicCamera(800, 480 * (height / width));

As pointed out by BennX in the comments, LibGDX introduced viewports which allow you do very much the same thing as above, just in a different way. To achieve the same effect as I outlined above, only using a viewport, you'd use an ExtendViewport. What this does is maintain the world size in one direction while stretching it in the other direction. So the world will first scale up to fill the screen, then the shorter dimension is expanded while maintaining aspect ratio. To create this type of viewport, it'd look something like this:

OrthographicCamera camera = new OrthographicCamera(800, 480);
ExtendViewport viewport = new ExtendViewport(800, 480, camera);

The viewport above will have a minimum width of 800 and a minimum height of 480. One of these values will be the same after the viewport is applied and the other will change based on the aspect ratio of the screen. More narrow screens will have more vertical space while wider screens will have more horizontal space. For more on viewports, visit the LibGDX wiki page here.



Related Topics



Leave a reply



Submit