0

I am trying to follow along with the guide on here and learn LibGdx.

http://www.kilobolt.com/day-4-gameworld-and-gamerenderer-and-the-orthographic-camera.html

Here's the author's code for setting the width and height of the orthographic camera(camera used to project the 3d stuff all evenly into 2d?

    private OrthographicCamera cam;
    and later in a constructor 
    cam = new OrthographicCamera();
    cam.setToOrtho(true, 136, 204);

Is there a reason why he choose to hardcode the width and height and not retrieve the height and width of the screen the game is being run on via Gdx.graphics.getWidth/getHeight? (-from Changing the Coordinate System in LibGDX (Java))

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126

2 Answers2

1

You didn't understand how camera behaves. It doesn't matter if screen is 320x480 or 1080*1920 for camera. Camera uses own coordinate system. For example we have 1920*1080 screen. We DON'T wanna use pixels because it's bad practice. What we really want is to have own coordinate system of our world. If you have world 16*9 m then you can calculate that 1 m = 120 pixels. But your friend can have 800*450 screen and for him 1 m = 50 pixels. That's why we hardcode camera's width and height. But there is another problem here, the ratio. We considered that our ratio is 16/9 but some devices can have 4/3 ratio. Supporting a lot of ratios is very complex theme so i don't wanna mention it here.

Screenshots on different ratios of my game

If you want i can share with you my code. But note it isn't perfect and it's not complete game. And as you can see from screenshots i didn't hardcode height, only width. So i have empty space up and down.

nikoliazekter
  • 757
  • 2
  • 6
  • 23
  • Can you take a look at my other question(coordinate system), I don't get why the grey rectangle is drawing at the top of the screen – committedandroider Dec 12 '14 at 19:45
  • What i got was the camera is used to show everything/project everything into 2D. I don't get your example though. How would hardcoding camer's width and height address the issue of different dpi(pixels per inch/meter) that you brought up. That width would stay at 136, it doesn't address m/pixels of different screens. getWidth wouldn't do that also. – committedandroider Dec 12 '14 at 19:51
  • I mean you hardcode camera's width and height to have own coordinate system. I'll provide very simple example. In my TD i have 16*9 field with cells. Player can put tower in every cell. I use camera's width 16 and height 9. Then i use meter system for velocity, physics and it doesn't matter how many pixels are in that meter. Also if you are going to deal with Box2D you should deal only with International System of Units. – nikoliazekter Dec 12 '14 at 20:29
  • Ohh because if you depend on screen size, you won't know how many cells there are. So if you hardcode, you have a way of accessing each cell. Will the cells size up/down though? – committedandroider Dec 13 '14 at 00:12
  • And in terms of this example, is it important to access each cell? – committedandroider Dec 13 '14 at 00:17
  • I don't have class for cells, they are only on background. I have array of towers and each tower has own position. – nikoliazekter Dec 13 '14 at 09:43
  • Oh thanks, two different coordinate systems. you have to stretch one to get to the other. can you take a look at this one? http://stackoverflow.com/questions/27457401/why-doesnt-spritebatch-draw-anything-libgdx – committedandroider Dec 13 '14 at 09:46
0

If anyone is still struggling with this, I suggest reading into part 5, where the author explains how "we are going to assume that the width of the game is always 136. The height will be dynamically determined! We will calculate our device's screen resolution and determine how tall the game should be."

committedandroider
  • 8,711
  • 14
  • 71
  • 126