简体   繁体   English

具有AndEngine异常创建夹具的Box2d

[英]Box2d with AndEngine exception creating fixture

I'm using AndEngine with the Box2d extension, and when my game loads its map, sometimes it crashes (yes, only sometimes. It looks so random to me) and this is what the trace looks like: 我将AndEngine与Box2d扩展一起使用,当我的游戏加载其地图时,有时会崩溃(是的,只是有时。它对我来说是如此随机),这就是跟踪的样子:

#00 pc 00014480 /data/app-lib/com.sergio.game-2/libandenginephysicsbox2dextension.so (b2Body::CreateFixture(b2FixtureDef const*)+8)
#01 pc 0000c408 /data/app-lib/com.sergio.game-2/libandenginephysicsbox2dextension.so (Java_com_badlogic_gdx_physics_box2d_Body_jniCreateFixture__JJFFFZSSS+112)
#02 pc 0001dbcc /system/lib/libdvm.so (dvmPlatformInvoke+112)
#03 pc 0004e123 /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+398)
#04 pc 00000214 /dev/ashmem/dalvik-jit-code-cache (deleted)

It won't tell me where exactly is crashing, and I don't know where to look at. 它不会告诉我确切的崩溃位置,我也不知道要看哪里。 I create bodies and fixtures as usual: 我照常创建实体和固定装置:

FixtureDef wallfixture = PhysicsFactory.createFixtureDef(0, 0, 0.2f);
wallfixture.filter.categoryBits = CATEGORY_WALL;
wallfixture.filter.maskBits = MASK_WALL;
final Body theBody = PhysicsFactory.createBoxBody(mPhysicsWorld, greenRectangle, BodyType.StaticBody, wallfixture);

Any idea? 任何想法?

From my experience, there are several frequent problems with fixtures in box2d: 根据我的经验,box2d中的灯具经常出现以下问题:

  1. Invalid winding order; 无效的缠绕顺序;
  2. Not convex polygon; 不凸多边形;
  3. Too small size of shape. 形状尺寸太小。 When distance between several points is too short, box2d assumes they are one point. 当几个点之间的距离太短时,box2d假定它们是一个点。 That's why box2d can interpret a triangle as a line, for example, which is not acceptable (polygon must have at least 3 points). 这就是为什么box2d可以将三角形解释为直线的原因(例如,多边形必须至少具有3个点)是不可接受的。

Check out if all your fixtures always math these rules. 检查您所有的灯具是否总是符合这些规则。 Looks like you are generating fixtures by random, and maybe sometimes some of fixtures become invalid in described meaning. 看起来您是在随机生成固定装置,有时某些固定装置在描述的意义上会失效。

After a lot of work, I've found what the problem was. 经过大量工作,我发现了问题所在。 I hope this can help people with this problem. 我希望这可以帮助这个问题的人。 I was creating bodies outside of the update thread, which seems not ok. 我在更新线程之外创建主体,这似乎不行。 So I fixed it by putting all the bodies creation on the Update thread, like this: 因此,我通过将所有主体创建都放在Update线程上来修复它,如下所示:

mEngine.runOnUpdateThread(new Runnable() {
     @Override
     public void run() {
          ...
          FixtureDef wallfixture = PhysicsFactory.createFixtureDef(0, 0, 0.2f);
          wallfixture.filter.categoryBits = CATEGORY_WALL;
          wallfixture.filter.maskBits = MASK_WALL;
          final Body theBody = PhysicsFactory.createBoxBody(mPhysicsWorld, greenRectangle, BodyType.StaticBody, wallfixture);
          ...
     }
}

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

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