【问题标题】:Box2D static body collision performance issueBox2D 静态物体碰撞性能问题
【发布时间】:2012-12-20 19:23:35
【问题描述】:

我正在使用 JBox2d 在我正在处理的游戏项目中执行碰撞检测。 我用静态物体代表世界上的障碍。每当动态物体(即游戏角色)与其中一个障碍物发生碰撞时,性能就会出现非常明显的下降。 Fps 将从 ~120 下降到 ~5。当静态物体的角碰撞时,这种情况似乎更频繁地发生。

当我将世界障碍物的身体类型设置为动态而不是静态,密度非常高(以防止身体在碰撞时移动),这个问题就消失了......这个解决方案并不理想然而我的情况......

关于什么可能导致这种巨大的 fps 下降的任何想法?

这是我用来创建静态主体的代码:

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,但这没有任何帮助。

谢谢!

【问题讨论】:

    标签: java performance box2d physics jbox2d


    【解决方案1】:

    这是我目前正在开发的游戏的代码,运行良好。希望如果您复制和粘贴,更改一些变量名称和内容可能会解决您的问题。我是 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多