我正在使用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之后,但它冻结了一切。 :(
答案 0 :(得分:1)
GetFixtureList仅返回第一个fixture。你需要说
var fix = body.GetFixtureList();
while (fix) {
body.DestroyFixture(fix);
fix = fix.next();
}
答案 1 :(得分:0)
我在使用LibGDX和Box2d时遇到了同样的问题。
这种方式解决了这个问题:int fixtureCount = body.getFixtureList().size;
for(int i=0;i<fixtureCount;i++){
body.destroyFixture(body.getFixtureList().get(0));
}
答案 2 :(得分:0)
body.destroyFixture
将modify( see line 130) fixtureList
。试试这个:
while (body.getFixtureList().size > 0) {
body.destroyFixture(body.getFixtureList().first());
}