Using Box2d Debug Renderer in Libgdx

This is an update to the older example (“using Box2d in Libgdx”) we put in the post here.

We added box2d debug renderer to the code which is very simple to add and useful in seeing how box2d objects look like.

In all of our example the objects we create in Box2d are scaled down when compared to real world (by a factor of 100). So to use debug renderer just create a new matrix using the spritebatch projection matrix and then scale it by the factor 100. Then use that matrix to render the debug bodies. Here is the part of snippet of the class GameLoop present in the example.

Adding Box2d Debug Renderer

SpriteBatch spritebatch;
Box2DDebugRenderer debugRenderer;
Matrix4 debugMatrix;

public GameLoop(int screenId,OrthographicCamera cam){
        //.....
//Create a copy of camera projection matrix
    debugMatrix=new Matrix4(cam.combined);

//BoxObjectManager.BOX_TO_WORLD = 100f
//Scale it by 100 as our box physics bodies are scaled down by 100
debugMatrix.scale(BoxObjectManager.BOX_TO_WORLD, BoxObjectManager.BOX_TO_WORLD, 1f);

debugRenderer=new Box2DDebugRenderer();

}

public void render(){

        //.....
        spritebatch.begin();
        //BoxObjectManager.GetWorld() gets the reference to Box2d World object
debugRenderer.render(BoxObjectManager.GetWorld(), debugMatrix);
spritebatch.end();
}

Source Code:
Here is the link to new source code.

Thank you

Tagged with: , , , , ,
Posted in Tutorials
4 comments on “Using Box2d Debug Renderer in Libgdx
  1. Emilio says:

    Finally! I searched everywhere trying to find the right way to scale the camera for the debug renderer.

    THANKS!!!!

    PS: you can just type cam.combined.cpy() to get a matrix copy

  2. How to exactly enable debug render? What lines should be commented? When I do nothing, then game is being rendered with textures. If I comment texture rendering in render method, then I see just black screen :(

1 Pings/Trackbacks for "Using Box2d Debug Renderer in Libgdx"
  1. [...] Box2d renderer… how to use it : http://rotatingcanvas.com/using-box2d-debug-renderer-in-libgdx/ [...]

Try our games



Error: Twitter did not respond. Please wait a few minutes and refresh this page.