You can set up a Camera
like this:
new OrthographicCamera(viewPortWidth, viewPortHeight)
for 2D gamenew PerspectiveCamera(viewAngel, viewPortWidth, viewPortHeight)
for 3D.
I stay with 2D for now. The viewport you set will automatically be scaled up to fit the window/screen. So if you use a viewport of 80 width and 45 height on a 1600 * 900px window, every unit gets scaled up to use 20px. On a 800*450px screen one unit would only use up 10px.
The origin of your camera (P(0,0)) is in the middle of the screen by default. So if you render a Texture
at x = 0, y = 0, with a wide of 1 and a height of one, its left lower corner is in the middle of the screen, and it is 20px*20px big (on the 1600*900 screen).
To have the cameras origin at the lower left corner of the screen (as we normally want it), you need to move it by calling camera.translate(viewPortWidth/2, viewPortHeight/2)
or by seting its position: camera.position.set(viewPortWidth/2, viewPortHeight/2)
. After that you have to call camera.update()
to let it know that you moved it. To that after every change on the camera (rotate, translate...).
To use this camera to render your Texture
s you have to set the SpriteBatch
s projectionMatrix
, which is like the view of the world or something like this. To do this you call: spriteBatch.setProjectionMatrix(camera.combined)
. Then if you draw with this spriteBatch
everything should work.
Something to read: Projection, viewport, camera The new Viewports