我已经创建了一个落球的基本示例,但我有点困惑为什么物体在落下时没有加速。它以恒定的速度行驶,这不是我所期望的。这是我使用 Box2D 的第一天,我想我错过了一些基本的东西,但无法弄清楚。
public PhysicsWorld() {
// Step 1: Create Physics World Boundaries
Vec2 gravity = new Vec2(0, 20);
boolean doSleep = true;
world = new World(gravity, doSleep);
// Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(100, 100);
body = world.createBody(bodyDef);
MassData md = new MassData();
md.mass = 5;
body.setMassData(md);
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);
velocityIterations = 6;
positionIterations = 2;
}
public void update() {
world.step(timeStep, velocityIterations, positionIterations);
Log.i("body", "x: " + body.getPosition().x + " y: " + body.getPosition().y);
}
输出:
01-22 21:17:20.750: I/body(7698): x: 100.0 y: 102.0
01-22 21:17:20.777: I/body(7698): x: 100.0 y: 104.0
01-22 21:17:20.796: I/body(7698): x: 100.0 y: 106.0
01-22 21:17:20.824: I/body(7698): x: 100.0 y: 108.0
01-22 21:17:20.847: I/body(7698): x: 100.0 y: 110.0
我希望每次迭代都会应用重力并增加 Y 方向的球速度。