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
[sourcecode language=”java”]
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();
}
[/sourcecode]
Source Code:
Here is the link to new source code.
Thank you