您好,您能告诉我在哪里找到关于使用jbox2d编程游戏的教程吗?
我已经将 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);
}