Box2D 静态刚体碰撞性能问题

4
我正在使用JBox2d来进行游戏项目中的碰撞检测。我用静态刚体表示世界上的障碍物。每当动态刚体(即游戏角色)与其中一个障碍物发生碰撞时,性能会显著下降。帧率将从 ~120 降至 ~5。似乎在静态刚体的角落发生碰撞时更频繁出现这种情况。
当我将世界障碍物的刚体类型设置为动态而不是静态,并设置非常高的密度(以防止在碰撞时移动刚体)时,这个问题消失了...但是,这个解决方案并不适合我的情况......
有什么想法可以解决这个巨大的帧率下降吗?
以下是我用来创建静态刚体的代码:
BodyDef def = new BodyDef();
        def.type = BodyType.STATIC; // If this line is commented and the other     
                               //commented lines are uncommented, the issue goes away.
        //def.type = BodyType.DYNAMIC;
        def.position.set(worldBounds.getCenterX(), worldBounds.getCenterY());
        Body staticBody = b2World.createBody(def);

        PolygonShape box = new PolygonShape();
        box.setAsBox(worldBounds.getWidth() * 0.5f, worldBounds.getHeight() * 0.5f);

        FixtureDef fixture = new FixtureDef();
        fixture.shape = box;
        fixture.friction = 0.3f;
        //fixture.density = 1000000000;
        staticBody.createFixture(fixture);
        //staticBody.setSleepingAllowed(true);
        //staticBody.setFixedRotation(true);

我尝试使用CircleShape而不是PolygonShape,但这并没有帮助解决问题。

谢谢!

1个回答

0

这是我目前正在开发的游戏代码,它可以很好地运行。希望如果你复制和粘贴一些变量名和其他东西,它可能会解决你的问题。我对box2d还很陌生,不能告诉你问题出在哪里。希望能帮到你。

    //bodydef
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.StaticBody;
    bodyDef.position.set(position);
    body = world.createBody(bodyDef);

    //shape
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(dimension.x / 2, dimension.y / 2);

    //fixture
    FixtureDef fixture = new FixtureDef();
    fixture.friction = 0.3f;
    fixture.shape = shape;

    body.createFixture(fixture);
    shape.dispose();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接