简体   繁体   中英

How to achieve same physics on different screen sizes?

Is there any elegant way of calculating velocities, sizes and distances so that they end up the same on every screen size?

I tried a lot of things, including scaling factors etc. but it didn't work. Everything ended up having super weird sizes as soon as the screen size wasn't 1920x1080 anymore.

EDIT: I fixed it. I now create the objects using fractions of the screen width for the sizes. I divide every hard-coded velocity I use by 1080 / screenWidth or 1920 / screenHeight , respectively, so that they are the same on every screen size.

So I still use scaling factors, but I use them way less frequently, and I think I sometimes used them in wrong places.

Now the question is: Now that everything is scaled with the screen width, how can I support devices with other ratios than 16:9? I tried to find out which of the sides is closer to the 16:9 frame and then use that one to scale everything properly, but that cannot work as I found out. How do games handle different screen ratios?

I now use the height of the given 16 : >9 display and calculate the width it would have on a screen with a 16 : 9 display, then I calculate everything as if that was the actual width. Everything is drawn with an offset of (actualWidth-wantedWidth)/2f so that the objects appear centered. The same is possible for the height, if the ratio is 16 : <9

So in a nutshell: if the screen ratio doesn't fit, I make it fit by putting (black or textured) bars around my desired space.

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