问题描述:

It seems like a viewport is useless, but I AM PROBABLY MISSING SOMETHING.

For example I wanted to make a game where I have an object and if I hit the left or the right end of the screen I DIE! So if I used Gdx.graphis.getWidth() and 0 everything would work fine.

OK, but let's setup the viewport = new ExtendViewport(500, 800, camera);

Cool I have not virtual pixels of 500 and 800 === > 5 : 8

Then I type the logic for hitting the end screen, so if object.x < 0 or object.x > 800 ----> object DIES!

But now I start the game on a tablet which has the resolution of 1500 x 2000 ===> 3 : 4, now everything is wrong, the whole screen is moved to the left, so 0 still works but 800 is not the end of the right screen edge.

Then I go use FitViewport and the collision of the left and right edge is now moved, because of a different aspect ratio.

Seems like I can use strechViewport, or just manually code the stuff... But I know I am missing something, because IT MUST HAVE ITS USE.

Would appreciate any help!

网友答案:

First of all in my opinion it is good habit to center the camera at (0, 0) coordinates. Then you will have always -screenWidth/2 as left screen's edge's position and screenWidth/2 for the right side.

Secondly you should not use screen's coordinates directly when using viewport. Viewport has a method to transform screen's to viewport's coordinates:

    unproject(Vector2 screenCoords)

it has also backward method

    project(Vector2 worldCoords)

These will make you able to keep always same type of coordinates no matter how big display will be.

相关阅读:
Top