Box2D-无法销毁多个灯具 [英] Box2D - Can't destroy multiple fixtures

查看:59
本文介绍了Box2D-无法销毁多个灯具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正在处理的项目上将box2d和libgdx一起使用.我在销毁尸体/尸体固定装置方面遇到了小问题.本质上,我想完全破坏身体,这是通过破坏所述身体的固定装置来实现的.带有一个固定装置的物体,一切工作都很好,但是当我使用两个固定装置时,只有一个固定装置被破坏,而另一个固定装置则完好无损.

I'm using box2d along with libgdx on a project I'm working on. I'm having a slight problem destroying a body/the fixtures of a body. Essentially, I want to completely destroy the body, which I do by destroying the fixtures of said body. Everything works perfectly fine with a body with one fixture, but when I use two fixtures, only one fixture get's destroyed, leaving the body intact with the other fixture.

这里有两张照片来说明我的意思:

Here's two pictures to demonstrate what I mean:

使用两个固定装置:

With both fixtures:

只有一个固定装置:

With only one fixture:

这是我创建身体的方式:

Here is how I create the body:

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();

这是我销毁尸体的代码:

Here is my code for destroying the bodies:

public static void removeSpecifiedBodies() {
        for (Body body : bodiesToRemoveList) {
            Array<Fixture> fixtures = body.getFixtureList();
            for (Fixture fixture : fixtures) {
                body.destroyFixture(fixture);
            }
        }
        bodiesToRemoveList.clear();
    }

在我的b2world被踩踏之后,我将此静态方法称为.我检查了日志记录,固定装置的大小为2,并且正在运行两次,但是只有一个固定装置被破坏了.为什么会这样呢?什么被破坏了?它运行了两次,但我只看到其中一个被破坏了.

I call this static method after my b2world is stepped. I checked the logging, and the fixtures size is 2, and it is being run twice, but only one fixture is being destroyed. Why is this happening? And what is being destroyed? It runs twice, but I'm only seeing one of them getting destroyed.

我没有使用上面的remove方法,而是添加了

Instead of using that above remove, method, I added

for(Body body : CollisionHandler.bodiesToRemoveList)
            b2world.destroyBody(body);

在b2world.step之后,但它冻结了所有内容. :(

after b2world.step, but it froze everything. :(

推荐答案

GetFixtureList仅返回第一个灯具.您需要说

GetFixtureList only returns the first fixture. You need to say

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

这篇关于Box2D-无法销毁多个灯具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆