1
\$\begingroup\$

Short question about the scaling in libGDX. At the moment i have a application with the resolution 480x320. I define this in the main method of the desktop launcher. My whole application is now calculating with this resolution. For example is the player 50x50 pixels. How can i now scale my application to, for example, 1920x1080 on android so my player is now not only 50x50? I use a orthographic camera and give the width and height of 480x320 in the constructor.

\$\endgroup\$
3
  • \$\begingroup\$ Just for clarification - you want your game to render the same regardless of target resolution? \$\endgroup\$
    – driima
    Jul 14, 2015 at 11:32
  • 1
    \$\begingroup\$ I just want it to scale linear to the target resolution, so if my player is now 50x50 on a 100x100 screen, then the player should be 100x100 if the screen is 200x200. How can i achieve this? \$\endgroup\$
    – Noshii
    Jul 14, 2015 at 11:46
  • \$\begingroup\$ libGDX should do this by default, but you could take a look at Viewports. See also: gamedev.stackexchange.com/questions/24638/… gamedev.stackexchange.com/questions/101655/… \$\endgroup\$
    – driima
    Jul 14, 2015 at 11:54

2 Answers 2

1
\$\begingroup\$

Take a look as using a different type of ViewPort (this page shows the different types).

Based on what you have described I would recommend using a StretchViewport, which would upscale (or downscale) your 480x320 camera to whatever resolution you needed it to be.

Note that if you changed the aspect ratio you might get some ugly stretching artefacts.

\$\endgroup\$
0
\$\begingroup\$

On the resize event method in your ApplicationListener (the main game class) change public static width and height integers like so:

WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
float scale = (float)prefferedWidth / (float)WIDTH;
WIDTH = (int) (WIDTH  * scale);
HEIGHT = (int) (HEIGHT  * scale);

The prefferedWidth is the in-game-world width that you want to cover. This keeps the aspect ratio from being gross and stretchy on different phones. Then you just use this WIDTH and HEIGHT to set up your camera:

//gsm.game is how I obtain the instance of the main game class
cam = new OrthographicCamera(GdxGame.WIDTH,GdxGame.HEIGHT);

It keeps the window nice and pretty without skewing. If you make a portrait game do the scaling based on your prefferedHeight / HEIGHT

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .