2

I have created a basic example of a falling ball but I am slightly confused to why the object is not accelerating while falling. It is travelling at constant speed which isn't what I would expect. This is my first day using Box2D I assume I have missed something basic, but can't figure it out.

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);
}

Output:

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

I would expect gravity to be applied each iteration and increase the balls speed in Y.

Moz
  • 1,494
  • 6
  • 21
  • 32
  • 1
    I think we need the World.step() method to make sense of this all. – Maarten Bodewes Jan 22 '12 at 21:26
  • What is the value of timeStep? – Tim Gee Jan 22 '12 at 21:45
  • its 25f, as my frame rate is 40. Even the first example in the manual has a similar output http://www.box2d.org/manual.html why are the values not taking into account acceleration gravity causes on an object? – Moz Jan 22 '12 at 23:48
  • The only thing I can think of is that you use .setLinearVelocity() elsewhere and override the gravity. Your gravity is upwards btw, but I don't think that would cause a problem. Is it possible to create a complete, short, self-contained, compilable example? – Tim Gee Jan 23 '12 at 18:15

2 Answers2

1

This is because the velocity is capped by the engine and you are not using proper world coordinates. Think of it as a meter-kilogram-second system. You have a 1 by 1 meter box falling at 2 meters per 1/40th of a second, or 80 meters per second. That's pretty fast.

Daniel Murphy
  • 852
  • 7
  • 14
0

http://www.iforce2d.net/b2dtut/gotchas#speedlimit

If your framerate is 40fps, timeStep would typically be 1/40.0f

iforce2d
  • 8,194
  • 3
  • 29
  • 40