I'm doing something wrong when trying to get bodies to collide using jBox2d & Slick2d. Here is have just my basic init, update, and render methods. Render and update seem to work fine for drawing (gravity effectively pushes the ball down, and will rotate if I apply an impulse).

I have a feeling I'm overlooking something when creating the edge, but I have no idea what. My shapes/bodies work fine in the testbed, but I'm having no luck in my standalone application. I have experimented with moving the edge body / ball body around, but it doesn't seem to have any effect.

public void init(GameContainer gc, StateBasedGame sbg)
        throws SlickException {
    world = new World(new Vec2(0F, 9.8F), false);
    windowCenter = new Vec2(gc.getWidth() / 2, gc.getHeight() / 2);

    circleFixtureDef.shape = circleShape;
    circleFixtureDef.density = 1f;
    circleFixtureDef.restitution = .6f;

    circleBodyDef.type = BodyType.DYNAMIC;
    circleBodyDef.bullet = true;
    circleBody = world.createBody(circleBodyDef);

    bottomEdge.setAsEdge(new Vec2(0, gc.getHeight()), new Vec2(gc.getWidth(), gc.getHeight()));
    bottomEdgeDef.position.set(0f, gc.getHeight());
    bottomEdgeDef.type = BodyType.STATIC;
    bottomEdgeFixtureDef.restitution = .6f;
    bottomEdgeFixtureDef.density = 0f;
    bottomEdgeBody = world.createBody(bottomEdgeDef);

    ball = new Image("src/main/resources/bball.png");
    float shapeToImageFactor = ball.getWidth() / circleShape.m_radius;
    ball = ball.getScaledCopy(1 / shapeToImageFactor);


public void render(GameContainer gc, StateBasedGame sbh, Graphics g)
        throws SlickException {
    Vec2 ballBodyPos = circleBody.getPosition();
    ball.draw(ballBodyPos.x, ballBodyPos.y);

public void update(GameContainer gc, StateBasedGame sbg, int delta)
        throws SlickException {
    world.step(delta, 10, 10);
    if (world.getContactCount() > 0) {
        System.out.println("thank god, something hit another thing");

Also, the commented out bottomEdgeBody.createFixture(bottomeEdgeFixtureDef) throws a NullPointerException, and I have no idea why. Could this be related to the body not being created properly?

The world body count is at 2 as I think it should be. I was also thinking that when setting the vector's Y position at my screen height, it's actually getting set at screen height meters rather than pixels. Am I overlooking something obvious?

  • 1
    \$\begingroup\$ "I was also thinking that when setting the vector's Y position at my screen height, it's actually getting set at screen height meters rather than pixels." Yes it is. Null pointer exception in the createFixture method are usually because it called while world.step is being executed. \$\endgroup\$ – ClassicThunder Mar 5 '13 at 18:10
  • \$\begingroup\$ @ClassicThunder So when creating shape sizes I should always consider meters, whereas Body.getPosition() should still be considered as pixels? \$\endgroup\$ – lase Mar 5 '13 at 18:39
  • 1
    \$\begingroup\$ Box2D only cares about meters never pixels, even GetPosition returns meters. I recommend a class that converts from meters to pixels and vice versa to make sure everything stays consistent. \$\endgroup\$ – ClassicThunder Mar 5 '13 at 18:44
  • \$\begingroup\$ @ClassicThunder Does my world class have a coordinate system? I don't understand the conversion to pixels because the world size seems infinite with an arbitrary origin? \$\endgroup\$ – lase Mar 6 '13 at 0:58
  • \$\begingroup\$ "world size seems infinite with an arbitrary origin" It is but this doesn't actually make anything difficult. I simply have 16 pixels = 1 meter. So my pixel to meter method return pixels / 16 and my meter to pixel method returns meters * 16. Its simply 2 Cartesian Coordinate systems and you create a mapping between them. My mapping is that I the Box2D system is 1/16th of the monitor's system. \$\endgroup\$ – ClassicThunder Mar 6 '13 at 1:42