【问题标题】:Box2D - Can't destroy multiple fixturesBox2D - 不能破坏多个固定装置
【发布时间】:2014-04-18 23:06:13
【问题描述】:

我正在一个项目中使用 box2d 和 libgdx。我在破坏身体/身体固定装置时遇到了小问题。本质上,我想完全摧毁身体,我通过摧毁身体的固定装置来做到这一点。带有一个夹具的主体一切正常,但是当我使用两个夹具时,只有一个夹具被破坏,而另一个夹具则保持身体完好。

这里有两张图片来说明我的意思:

使用两个固定装置:

只有一个夹具:

这是我创建身体的方式:

BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.position.set(level.character.position);
        Body body = b2world.createBody(bodyDef);
        body.setUserData(level.character);
        level.character.body = body;

        CircleShape polygonShapeHead = new CircleShape();
        origin.x = level.character.circleBoundOrigin.x * 2.0f;
        origin.y = level.character.circleBoundOrigin.y * 3.0f;
        //polygonShapeHead.setAsBox(level.character.circleBoundOrigin.x,
                //level.character.circleBoundOrigin.y, origin, 0);
        polygonShapeHead.setPosition(origin);
        polygonShapeHead.setRadius(level.character.circleBoundOrigin.x);
        FixtureDef fixtureDefHead = new FixtureDef();
        fixtureDefHead.shape = polygonShapeHead;
        fixtureDefHead.friction = level.character.friction.x;
        body.createFixture(fixtureDefHead);

        polygonShapeHead.dispose();

        PolygonShape polygonShapeBod = new PolygonShape();
        origin = level.character.rectBoundOrigin;
        polygonShapeBod.setAsBox(level.character.rectBoundOrigin.x,
                level.character.rectBoundOrigin.y, origin, 0);
        FixtureDef fixtureDefBod = new FixtureDef();
        fixtureDefBod.shape = polygonShapeBod;
        fixtureDefBod.friction = level.character.friction.x;
        body.createFixture(fixtureDefBod);

        polygonShapeBod.dispose();

这是我销毁尸体的代码:

public static void removeSpecifiedBodies() {
        for (Body body : bodiesToRemoveList) {
            Array<Fixture> fixtures = body.getFixtureList();
            for (Fixture fixture : fixtures) {
                body.destroyFixture(fixture);
            }
        }
        bodiesToRemoveList.clear();
    }

在我的 b2world 被步进后,我调用了这个静态方法。我检查了日志,夹具大小为 2,它运行了两次,但只有一个夹具被破坏。为什么会这样?什么正在被摧毁?它运行了两次,但我只看到其中一个被破坏了。

编辑:我没有使用上面的删除方法,而是添加了

for(Body body : CollisionHandler.bodiesToRemoveList)
            b2world.destroyBody(body);

在 b2world.step 之后,但它冻结了一切。 :(

【问题讨论】:

    标签: java libgdx box2d


    【解决方案1】:

    GetFixtureList 只返回第一个夹具。你需要说

    var fix = body.GetFixtureList();
    while (fix) {
       body.DestroyFixture(fix);
       fix = fix.next();
    }
    

    【讨论】:

    • 嗯...我不知道。 javadoc 的解释很奇怪。谢谢,我试试!实际上,也许它在 java 中有所不同,但 getFixtureList 返回一个夹具列表,而不仅仅是一个夹具。
    • 我正在使用 JavaScript Box2D 版本,但据我所知,具有完全相同名称的函数在每个实现中都执行相同的操作
    • 我运行了一些测试,即使我的Array&lt;Fixture&gt; fixtures = body.getFixtureList(); 大小为 2,我的循环也只运行一次。做了一些研究,结果我在循环完成之前清除了要删除的主体列表。不过,这没有任何意义。只有在遍历整个实体列表后才能擦除它...
    • 我想我会提出一个新问题,因为我的问题似乎不是 Box2D 而是我的 java
    • 我不了解 Java,但 C++ 使用链表结构。例如,我使用for (b2Fixture* f = body-&gt;GetFixtureList(); f; f = f-&gt;GetNext()) body-&gt;DestroyFixture(f);
    【解决方案2】:

    我在使用带有 Box2d 的 LibGDX 时遇到了同样的问题。

    这样就解决了问题:

    int fixtureCount = body.getFixtureList().size;
    for(int i=0;i<fixtureCount;i++){
        body.destroyFixture(body.getFixtureList().get(0));
    }
    

    【讨论】:

      【解决方案3】:

      body.destroyFixturemodify( see line 130)fixtureList。试试这个:

      while (body.getFixtureList().size > 0) {
          body.destroyFixture(body.getFixtureList().first());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-17
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-23
        相关资源
        最近更新 更多