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);