Search code examples
debugginglibgdxbulletphysics

DebugDrawing in libGDX


What is the proper way to debug draw Bullet physics in libGDX so that I may see the btCollisionObjects that I am setting up?

So far I have the below, but it doesn't appear that the btCollisionObjects are appearing.

public void render(float delta) {

    debugDrawer.begin(cam);
    collisionWorld.debugDrawWorld();
    debugDrawer.end();
    modelBatch.begin(cam);
    ...
    modelBatch.end();
}

@Override
public void show() {
    Bullet.init();
    ...         
    collisionConfig = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfig);
    broadphase = new btDbvtBroadphase();
    collisionWorld = new btCollisionWorld(dispatcher, broadphase, collisionConfig);
    debugDrawer = new DebugDrawer();
    collisionWorld.setDebugDrawer(debugDrawer);
    debugDrawer.setDebugMode(btIDebugDraw.DebugDrawModes.DBG_MAX_DEBUG_DRAW_MODE);
}

Solution

  • Hopefully this will still help you 3 months after the fact, because your snippets definitely helped me! ;)

    I put your code snippets into my app, and was able to get debug drawing working fine.

    For the record, I'm using a dynamicsWorld instead, but swapped out code and it works.

    dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, constraintSolver, collisionConfig);
    

    The one thing I would suggest is swapping when you draw the models and when you draw the debug. If you put the debug second, it will get drawn last, and thus on top of the models. Otherwise you will experience the debug draw being covered up by the models. Try this instead:

    public void render(float delta) {
    
        modelBatch.begin(cam);
        ...
        modelBatch.end();
    
        debugDrawer.begin(cam);
        collisionWorld.debugDrawWorld();
        debugDrawer.end();
    }