Pregunta

I have made an android/ios game and it was working fine on Android only one I have published but say I make a texture render 50px by 50px the pixel density on other devices make the texture render differently is there a way I can make fixed world coordinates so the images render the same but instead of the texture rendering at random sizes it just scales down/up

¿Fue útil?

Solución

You can set up a Camera like this:

  1. new OrthographicCamera(viewPortWidth, viewPortHeight) for 2D game
  2. new 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 Textures you have to set the SpriteBatchs 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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top