Hi,
I've to use Jbox2d v2.0.1 for a project. But i don't know how the library work exactly so I did a basic test but the result isn't what I expected.
Here is the code:
With that code I expected that I could see the value in Y decrease in the console until the square stop on the ground.
But here is the console output :
So for what I understand there is something that i'm doing wrong with the world.step().
If some can help me that be great.
I've to use Jbox2d v2.0.1 for a project. But i don't know how the library work exactly so I did a basic test but the result isn't what I expected.
Here is the code:
Code:
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.MassData;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.collision.PolygonShape;
import org.jbox2d.collision.ShapeDef;
import org.jbox2d.common.Color3f;
import org.jbox2d.common.Vec2;
import org.jbox2d.common.XForm;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.DebugDraw;
import org.jbox2d.dynamics.World;
public class Main{
public static void main(String[] args) {
AABB aabb = new AABB();
aabb.lowerBound.set(-1.0f, -1.0f);
aabb.upperBound.set(1.0f, 1.0f);
//world creation
Vec2 gravity = new Vec2(0.0f, -10.0f);
boolean sleep = true;
World world = new World(aabb, gravity, sleep);
//ground creation
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(0, -10f);
Body groundBody = world.createBody(groundBodyDef);
PolygonDef groundBoxDef = new PolygonDef();
groundBoxDef.setAsBox(10f, 50f);
PolygonShape groundBox = new PolygonShape(groundBoxDef);
groundBody.createShape(groundBoxDef);
//dynamic body creation
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(0.0f, 100.0f);
bodyDef.massData.mass=0.5f;
PolygonDef polyDef = new PolygonDef();
polyDef.setAsBox(0.5f, 0.5f);
polyDef.density = 5.0f;
polyDef.restitution = 0.0f;
polyDef.friction = 0.5f;
Body body = world.createBody(bodyDef);
body.createShape(polyDef);
for (int i = 0; i < 100; ++i){
[b] world.step((float)10 / 1000, 10);[/b]
Vec2 position = body.getPosition();
float angle = body.getAngle();
System.out.printf("%f %f %f\n", position.x, position.y, angle);
}
}
}
import org.jbox2d.collision.MassData;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.collision.PolygonShape;
import org.jbox2d.collision.ShapeDef;
import org.jbox2d.common.Color3f;
import org.jbox2d.common.Vec2;
import org.jbox2d.common.XForm;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.DebugDraw;
import org.jbox2d.dynamics.World;
public class Main{
public static void main(String[] args) {
AABB aabb = new AABB();
aabb.lowerBound.set(-1.0f, -1.0f);
aabb.upperBound.set(1.0f, 1.0f);
//world creation
Vec2 gravity = new Vec2(0.0f, -10.0f);
boolean sleep = true;
World world = new World(aabb, gravity, sleep);
//ground creation
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(0, -10f);
Body groundBody = world.createBody(groundBodyDef);
PolygonDef groundBoxDef = new PolygonDef();
groundBoxDef.setAsBox(10f, 50f);
PolygonShape groundBox = new PolygonShape(groundBoxDef);
groundBody.createShape(groundBoxDef);
//dynamic body creation
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(0.0f, 100.0f);
bodyDef.massData.mass=0.5f;
PolygonDef polyDef = new PolygonDef();
polyDef.setAsBox(0.5f, 0.5f);
polyDef.density = 5.0f;
polyDef.restitution = 0.0f;
polyDef.friction = 0.5f;
Body body = world.createBody(bodyDef);
body.createShape(polyDef);
for (int i = 0; i < 100; ++i){
[b] world.step((float)10 / 1000, 10);[/b]
Vec2 position = body.getPosition();
float angle = body.getAngle();
System.out.printf("%f %f %f\n", position.x, position.y, angle);
}
}
}
With that code I expected that I could see the value in Y decrease in the console until the square stop on the ground.
But here is the console output :
Code:
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
... don't change
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
0.000000 99.999001 0.000000
... don't change
So for what I understand there is something that i'm doing wrong with the world.step().
If some can help me that be great.