简体   繁体   中英

Why does the sample author hardcode width and height for orthographic camera?(LibGdx Zombie Bird tutorial)

I am trying to follow along with the guide on here and learn LibGdx.

http://www.kilobolt.com/day-4-gameworld-and-gamerenderer-and-the-orthographic-camera.html

Here's the author's code for setting the width and height of the orthographic camera(camera used to project the 3d stuff all evenly into 2d?

    private OrthographicCamera cam;
    and later in a constructor 
    cam = new OrthographicCamera();
    cam.setToOrtho(true, 136, 204);

Is there a reason why he choose to hardcode the width and height and not retrieve the height and width of the screen the game is being run on via Gdx.graphics.getWidth/getHeight? (-from Changing the Coordinate System in LibGDX (Java) )

You didn't understand how camera behaves. It doesn't matter if screen is 320x480 or 1080*1920 for camera. Camera uses own coordinate system. For example we have 1920*1080 screen. We DON'T wanna use pixels because it's bad practice. What we really want is to have own coordinate system of our world. If you have world 16*9 m then you can calculate that 1 m = 120 pixels. But your friend can have 800*450 screen and for him 1 m = 50 pixels. That's why we hardcode camera's width and height. But there is another problem here, the ratio. We considered that our ratio is 16/9 but some devices can have 4/3 ratio. Supporting a lot of ratios is very complex theme so i don't wanna mention it here.

Screenshots on different ratios of my game

If you want i can share with you my code. But note it isn't perfect and it's not complete game. And as you can see from screenshots i didn't hardcode height, only width. So i have empty space up and down.

如果有人还在为此苦苦挣扎,建议阅读第5部分,作者在其中解释“我们将如何假设游戏的宽度始终为136。高度将动态确定!我们将计算设备的屏幕分辨率并确定游戏的高度。”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM