1
\$\begingroup\$

While programming a game with LibGDX, I came across a problem. My OrthographicCamera wasn't centering in the right place. I was trying to center the camera by using the line of code:

gameCam.position.set(gamePort.getScreenWidth() / 2, gamePort.getScreenHeight() / 2, 0);

But that centered the camera at (0, 0) so that you could see all four quadrants of the coordinate table.

Here is the image:

NOTE: I am using a FitViewport that was initialized like gamePort = new FitViewport(MarioBros.V_WIDTH, MarioBros.V_HEIGHT, gameCam);

Also: public static final int V_WIDTH = 400; public static final int V_HEIGHT = 208; (in the MarioBros class)

But after some fiddling with the code, I changed the line of code to this:

gameCam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0); I changed it from ScreenSize to WorldSize and got this result:

I'm still unsure of what using WorldSize changed.

What's the difference between the two?. Also, why did one work while the other didn't?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The difference between ScreenSize and WorldSize is part of the brilliance of graphics systems like OpenGL.

ScreenSize is the actual size of the window in pixels. When the user grabs the window handles and resizes the window, then ScreenSize will change.

WorldSize is the size of your game level or "World". It is completely arbitrary. In a 2D game, 1 unit in your game world will generally mean 1 pixel on your game map, but depending on how you setup your world, you could use whatever units you wanted to.

Libgdx very helpfully makes it so you really don't have to worry about the ScreenSize when writing your game. After all, depending on the computer they are using, your users will have vastly different screen sizes. When you create a FitViewport, you are saying "No matter what the user's ScreenSize is, I want the screen camera to be V_HEIGHT world units tall and V_WIDTH world units wide. If you are using an OrthographicCamera, this means that your camera will always show V_HEIGHT x V_WIDTH world units of your world. FitViewport then automagically fits your camera into the user's window.

FitViewport's getWorldWidth() and getWorldHeight() are perhaps named a little inappropriately. They are getting the width and height of the viewport in world units. Not the entire width and height of your world. Essentially, these functions are just returning V_WIDTH and V_HEIGHT that you gave it in the constructor.

Note that the World can be much bigger than the V_HEIGHT and V_WIDTH dimensions because not all of the World will be visible at once. So if you make your level bigger and you want to start the camera off at the bottom left corner of the level, then you will want to position it at V_HEIGHT/2 and V_WIDTH/2 (assuming of course that your map is drawn at the origin).

One last thing. Even though you can mostly forget about the screen size, if the user resizes the window, then you have to notify your viewport. You do this by overriding the resize(int width, int height) method of your ApplicationListener class and calling

gamePort.update(width,height);
\$\endgroup\$
2
  • \$\begingroup\$ Oh wow, I had no idea that the worldWidth/Height that you pass into the FitViewport is also the size of what the gameCam can see! I thought it was completely seperate (or am I still not seeing something). And if this is true, does this make OrthographicCamera.setToOrtho obsolete? \$\endgroup\$
    – Eames
    Apr 29, 2016 at 20:27
  • \$\begingroup\$ Sounds like you've got it right. And yes, you can probably get rid of setToOrtho because Viewport sets the viewport height and width on the camera object. \$\endgroup\$ Apr 29, 2016 at 20:37

You must log in to answer this question.

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