Arda Kara
Arda Kara

Reputation: 521

Fitviewport Wrong Camera Positioning

I have developed a game on my phone which has 1080x1920(portrait) resolution. And I set my fitviewport according to it. It looks like this on my phone:

my phone

But on any other phone or on desktop camera has wrong positioning. It looks like this on other devices:

other phone

I have used fitviewport in other projects before but never encountered that problem. May it be because of portrait mode?

EDIT Related code is here:

//in create
camera = new Orthographiccamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeigth());

viewport = new FitViewport(1080,1920,camera);

//in resize
viewport.update(width,height);

//before starting batch
batch.setProjectionMatrix(camera.combined);

Upvotes: 3

Views: 205

Answers (1)

noone
noone

Reputation: 19776

You have two contradicting statements in your code: camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeigth()). This will set the camera's viewport size to the display size and then center the camera.

On the other hand, you want to use a FitViewport with a fixed size of 1080x1920.

If your screen size is exactly 1080x1920, there's no problem, because the center will be the same. If the screen size differs from the one you use in FitViewport, then setToOrtho() will set the camera's center to a position that's not the center of 1080x1920 and that's why you notice the offset.

Using viewport.update(width, height, true) will correct this. The last parameter will center the camera correctly and override what happened in setToOrtho().

Let the viewport manage your camera and remove the call of camera.setToOrth().

Upvotes: 1

Related Questions