2
\$\begingroup\$

I'm trying to create my own jumper type game where the player moves up through an infinite y value. The problem I am having is getting the screen size to match up with the android screen.

  viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

Currently I just want the sprite to be drawn in the center of the screen. I'm not worried about the game world size as it will differ from each device.

  camera.update();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(player.getKeyFrame(animationTime), Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);

This draws the player somewhere in the upper right corner of the screen. It's my understanding that Gdx.graphics.getwhatever is used to get the dimensions of whatever your device screen is. So I'm wondering if I'm using the viewport class correctly or not?

\$\endgroup\$
  • \$\begingroup\$ Maybe the sprite is near as big as a quarter of the screen? For example, the blue square in this image. I mean you did not substracted half the width and eight of the sprite size when defining the sprite origin when drawing it... \$\endgroup\$ – Blas Soriano May 30 '15 at 2:17
  • \$\begingroup\$ try to remove camera.settoortho \$\endgroup\$ – Vahe Muradyan Jun 1 '15 at 6:43
2
\$\begingroup\$

The call to camera.setToOrtho sets the cameras position to the center. So it's P(0,0) is the middle of the Screen. So you are seeing everything from P(-Gdx.graphics.getWidth() / 2, -Gdx.graphics.getHeight() / 2), which is the lower, left corner, to P(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2), which is the upper, right corner, where your player is drawn at.
There are now two posibilities:

  • Draw your player at P(0,0)
  • Set the cameras position to P(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2)

Also, don't forget to call viewport.update(width, height) in the resize(width, height) method.
And you might consider using a different Viewport size. It could make things easier, as you don't need to think in pixels, but in meters or another preferred unit. The Viewport size will then be "translated" to pixels by Libgdx, you don't need to worry about that.

EDIT: As @MrPlow wrote in this comment, the sprites origin by default is it's lower left corner. Also it's position is given by the lower left corner, not it's center.
While the origin can be changed, so that the sprite will be scaled and rotated arround the given origin, the position will always be the lower left corner.

\$\endgroup\$
  • 1
    \$\begingroup\$ Also, a small thing to add onto this answer, you should take into consideration that sprite origin is by default in the bottom left corner so to perfectly render the sprite it needs to be offset by half-width and half-height \$\endgroup\$ – MrPlow Dec 29 '15 at 10:58
  • \$\begingroup\$ @MrPlow note, that the origin will only affect the rotation and scaling, not the position. So the position will always be the lower, left corner, independent of the origin. \$\endgroup\$ – Springrbua Dec 29 '15 at 12:56
  • \$\begingroup\$ Yeah, I meant that the sprite position needs to be offset by half-width and half-height so that it's "properly" centered though i haven't expressed myself clearly. Thanks for clearing that up. \$\endgroup\$ – MrPlow Dec 29 '15 at 13:19
  • \$\begingroup\$ @MrPlow then, ofc, you are right. Thanks for mentioning it, it is definitly an important detail! \$\endgroup\$ – Springrbua Dec 29 '15 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.