Box2D – 无法破坏多个灯具
我正在使用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 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只返回第一个fixture。 你需要说
var fix = body.GetFixtureList(); while (fix) { body.DestroyFixture(fix); fix = fix.next(); }
使用LibGDX和Box2d时遇到了同样的问题。
这种方式解决了这个问题:
int fixtureCount = body.getFixtureList().size; for(int i=0;i
body.destroyFixture
将修改(见第130行) fixtureList
。 尝试这个:
while (body.getFixtureList().size > 0) { body.destroyFixture(body.getFixtureList().first()); }