4
votes

I'm using JBox2d to perform collision detection in a game project I'm working on. I represent obstacles in the world with static bodies. Whenever a dynamic body (i.e. a game character) collides with one of these obstacles, there is a very noticeable drop in performance. Fps will drop from ~120 to ~5. This seems to happen more frequently when the corners of the static body are collided with.

When I set the body type of the world obstacles to be dynamic instead of static, with very high density(to prevent the body from moving when it is collided with), this issue disappears... This solution is not ideal for my situation however......

Any ideas as to what could be causing this huge fps drop?

Here's the code I use to create the static bodies:

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

I've tried using a CircleShape instead of a PolygonShape, but that doesn't help anything.

Thank you!

1

1 Answers

0
votes

this is the code from the game im working on at the moment which works fine. hopefully if you copy and paste, change a few variable names and stuff it might sort your problem. im new to box2d so cant tell you exactly where the problem lies. hope it helps.

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