简体   繁体   English

AndEngine Box2D扩展-缩放

[英]AndEngine Box2D Extension - Scaling

I'm new to AndEngine and Box2D. 我是AndEngine和Box2D的新手。 So bear with me please. 所以请和我一起。

I created a new project, set up a 480x800 camera, added a 32x32 stripe, created a physics world at Earth gravity and dropped the stripe. 我创建了一个新项目,安装了480x800摄像头,添加了32x32条纹,在地球引力下创建了一个物理世界,并删除了该条纹。 Lo and behold, it DID drop. 瞧,它DID掉落了。 But it didn't seem "natural" to me; 但这对我来说似乎不是“自然的”。 it was too slow. 太慢了。

Then I realized that the gravity is in meters (m/s2) whereas the environment is in pixels. 然后我意识到重力以米(m / s2)为单位,而环境以像素为单位。 Where does the conversion between meters and pixels tak place? 米和像素之间的转换在哪里? Somewhere there should be an assumption behind the scenes. 幕后某个地方应该有一个假设。 Do I have any control over it? 我对此有任何控制权吗?

How does Box2D know whether it's dropping the stripe from 100 meters above the ground (and viewing it from a distance which would appear a very slow drop) or 1 meter above the ground (and viewing it from up close which would appear very fast)? Box2D如何知道是从高出地面100米的地方掉下条纹(并从一个看起来很慢的距离观察距离)还是从高出地面1米的地方(从近距离看它会很快看到条纹)?

To test that the conversion is the real problem, I multiplied the gravity by 10 and it improved the "naturalness". 为了测试转换是否是真正的问题,我将重力乘以10并改善了“自然度”。 But I think there should be a more sophisticated way to convert pixels to meters. 但是我认为应该有一种更复杂的方法将像素转换为米。

Thanks in advance. 提前致谢。 I really appreciate your comments. 非常感谢您的评论。

It's as @iforce2d said it the comment. 就像@ iforce2d所说的那样。 In AndEngine the default value is 32, therefore 32 pixels is considered one meter. 在AndEngine中,默认值为32,因此32像素被视为一米。 When converting pixels to meter, divide the pixels by this value. 将像素转换为米时,将像素除以该值。 When converting from meters to pixels, multiply with this value. 从米转换为像素时,请乘以该值。 You can find this value in org.andengine.extension.physics.box2d.util.constants.PhysicsConstants class. 您可以在org.andengine.extension.physics.box2d.util.constants.PhysicsConstants类中找到此值。

The ratio is then used in PhysicsFactory.create... methods if you don't specify your own. 如果未指定自己的比例,则在PhysicsFactory.create...方法中使用该比例。 These methods create the physics body for you, measuring your sprite size in pixels and passing meters to Box2D. 这些方法可以为您创建物理体,以像素为单位来测量精灵大小,然后将仪表传递给Box2D。 It's also used in the PhysicsConnector class constructor. 它还在PhysicsConnector类构造函数中使用。 Use your own value if 32 doesn't suit you, but then you will have to be consistent and use it every time. 如果32不适合您,请使用您自己的值,但是您必须保持一致并每次使用它。

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

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