Hi.
I'm trying to set up JBox2d. I'm basing my code off the box2d tutorials (http://www.box2d.org/manual.html#_Toc258082968"box2d%20tutorials). This is my code:
Here is the console output:
I have no idea why an exception is being thrown - I'm most likely screwing something up somewhere, but I'm unsure where as I'm still new to box2d.
Cheers.
I'm trying to set up JBox2d. I'm basing my code off the box2d tutorials (http://www.box2d.org/manual.html#_Toc258082968"box2d%20tutorials). This is my code:
Code:
package test;
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.dynamics.World;
public class Game {
static final float timeStep = 1.0f / 60.0f;
static final int velocityIterations = 6;
static final int positionIterations = 2;
static int debugInt = 0;
World world;
BodyDef bodyDef;
Body body;
PolygonShape groundBox;
BodyDef dynamicBodyDef;
Body dynamicBody;
PolygonShape dynamicBox;
FixtureDef dynamicFixDef;
public Game() {
// Create the ground box.
world = new World(new Vec2(0, -10.0f), true);
bodyDef = new BodyDef();
bodyDef.position.set(0, -10.0f);
body = new Body(bodyDef, world);
groundBox = new PolygonShape();
groundBox.setAsBox(50.0f, 10.0f);
body.createFixture(groundBox, 1.0f);
// Create the dynamic body.
dynamicBodyDef = new BodyDef();
dynamicBodyDef.type = BodyType.DYNAMIC;
dynamicBodyDef.position = new Vec2(0, 4.0f);
dynamicBody = world.createBody(dynamicBodyDef);
dynamicBox = new PolygonShape();
dynamicBox.setAsBox(1, 1);
dynamicFixDef = new FixtureDef();
dynamicFixDef.shape = dynamicBox;
dynamicFixDef.density = 1.0f;
dynamicFixDef.friction = 0.3f;
dynamicBody.createFixture(dynamicFixDef);
}
public void run() {
for (int i = 0; i < 60; ++i) {
world.step(timeStep, velocityIterations, positionIterations);
world.clearForces();
Vec2 position = body.getPosition();
float angle = body.getAngle();
System.out.println("iteration = " + i + " pos.x = " + position.x + " pos.y = " + position.y + " angle = " + angle);
}
}
public static void main(String[] args) {
Game game = new Game();
game.run();
}
}
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.dynamics.World;
public class Game {
static final float timeStep = 1.0f / 60.0f;
static final int velocityIterations = 6;
static final int positionIterations = 2;
static int debugInt = 0;
World world;
BodyDef bodyDef;
Body body;
PolygonShape groundBox;
BodyDef dynamicBodyDef;
Body dynamicBody;
PolygonShape dynamicBox;
FixtureDef dynamicFixDef;
public Game() {
// Create the ground box.
world = new World(new Vec2(0, -10.0f), true);
bodyDef = new BodyDef();
bodyDef.position.set(0, -10.0f);
body = new Body(bodyDef, world);
groundBox = new PolygonShape();
groundBox.setAsBox(50.0f, 10.0f);
body.createFixture(groundBox, 1.0f);
// Create the dynamic body.
dynamicBodyDef = new BodyDef();
dynamicBodyDef.type = BodyType.DYNAMIC;
dynamicBodyDef.position = new Vec2(0, 4.0f);
dynamicBody = world.createBody(dynamicBodyDef);
dynamicBox = new PolygonShape();
dynamicBox.setAsBox(1, 1);
dynamicFixDef = new FixtureDef();
dynamicFixDef.shape = dynamicBox;
dynamicFixDef.density = 1.0f;
dynamicFixDef.friction = 0.3f;
dynamicBody.createFixture(dynamicFixDef);
}
public void run() {
for (int i = 0; i < 60; ++i) {
world.step(timeStep, velocityIterations, positionIterations);
world.clearForces();
Vec2 position = body.getPosition();
float angle = body.getAngle();
System.out.println("iteration = " + i + " pos.x = " + position.x + " pos.y = " + position.y + " angle = " + angle);
}
}
public static void main(String[] args) {
Game game = new Game();
game.run();
}
}
Here is the console output:
Quote:
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.
iteration = 0 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 1 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 2 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 3 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 4 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 5 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 6 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 7 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 8 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 9 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 10 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 11 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 12 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 13 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 14 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 15 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 16 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 17 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 18 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 19 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 20 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 21 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 22 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 23 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 24 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 25 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 26 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 27 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 28 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 29 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 30 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 31 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 32 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 33 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 34 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 35 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 36 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 37 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 38 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 39 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 40 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 41 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 42 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 43 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 44 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 45 pos.x = 0.0 pos.y = -10.0 angle = 0.0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at org.jbox2d.dynamics.Island.add(Island.java:413)
at org.jbox2d.dynamics.World.solve(World.java:916)
at org.jbox2d.dynamics.World.step(World.java:560)
at test.Game.run(Game.java:53)
at test.Game.main(Game.java:65)
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
iteration = 0 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 1 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 2 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 3 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 4 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 5 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 6 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 7 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 8 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 9 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 10 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 11 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 12 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 13 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 14 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 15 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 16 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 17 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 18 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 19 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 20 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 21 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 22 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 23 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 24 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 25 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 26 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 27 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 28 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 29 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 30 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 31 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 32 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 33 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 34 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 35 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 36 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 37 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 38 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 39 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 40 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 41 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 42 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 43 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 44 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 45 pos.x = 0.0 pos.y = -10.0 angle = 0.0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at org.jbox2d.dynamics.Island.add(Island.java:413)
at org.jbox2d.dynamics.World.solve(World.java:916)
at org.jbox2d.dynamics.World.step(World.java:560)
at test.Game.run(Game.java:53)
at test.Game.main(Game.java:65)
I have no idea why an exception is being thrown - I'm most likely screwing something up somewhere, but I'm unsure where as I'm still new to box2d.
Cheers.