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
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
Hey thanks for the tip!
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 🙁
Sorry. I’ve worked this out. I didn’t see black border around objects. 🙂