java - LibGDX - Centering Orthographic Camera -
so i'm working on game have camera in game centered on middle of screen device lengths. hope this picture can better explain i'm trying achieve. have tried setting position of camera hasn't worked me.
scrnheight = gdx.graphics.getheight(); if (scrnheight <= height) { cam.settoortho(false, 480, 800); } else { cam.settoortho(false, 480, scrnheight); } //this part seems giving me issues cam.position.set(cam.viewportwidth/2,cam.viewportheight/2, 0); cam.update(); gdx.input.setinputprocessor(this); gdx.gl.glclear(gl20.gl_color_buffer_bit); gsm.update(gdx.graphics.getdeltatime()); gsm.render(batch); batch.begin(); batch.draw(border, -(border.getwidth() - width) / 2, -(border.getheight() / 4)); batch.end(); i don't know if i'm giving wrong coordinates when i'm setting position or happening causes lack of vertical centering. appreciated.
the orthographic camera position in libgdx means position in-game, not on device screen, therefore changing won't move game screen on device.
therefore, use camera position move , position camera in-game.
example, in response player input movement:
if (gdx.input.iskeypressed(input.keys.left)) { cam.translate(-3, 0, 0); // moves camera left. } if (gdx.input.iskeypressed(input.keys.right)) { cam.translate(3, 0, 0); // moves camera right. } as can see, moving camera in-game, left , right according player's input.
however, code has few more issues not setting batch projection matrix:
batch.setprojectionmatrix(cam.combined); and resetting camera position center of viewport each frame:
// don't run each frame, resets camera position! cam.position.set(cam.viewportwidth/2,cam.viewportheight/2, 0); cam.update(); // <- however, must run line each frame.
finally, centering libgdx app on device screen should done outside of libgdx, otherwise, if intend use spare screen same libgdx app, should create camera work full screen , render before actual game camera, used hud , such...
Comments
Post a Comment