hi, first of all, I need to point out that I'm pretty new to JBox2D and that I have read and study the Box2D manual in C++ and the java TestBed for JBox2D.
so, this is my problem :
as you can see, I'm trying to draw the PolygonShape using the vertices in DebugDraw mode and it giving me this error when I'm trying to run it :
I'm a bit confused about the DebugDraw, so I have no idea where did I do wrong.
can somebody show me where I do wrong?
so, this is my problem :
Code:
package menarabalok;
import org.jbox2d.common.Color3f;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.callbacks.DebugDraw;
public class TestBalok extends BasicGame {
private World dunia;
private float timeStep = 1.0f/60.0f;
private int velocityIterations = 8;
private int positionIterations = 3;
private Image bg = null;
private Image jet = null;
private Image dasar = null;
private DebugDraw dDraw;
//private TestPolygon poli;
private PolygonShape jetShape, dasarShape;
private BodyDef jetDef, dasarDef;
private FixtureDef jetFix, dasarFix;
private Body jetBd, dasarBd;
public TestBalok(){
super("Test Balok");
Vec2 grav = new Vec2(0.0f, 0.1f);
boolean doSleep = true;
dunia = new World(grav, doSleep);
//poli = new TestPolygon();
//poli.buatPolygon();
Vec2[] vertices = new Vec2[3];
jetShape = new PolygonShape();
vertices[0] = new Vec2(-1.0f, 0.0f);
vertices[1] = new Vec2(1.0f, 0.0f);
vertices[2] = new Vec2(0.0f, 0.5f);
jetShape.set(vertices, 3);
jetDef = new BodyDef();
jetDef.type = BodyType.DYNAMIC;
jetDef.position.set(0.1f, 0.1f);
jetFix = new FixtureDef();
jetFix.shape = jetShape;
jetFix.density = 1.0f;
jetBd = dunia.createBody(jetDef);
jetBd.createFixture(jetFix);
dasarShape = new PolygonShape();
dasarShape.setAsBox(400.0f, 55.0f);
dasarDef = new BodyDef();
dasarDef.position.set(0.0f, 400.0f);
dasarFix = new FixtureDef();
dasarFix.shape = dasarShape;
dasarFix.density = 1.0f;
dasarFix.restitution = 1.0f;
dasarBd = dunia.createBody(dasarDef);
dasarBd.createFixture(dasarFix);
dDraw.drawPolygon(vertices, 3, Color3f.RED);
}
public World getWorld(){
return dunia;
}
@Override
public void init(GameContainer gc) throws SlickException {
bg = new Image("data/bg.jpg");
jet = new Image("data/jet.png");
dasar = new Image("data/dasar.png");
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
dunia.step(timeStep, velocityIterations, positionIterations);
//float x = poli.getPostitionX();
//float y = poli.getPostitionY();
float x = jetBd.getPosition().x;
float y = jetBd.getPosition().y;
float q = dasarBd.getPosition().x;
float w = dasarBd.getPosition().y;
bg.draw(0.0f, 0.0f);
jet.draw(x, y);
dasar.draw(q, w);
}
}
import org.jbox2d.common.Color3f;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.callbacks.DebugDraw;
public class TestBalok extends BasicGame {
private World dunia;
private float timeStep = 1.0f/60.0f;
private int velocityIterations = 8;
private int positionIterations = 3;
private Image bg = null;
private Image jet = null;
private Image dasar = null;
private DebugDraw dDraw;
//private TestPolygon poli;
private PolygonShape jetShape, dasarShape;
private BodyDef jetDef, dasarDef;
private FixtureDef jetFix, dasarFix;
private Body jetBd, dasarBd;
public TestBalok(){
super("Test Balok");
Vec2 grav = new Vec2(0.0f, 0.1f);
boolean doSleep = true;
dunia = new World(grav, doSleep);
//poli = new TestPolygon();
//poli.buatPolygon();
Vec2[] vertices = new Vec2[3];
jetShape = new PolygonShape();
vertices[0] = new Vec2(-1.0f, 0.0f);
vertices[1] = new Vec2(1.0f, 0.0f);
vertices[2] = new Vec2(0.0f, 0.5f);
jetShape.set(vertices, 3);
jetDef = new BodyDef();
jetDef.type = BodyType.DYNAMIC;
jetDef.position.set(0.1f, 0.1f);
jetFix = new FixtureDef();
jetFix.shape = jetShape;
jetFix.density = 1.0f;
jetBd = dunia.createBody(jetDef);
jetBd.createFixture(jetFix);
dasarShape = new PolygonShape();
dasarShape.setAsBox(400.0f, 55.0f);
dasarDef = new BodyDef();
dasarDef.position.set(0.0f, 400.0f);
dasarFix = new FixtureDef();
dasarFix.shape = dasarShape;
dasarFix.density = 1.0f;
dasarFix.restitution = 1.0f;
dasarBd = dunia.createBody(dasarDef);
dasarBd.createFixture(dasarFix);
dDraw.drawPolygon(vertices, 3, Color3f.RED);
}
public World getWorld(){
return dunia;
}
@Override
public void init(GameContainer gc) throws SlickException {
bg = new Image("data/bg.jpg");
jet = new Image("data/jet.png");
dasar = new Image("data/dasar.png");
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
dunia.step(timeStep, velocityIterations, positionIterations);
//float x = poli.getPostitionX();
//float y = poli.getPostitionY();
float x = jetBd.getPosition().x;
float y = jetBd.getPosition().y;
float q = dasarBd.getPosition().x;
float w = dasarBd.getPosition().y;
bg.draw(0.0f, 0.0f);
jet.draw(x, y);
dasar.draw(q, w);
}
}
as you can see, I'm trying to draw the PolygonShape using the vertices in DebugDraw mode and it giving me this error when I'm trying to run it :
Code:
Exception in thread "main" java.lang.NullPointerException
at menarabalok.TestBalok.<init>(TestBalok.java:78)
at menarabalok.MenaraBalok.main(MenaraBalok.java:9)
Java Result: 1
at menarabalok.TestBalok.<init>(TestBalok.java:78)
at menarabalok.MenaraBalok.main(MenaraBalok.java:9)
Java Result: 1
I'm a bit confused about the DebugDraw, so I have no idea where did I do wrong.
can somebody show me where I do wrong?