Debugging JBox2d with PulpCore

JBox2D is pretty easy to use until it isn’t, so when that happens it is quite helpful to get some details on what is going on in the physics world. The Box2D manual is somewhat cryptic and so is the JBox2D port, there is a DebugDraw class as promised in the docs but not much of a hint on how to use it. Luckily there is source code and it “explains” things well enough.

Scroll all the way to the end of this post to find out how things turned out or keep reading to see how I got there.

First step is indeed extending the org.jbox2d.dynamics.DebugDraw class and implementing its drawing methods, note that I will using the java.awt.Graphics2D instead of PulpCore’s CoreGraphics class, mostly because I did not wanted to implement circle drawing (Yes, I know it is simple enough, but why bother?)

public class PulpCoreDebugDraw extends DebugDraw {
private Graphics2D g;
private final float scale;

public PulpCoreDebugDraw(int width, int height, float scale) {
super(new OBBViewportTransform());
this.scale = scale;
viewportTransform.setCamera(width / 2 / scale, height / 2 / scale, scale);
viewportTransform.setExtents(width / 2, height / 2);
}

public void setGraphics(Graphics2D g) {
this.g = g;
}

@Override
public void drawCircle(Vec2 center, float radius, Color3f color) {
Color oldColor = g.getColor();
g.setColor(new Color(color.x / 255, color.y / 255, color.z / 255));
Vec2 v = new Vec2(center.x - radius, center.y - radius);
viewportTransform.getWorldToScreen(v, v);
int r = (int) (radius * scale);
g.drawArc((int) v.x, (int) v.y, 2 * r, 2 * r, 0, 360);
g.setColor(oldColor);
}

Now wrap the PulpCoreDebugDraw into a Sprite so that it can be added to Scene2D. The Java2DSprite class can be found in the contrib directory of the PulpCore distribution.

public class JBox2DDebugSprite extends Java2DSprite {
private final World world;
private PulpCoreDebugDraw debugDraw;

public JBox2DDebugSprite(World world, int x, int y, int w, int h, float scale) {
super(x, y, w, h);
this.world = world;
debugDraw = new PulpCoreDebugDraw(w, h, scale);
world.setDebugDraw(debugDraw);
}

@Override
protected void draw(Graphics2D g) {
debugDraw.setGraphics(g);
world.drawDebugData();
}

@Override
protected boolean needsRedraw() {
return true;
}
}

All the is left is adding the JBox2DDebugSprite to your scene and telling the JBox2D World to use it. The debug flags are explained in the DebugDraw source code.

world.setDrawDebugData(true);
add(new JBox2DDebugSprite(world, 0, 0, Stage.getWidth(), Stage.getHeight(), WORLD_TO_STAGE_SCALE));
world.getDebugDraw().setFlags(
DebugDraw.e_shapeBit | DebugDraw.e_coreShapeBit | DebugDraw.e_jointBit | DebugDraw.e_controllerBit);

Download the source code, PulpCore and JBox2D not included.

One thought on “Debugging JBox2d with PulpCore

  1. Anonymous

    when I build the project from eclipse I can see the debugdraw working but if I try to build and run it using ant, the player shows a black screen and if I try to use the menu it gets locked. Have you tried it? I get no errors. BTW good job!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *