Hello All,
Please forgive if this question or something like this has been asked before.....
I'm having a bit of trouble getting this simple example to work. It seems as though the shape falls, but it's linear velocity (y) does not increase or decrease when it hits the ground. The result is a box that falls but not very realistically (velocity is constant) and it just jitters when it hits the ground. Please advise....
Here is the class:
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.World;
public class Box2dTest {
public int targetFPS = 40;
public int timeStep = (1000 / targetFPS);
private World world;
private BodyDef groundBodyDef;
private PolygonShape groundShapeDef;
private Body actorBody;
public void create() {
Vec2 gravity = new Vec2(0, -10);
boolean doSleep = true;
world = new World(gravity, doSleep);
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonShape();
groundShapeDef.setAsBox((float) 50.0, (float) 1);
groundBody.createFixture(groundShapeDef, 0);
}
public void addBox() {
BodyDef actorBodyDef = new BodyDef();
actorBodyDef.type = org.jbox2d.dynamics.BodyType.DYNAMIC;
actorBodyDef.position.set(0, 50);
actorBody = world.createBody(actorBodyDef);
PolygonShape actorBox = new PolygonShape();
actorBox.setAsBox(1, 1);
org.jbox2d.dynamics.FixtureDef fixtureDef = new org.jbox2d.dynamics.FixtureDef();
fixtureDef.shape = actorBox;
fixtureDef.density = 1;
fixtureDef.friction = 0.3f;
fixtureDef.restitution =1f;
actorBody.createFixture(fixtureDef);
}
public void update() {
world.step(timeStep, 8, 3);
Vec2 position = actorBody.getPosition();
float angle = actorBody.getAngle();
Vec2 velocity = actorBody.getLinearVelocity();
System.out.println("Pos: (" + position.x + ", " + position.y + "), vel: " + velocity.y);
}
}
and here is the main loop:
public static void main(String[] args) {
Box2dTest t = new Box2dTest();
t.create();
t.addBox();
for(int i=0; i < 60; i++) {
t.update();
}
}
some output:
Thanks in advance!
Please forgive if this question or something like this has been asked before.....
I'm having a bit of trouble getting this simple example to work. It seems as though the shape falls, but it's linear velocity (y) does not increase or decrease when it hits the ground. The result is a box that falls but not very realistically (velocity is constant) and it just jitters when it hits the ground. Please advise....
Here is the class:
Code:
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.World;
public class Box2dTest {
public int targetFPS = 40;
public int timeStep = (1000 / targetFPS);
private World world;
private BodyDef groundBodyDef;
private PolygonShape groundShapeDef;
private Body actorBody;
public void create() {
Vec2 gravity = new Vec2(0, -10);
boolean doSleep = true;
world = new World(gravity, doSleep);
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonShape();
groundShapeDef.setAsBox((float) 50.0, (float) 1);
groundBody.createFixture(groundShapeDef, 0);
}
public void addBox() {
BodyDef actorBodyDef = new BodyDef();
actorBodyDef.type = org.jbox2d.dynamics.BodyType.DYNAMIC;
actorBodyDef.position.set(0, 50);
actorBody = world.createBody(actorBodyDef);
PolygonShape actorBox = new PolygonShape();
actorBox.setAsBox(1, 1);
org.jbox2d.dynamics.FixtureDef fixtureDef = new org.jbox2d.dynamics.FixtureDef();
fixtureDef.shape = actorBox;
fixtureDef.density = 1;
fixtureDef.friction = 0.3f;
fixtureDef.restitution =1f;
actorBody.createFixture(fixtureDef);
}
public void update() {
world.step(timeStep, 8, 3);
Vec2 position = actorBody.getPosition();
float angle = actorBody.getAngle();
Vec2 velocity = actorBody.getLinearVelocity();
System.out.println("Pos: (" + position.x + ", " + position.y + "), vel: " + velocity.y);
}
}
and here is the main loop:
Code:
public static void main(String[] args) {
Box2dTest t = new Box2dTest();
t.create();
t.addBox();
for(int i=0; i < 60; i++) {
t.update();
}
}
some output:
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.
Pos: (0.0, 48.0), vel: -0.08
Pos: (0.0, 46.0), vel: -0.08
Pos: (0.0, 44.0), vel: -0.08
Pos: (0.0, 42.0), vel: -0.08
Pos: (0.0, 40.0), vel: -0.08
Pos: (0.0, 38.0), vel: -0.08
Pos: (0.0, 36.0), vel: -0.08
Pos: (0.0, 34.0), vel: -0.08
Pos: (0.0, 32.0), vel: -0.08
Pos: (0.0, 30.0), vel: -0.08
Pos: (0.0, 28.0), vel: -0.08
Pos: (0.0, 26.0), vel: -0.08
Pos: (0.0, 24.0), vel: -0.08
Pos: (0.0, 22.0), vel: -0.08
Pos: (0.0, 20.0), vel: -0.08
Pos: (0.0, 18.0), vel: -0.08
Pos: (0.0, 16.0), vel: -0.08
Pos: (0.0, 14.0), vel: -0.08
Pos: (0.0, 12.0), vel: -0.08
Pos: (0.0, 10.0), vel: -0.08
Pos: (0.0, 8.0), vel: -0.08
Pos: (0.0, 6.0), vel: -0.08
Pos: (0.0, 4.0), vel: -0.08
Pos: (0.0, 2.0), vel: -0.08
Pos: (0.0, 0.0), vel: -0.08
Pos: (0.0, -2.0), vel: -0.08
Pos: (0.0, -4.0), vel: -0.08
Pos: (0.0, -6.0), vel: -0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Pos: (0.0, 48.0), vel: -0.08
Pos: (0.0, 46.0), vel: -0.08
Pos: (0.0, 44.0), vel: -0.08
Pos: (0.0, 42.0), vel: -0.08
Pos: (0.0, 40.0), vel: -0.08
Pos: (0.0, 38.0), vel: -0.08
Pos: (0.0, 36.0), vel: -0.08
Pos: (0.0, 34.0), vel: -0.08
Pos: (0.0, 32.0), vel: -0.08
Pos: (0.0, 30.0), vel: -0.08
Pos: (0.0, 28.0), vel: -0.08
Pos: (0.0, 26.0), vel: -0.08
Pos: (0.0, 24.0), vel: -0.08
Pos: (0.0, 22.0), vel: -0.08
Pos: (0.0, 20.0), vel: -0.08
Pos: (0.0, 18.0), vel: -0.08
Pos: (0.0, 16.0), vel: -0.08
Pos: (0.0, 14.0), vel: -0.08
Pos: (0.0, 12.0), vel: -0.08
Pos: (0.0, 10.0), vel: -0.08
Pos: (0.0, 8.0), vel: -0.08
Pos: (0.0, 6.0), vel: -0.08
Pos: (0.0, 4.0), vel: -0.08
Pos: (0.0, 2.0), vel: -0.08
Pos: (0.0, 0.0), vel: -0.08
Pos: (0.0, -2.0), vel: -0.08
Pos: (0.0, -4.0), vel: -0.08
Pos: (0.0, -6.0), vel: -0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Thanks in advance!