2
\$\begingroup\$

Recently I have read here: https://xoppa.github.io/blog/pixels/ that I should not use pixels in my game. But Im not sure that I have understood this so I have some questions.

  1. Xoppa writes that pixels may look different on different devices but I thought all the time that viewport solves the problem of different screen size and aspect ratio. Was I wrong?

  2. How can I draw all assets without using pixels?

  3. I have an idea to support all screen ratios and sizes by using ExtendViewport with the biggest screen that I will support. It looks like this:

viewport = new ExtendViewport(1080, 1920, 1440, 1920, camera);

And everything works well on the smartphone during test. Game is prepared for 16:9 screen and if somebody have for example 4:3 screen he will se more background texture.

But I dont know how game will work on weaker devices. Is one viewport (1920x1440) enough? Or I should make more viewports for several sceen sizes for example 1920x1440, 1280x960 and 800x600?

Just I dont understand why I should not use pixels in game if everything works well. Can somebody explain it to me in simple way?

\$\endgroup\$
1
\$\begingroup\$

Reason's why I use meters instead of pixels

  • using pixel is mostly not recommended because base on my experience, when you are using libgdx box2d body the unit is known as meters, for example you created a Vector2 position = Vector2(1,1); the x and y value is 1 meter, so in that case if you use pixels for example 32 pixel the body value would be 32 meters.
  • to minimize the value

To convert from pixels to meters is easy.

float UNIT = 32.0f; // unit of 1 meter is 32 pixels in the given unit
float PIXELS_TO_METERS = 1 / UNIT;

float meterValue = PIXELS_TO_METERS * PIXEL_VALUE

For example, you have a camera and set its width to 800 pixels and height to 480 pixels. Then you just need to multiply it by PIXELS_TO_METERS to get the meter value.

public final static float WIDTH = 800;
public final static float HEIGHT = 480;

// This is the typical pixels value of WIDTH and HEIGHT
OrthographicCamera camera = new OrthographicCamera(WIDTH, HEIGHT);

// Now we applied the pixel to meter conversion.
float FRUSTUM_WIDTH = WIDTH * PIXELS_TO_METERS;
float FRUSTUM_HEIGHT = HEIGHT * PIXELS_TO_METERS;

OrthographicCamera camera = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);

// or set it directly
OrthographicCamera camera = new OrthographicCamera(25, 15);
\$\endgroup\$
  • \$\begingroup\$ what if i dont use box2d in my game? i should use for example meters? Xoppa dont use box2d in his blog and he dont use pixels. Im trying to make my game logic withous using pixels but i dont know how to make camera, viewport and draw textures, objects etc. without using pixels. \$\endgroup\$ – Land93 Aug 3 '16 at 22:42
  • \$\begingroup\$ @Land93 actually, it is the same. You just only minimize the value you entered, let say you create a camera and set 800 pixel as width and 480 pixel as height, you only have to multiply it to PIXELS_TO_METERS nothing more. \$\endgroup\$ – ronscript Aug 4 '16 at 6:26
0
\$\begingroup\$

It is more convenient. Say you are developing a 2D 4X game, a simplified version of the original civilization games. Your ingame world is divided into a 20x20 map of tiles. Tell me, to place your tiles, would you rather draw a tile at tiles[2][2].draw(20,20); or at tiles[2,2].draw(2,2); to draw a tile at (x,y) -> (2,2) from the origin of your world. This is a negligible difference in this case, but as the coordinates increase in value, you could eventually have to write tiles[2][2].draw(2000,2000);depending on your world size.

This might be a bit arbitrary, so let me give you another example. When designing a UI, would you rather have to guess where each UI element should be drawn pixel-wise, or would you rather divide your viewport into 100 units (a 10x10 map of coordinates) and draw it at 10,10 or 1000,1000. And, no, the viewport will not handle pixel -> device screen adjustments, it will handle world coordinates -> device screen positioning, so using pixels will still mess up your rendering.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.