简体   繁体   English

如何在不同屏幕尺寸上实现相同的物理效果?

[英]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. 一旦屏幕尺寸不再是1920x1080,一切都会变成超级奇怪的尺寸。

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. 我将每次使用的硬编码速度除以1080 / screenWidth1920 / screenHeight ,以便它们在每个屏幕尺寸上都相同。

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? 现在问题是:现在所有内容都按屏幕宽度缩放,我如何支持其他比率超过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. 我试图找出哪一侧更靠近16:9的框架,然后使用那个框架来正确地扩展所有东西,但这不能像我发现的那样工作。 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. 我现在使用给定的16 : >9显示的height并计算它在具有16 : 9显示的屏幕上的width ,然后我计算所有内容,就好像这是实际宽度。 Everything is drawn with an offset of (actualWidth-wantedWidth)/2f so that the objects appear centered. 所有东西都以(actualWidth-wantedWidth)/2f的偏移量绘制,以便对象显示为居中。 The same is possible for the height, if the ratio is 16 : <9 如果比率为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. 简而言之:如果屏幕比例不合适,我可以将(黑色或纹理)条形放在我想要的空间周围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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