我已将C手册中的Hello World示例移植到jbox2d.这只是一个逐行端口.显然你需要编写一个基本的
java程序并调用这段代码.你还需要导入一些库,我在StackOverflow中的导入格式有问题所以我将它们排除在外.希望您的IDE将为您处理导入.
// Static Body
Vec2 gravity = new Vec2(0,-10);
World world = new World(gravity);
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();
System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}