3

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?

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Finni
  • 429
  • 5
  • 17
  • Why not just use the units Android already has for this sort of thing, `dp`? https://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dip-dp-and-sp – TheWanderer Jan 30 '19 at 21:13
  • Also realize that screens don't have the same height/width ratio. Any hardcoded value like 1080 won't work. – Morrison Chang Jan 30 '19 at 21:15
  • 3
    Determine a base dimension where the physics work in the manner that you prefer. Then, for screens of different dimensions, obtain the scaling factors in the x and y dimensions relative to the base dimension. Finally, use these x and y scaling factors on the quantity of interest, e.g. velocity. – jrd1 Jan 30 '19 at 21:35
  • @TheWanderer i read that question before I started and tried to kind of implement my own dp... – Finni Jan 30 '19 at 21:39
  • @jrd1 that's what I did and I am unhappy with the solution because firstly I made a mistake somewhere and secondly I don't think this is a very pretty solution... Isn't there some elegant trick for that? If there isn't I guess I'll have to do the same thing again and hope I don't mess it up again, which would be annoying – Finni Jan 30 '19 at 21:41
  • @Finni: According to what you've written, you've _inverted_ the x scaling factor. IMO, there isn't a cleaner solution than involving scaling factors - anything else wouldn't be as adaptable, intuitive, or maintainable. – jrd1 Jan 30 '19 at 21:47

1 Answers1

0

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.

Finni
  • 429
  • 5
  • 17