It depends on what you want to achieve.
If what you're looking for is to, for debugging purposes, see the position of all your bodies you can use the Box2DDebugRenderer
which takes a World
and a transform matrix and renders a lot of information for you (it will for example show you contacts and the difference between a static and a dynamic body).
Box2DDebugRenderer renderer = new Box2DDebugRenderer();
renderer.render(world, camera.combined);
If that doesn't fit your purpose you need to iterate over the bodies in the World
and then for each Body
get all the Fixture
s and render their respective Shape
s. How to do that is slightly different depending on the type of Shape
but for polygons it might look something like this;
void render(Body body, ShapeRenderer renderer) {
for(Fixture f : body.getFixtureList()) {
switch(f.getType) {
case Shape.Type.Polygon:
{
PolygonShape s = (PolygonShape)f.getShape();
// NOTE: You don't want to be newing this up every time, make sure you keep an array of Vector2s and reuse that!
Vector2[] vertices = new Vector2[s.getVertexCount()];
// Build list of transformed vertices
for(int i = 0; i < s.getVertexCount(); ++i) {
vertices[i] = new Vector2();
s.getVertex(i, vertices[i]);
body.getTransform().mul(vertices[i]);
}
// This assumes a closed polygon, the last vertex is connected with the first.
for(int i = 0; i < s.getVertexCount(); ++i) {
Vector2 a = vertices[i];
Vector2 b = vertices[(i + 1) % s.getVertexCount()];
renderer.line(a.x, a.y, b.x, b.y);
}
}
break;
// And then cover the other types, Circle and Egde here.
}
}
}