Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DebugDraw class in JBox2D
#1
Here's my code:
Code:
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);
    }

    }
    }

And here's the ouput:
Code:
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

I'm not entirely sure what is causing the NPE. Can anyone help?
Btw, this is just a test, not a actual game.
EDIT: Line 49 is "debugDraw.setFlags(debugDraw.e_shapeBit);"
Reply


Messages In This Thread
DebugDraw class in JBox2D - by Dignity - 07-14-2012, 12:35 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)