How to achieve same physics on different screen sizes?

Finni :

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?

Finni :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109391&siteId=1