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

查看:282
本文介绍了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.

此段也出现在手册中.

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

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