简体   繁体   中英

Purpose of Virtual width/height in libgdx java

I watched a video by a guy I think is very good with game development, ForeignGuyMike and in his tutorials he uses a virtual width and height and scales it by 2. Here is a screen shot of the code. 范例图片

I am starting a brand new game and want to know if there is some type of purpose to this. I want to know the pros and cons of this because I can't seem to find where he explains why and where on the internet someone does this. He then multiplies the virtual width and height by the scale for the size of his window. Any help is extremely appreciated.

First go through this article by Xoppa , it should clear your basics. But just to understand by an example.

Say you have a screen of width height = 1980x1020px. Now in your game, you hardcode your character size be 100px which looks fine. But if same game is started on a screen of 1280x720px that character of 100px height will be too big.

One way is you use relative values always. For example, take height of screen every time game runs and set your character height be SCREEN_HEIGHT/10 . Now you have to follow this everywhere, set character tail height be SCREEN_HEIGHT/11.5 and so on. This could be hard to handle once your game starts to grow.

But if you create a screen camera of say 20x11 (virtual screen size) and set character height be 1.2f then no matter what the screen size be, your character size will always be relative to the ratio of height. Because you are working independently from pixels. Everything is explained in the above article by the way.

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