Java Box2D-Can';不要破坏多个固定装置
我正在一个项目中使用box2d和libgdx。我在销毁尸体/尸体的固定装置方面有点小问题。本质上,我想完全摧毁身体,我通过摧毁身体的固定装置来做到这一点。一个身体只有一个固定装置,一切都很好,但是当我使用两个固定装置时,只有一个固定装置被破坏,剩下身体和另一个固定装置完好无损 这里有两张图片来说明我的意思: 对于两个固定装置: 只有一个夹具: 以下是我如何创建主体:Java Box2D-Can';不要破坏多个固定装置,java,libgdx,box2d,Java,Libgdx,Box2d,我正在一个项目中使用box2d和libgdx。我在销毁尸体/尸体的固定装置方面有点小问题。本质上,我想完全摧毁身体,我通过摧毁身体的固定装置来做到这一点。一个身体只有一个固定装置,一切都很好,但是当我使用两个固定装置时,只有一个固定装置被破坏,剩下身体和另一个固定装置完好无损 这里有两张图片来说明我的意思: 对于两个固定装置: 只有一个夹具: 以下是我如何创建主体: BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyTyp
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.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<fixtureCount;i++){
body.destroyFixture(body.getFixtureList().get(0));
}
int fixtureCount=body.getFixtureList().size;
对于(int i=0;ibody.destroyFixture
将fixtureList
。请尝试以下操作:
while (body.getFixtureList().size > 0) {
body.destroyFixture(body.getFixtureList().first());
}
嗯…我不知道。javadoc的解释很奇怪。谢谢,我会试试!事实上,在java中可能不同,但是getFixtureList返回一个装置列表,而不仅仅是一个装置。我使用的是JavaScript Box2D版本,但据我所知,具有完全相同名称的函数在我运行的每个实现中都做相同的事情即使myArray fixtures=body.getFixtureList()
size为2,我的循环只运行一次。我做了一些研究,结果发现我在循环完成之前清除了要删除的实体列表。但这没有任何意义。它应该在遍历整个实体列表后才被删除……我想我会提出一个新问题,因为我的问题似乎不是关于Box2D,而是关于我的javaI不知道java,但是C++使用链表结构。例如,我使用<代码>为(B2Cux*f= Booer-> GeFixTeCudio();f;f= f-> GETNEXT())Syt- >销毁夹具(F);< /C>
while (body.getFixtureList().size > 0) {
body.destroyFixture(body.getFixtureList().first());
}