# Why won't my Box2d bodies collide? [closed]

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.

    @Override
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;
circleBodyDef.position.set(windowCenter);
circleBody = world.createBody(circleBodyDef);
circleBody.createFixture(circleFixtureDef);

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);
//bottomEdgeBody.createFixture(bottomEdgeFixtureDef);

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

}

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

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
world.step(delta, 10, 10);
ball.setRotation((float)Math.toDegrees(circleBody.getAngle()));
System.out.println(world.getBodyCount());
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?

## closed as too localized by bummzack, Sean Middleditch, MichaelHouse♦, Josh♦, AnkoApr 8 '13 at 8:33

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

• "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. – ClassicThunder Mar 5 '13 at 18:10
• @ClassicThunder So when creating shape sizes I should always consider meters, whereas Body.getPosition() should still be considered as pixels? – lase Mar 5 '13 at 18:39
• 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. – ClassicThunder Mar 5 '13 at 18:44
• @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? – lase Mar 6 '13 at 0:58
• "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. – ClassicThunder Mar 6 '13 at 1:42