我在我正在处理的项目中将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之后,但它冻结了所有内容。 :(

最佳答案

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

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

10-01 22:49