【发布时间】: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 之后,但它冻结了一切。 :(
【问题讨论】: