Search code examples
javalibgdxbox2d

Box2D. Trying to understand how much force is needed


I'm trying to understand the amount of force i need to move my object. This is how my world is setup and the physics step is done

private void setupWorld() {
    mWorld = new World(new Vector2(0f, -9.8f), true);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(x, y);

    body = world.createBody(bodyDef);

    PolygonShape box=new PolygonShape();
    box.setAsBox(1,1);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = box;
    fixtureDef.density = 1f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;

    Fixture fixture = body.createFixture(fixtureDef);
    box.dispose();
}

private void doPhysicsStep(float deltaTime) {
    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    while (accumulator >= TIME_STEP) {
        body.applyForceToCenter(new Vector2(0, 10f), true);
        world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
        accumulator -= (TIME_STEP);
    }
}

So i've got a 1x1 box with a density 1. Gravity is set at -9.8 and i'm expecting that when I apply an amount of force to my box that is greater than the gravity (in this example i've set it to 10) that the box should start moving up.

But the box doesn't move. I have to set the force to about 80 (i.e. body.applyForceToCenter(new Vector2(0, 80f), true);) before it starts to move the box.

I've considered that this is due to my time step (which i've currently set to 1/60f), but if anything taking that into account would reduce the force I'm applying in each step.

Can someone explain what i'm miscalculating here?


Solution

  • Your box has a mass of 4, not 1, because in method setAsBox(float hx, float hy) hx means half of desired width, and hy means half of desired height. So if you want to have a box 1 x 1 you will call setAsBox(0.5F, 0.5F).

    But this doesn't explain why you need a force of 80 to move it, because force of 50 should be enough to make a difference.

    Fg = m * g = 9.8 * 4 = 39.2

    On my test project on object of mass 4 even the force of 40 is noticeable when applied programmatically (the delay of application start and pressing the button is significant so I avoid it).