Box2D 中一米是多少像素? [英] how many pixels is a meter in Box2D?

查看:43
本文介绍了Box2D 中一米是多少像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单,没有代码!如果有人知道 Box2D 和 SDL2,那么请告诉我如何用 b2body 包装 SDL_Rect.当然,它需要知道米与像素的转换,反之亦然.这是因为 Box2D 以米为单位测量距离.你能给我一个简单的表达式或函数来将米(Box2D 的)转换为像素或像素转换为米(Box2D 的)吗?

Question is simple so, no codes! If someone knows Box2D and SDL2, then, please tell me how to wrap SDL_Rect with b2body. Ofcourse, it requires to know the conversion of metre to pixel and vice versa. This is because Box2D measures distance in metres. Can you give me a simple expression or function to convert metres(of Box2D) to pixels or pixels to metres(of Box2D)?

推荐答案

你能给我一个简单的表达式或函数来将米(Box2D的)转换为像素或像素到米(Box2D的)吗?

Can you give me a simple expression or function to convert metres(of Box2D) to pixels or pixels to metres(of Box2D)?

不幸的是,这对我们来说并不像听起来那么简单.因为,如果你的游戏是蠕虫,那么你的游戏世界将以毫米为单位,而如果它像 太空入侵者 那么它会以公里为单位吗?所以由游戏设计师来决定这个比例因子.在不知道游戏的复杂性的情况下,推断出这个因素充其量只是一个疯狂的猜测.

Unfortunately, this isn't as simple as it sounds, for us. Because, if your game is on worms, then your game world would be in millimetres while if it's like Space Invaders then it would be in kilometres? So it's up to the game designer to decide this scaling factor. Without knowing about the intricacies of the game, deducing this factor would just be a wild guess at best.

来自 Box2D 的常见问题:

如何将像素转换为米?

假设您有一个 100x100 像素的角色的精灵.您决定使用 0.01 的比例因子.这将使角色物理盒为 1m x 1m.所以去制作一个 1x1 的物理盒子.现在假设字符从像素坐标 (345,679) 开始.因此,将物理框定位在 (3.45,6.79) 处.现在模拟物理世界.假设角色物理框移动到 (2.31,4.98),那么将您的角色精灵移动到像素坐标 (231,498).现在唯一棘手的部分是选择一个比例因子.这真的取决于你的游戏.您应该尝试将移动物体放在 0.1 到 10 米的范围内,其中 1 米是最佳位置.

Suppose you have a sprite for a character that is 100x100 pixels. You decide to use a scaling factor that is 0.01. This will make the character physics box 1m x 1m. So go make a physics box that is 1x1. Now suppose the character starts out at pixel coordinate (345,679). So position the physics box at (3.45,6.79). Now simulate the physics world. Suppose the character physics box moves to (2.31,4.98), so move your character sprite to pixel coordinates (231,498). Now the only tricky part is choosing a scaling factor. This really depends on your game. You should try to get your moving objects in the range 0.1 - 10 meters, with 1 meter being the sweet spot.

这里的缩放系数是您设置的;您决定多少像素将构成您的游戏世界的一个单位.

Here the scaling factor is what you set; your decision on how many pixels would constitute one unit of your game world.

通常建议您以游戏世界为单位进行所有计算,最后作为最后一步(渲染)进行像素转换;同样,在将数据从外部(比如图像/精灵,以像素为单位)引入世界空间之前,只进行一次反向转换,然后在其余代码中处理世界单位.请参阅 Box2D 的作者 Erin Catto 关于此问题的这篇文章.摘录:

It is usually recommended that you do all the calculations in the game world units and finally, as a last step (render), do the conversion to pixels; likewise before bringing in data from outside (say an image/sprite, which is in pixels) to the world space, do the reverse conversion only once and then deal in world units in the rest of the code. See this article by Erin Catto, Box2D's author, on this issue. An excerpt:

您应该考虑在游戏代码中使用 MKS 单位,并在渲染时仅转换为像素.这将简化您的游戏逻辑并减少出错的机会,因为可以将渲染转换隔离到少量代码中.

You should consider using MKS units in your game code and just convert to pixels when you render. This will simplify your game logic and reduce the chance for errors since the rendering conversion can be isolated to a small amount of code.

这一段也出现在手册中.

This paragraph appears in the manual too.

这篇关于Box2D 中一米是多少像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆