java - Box2D - 不能破坏多个固定装置

标签 java libgdx box2d

我在我正在进行的项目中使用 box2d 和 libgdx。我在破坏 body / body 固定装置时遇到了一个小问题。本质上,我想完全摧毁 body ,我通过摧毁 body 的固定装置来做到这一点。对于带有一个固定装置的主体,一切都工作得很好,但是当我使用两个固定装置时,只有一个固定装置被破坏,而另一个固定装置则使主体完好无损。

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

对于两个固定装置: With both fixtures

只有一个夹具: With only one fixture

下面是我创建主体的方式:

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 后调用此静态方法。我检查了日志记录,fixtures 大小为 2,它运行了两次,但只有一个 fixture 被销毁了。为什么会这样?什么正在被摧毁?它运行了两次,但我只看到其中一个被破坏了。

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

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

在 b2world.step 之后,但它卡住了一切。 :(

最佳答案

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

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

关于java - Box2D - 不能破坏多个固定装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163728/

相关文章:

c++ - 在 CLion 中包含 Box2D

android andengine remove physics body crash 应用程序

java - libgdx - 创建选择框时出现异常

android - 事件未传递给监听器

java - 如何接收 Telegram Bot 方法 (API) 的返回参数

java - 如何在 Java 应用程序中使用最小库来检测音频源的节拍?

java - BodyEditorLoader.jar 给出 NoSuchMethodError

java - Android Studio LibGDX 游戏不显示障碍物和地板

Java - Maven JAXB-2插件具有不同配置的多个方案不会生成类

java - Retrofit 不会调用 onReponse 和 onFailure