0

Okay, so this is my code (this is just a test of using DebugDraw):

package test;
import org.jbox2d.callbacks.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

public class Main {
private static DebugDraw debugDraw;

public static DebugDraw getDebugDraw() {
    return debugDraw;
}
public static void main(String[] args) {
    Vec2  gravity = new Vec2(0,-10);
boolean doSleep = true;
World world = new World(gravity,doSleep);
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(0, -10);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox(50,10);
groundBody.createFixture(groundBox, 0);

// Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(0, 4);
Body body = world.createBody(bodyDef);
PolygonShape dynamicBox = new PolygonShape();
dynamicBox.setAsBox(1, 1);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicBox;
fixtureDef.density=1;
fixtureDef.friction=0.3f;
body.createFixture(fixtureDef);

// Setup world
float timeStep = 1.0f/60.0f;
int velocityIterations = 6;
int positionIterations = 2;

// Run loop
for (int i = 0; i < 60; ++i)
{
    world.step(timeStep, velocityIterations, positionIterations);
    Vec2 position = body.getPosition();
    float angle = body.getAngle();
    debugDraw.setFlags(debugDraw.e_shapeBit);
    world.setDebugDraw(debugDraw);
    System.out.println(i+": X: "+position.x+" Y: "+position.y+" ANGLE: "+angle);
}

}
}

When I run this code, I get:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NullPointerException
at test.Main.main(Main.java:49)
Java Result: 1

Does anyone know what's causing this and what I should do? I have tried google searches, but all I can find is Slick2D, which I do not want to implant a whole library to test one simple test application.

roperson
  • 81
  • 1
  • 1
  • 4

3 Answers3

0

I had the same problem with jbox2d. It has a dependency on slf4j. The easiest way to solve it is to do a maven clean install in the jbox2d root directory. You will then find the slf4j libraries in the .m2 directory (which itself should be in your home directory).

You then need to add the slf4j library to your project build path and export its jar file together with the target project.

That's the theory. In reality slf4j has a dependency itself on log4j which is not resolved by maven. So in the end I commented out all Log references in jbox2d, and did another maven clean install. That solved the issue for me.

Martin
  • 93
  • 1
  • 1
  • 4
0

FYI, the slf4j dependency has been removed in the latest version, too many people were having trouble with it.

The null pointer exception looks like you're doing something not quite right. Perhaps you should make a DebugDraw implementation? It will be null otherwise.

If you want to play around, it's best to just make a Testbed test. Follow the wiki here

Daniel Murphy
  • 852
  • 7
  • 14
0

"debugDraw" is null causing "debugDraw.setFlags(debugDraw.e_shapeBit)" to fail with an NPE.

Create a class that extends DebugDraw and assign it to your static variable. You need to implement the draw callbacks your self using whatever graphics lib you are using, e.g. Swing. But for starters you can also just write out log statements.

Also, you should move the "debugDraw.setFlags(debugDraw.e_shapeBit)" and "world.setDebugDraw(debugDraw)" statements up before the gameloop, and instead put a "world.drawDebugData()" statement in the gameloop after the world step.

mhvelplund
  • 2,099
  • 3
  • 22
  • 38