2
\$\begingroup\$

Can somebody tell me how to correctly handle Box2D "deltaTime"? When I have non-Box2D objects it's sufficient to multiply velocity by deltaTime but what should I do when I use Box2D world and bodies? multiply for example linearVelocity by delta or timeStep?

\$\endgroup\$
2
\$\begingroup\$

You shouldn't multiply the delta time by anything for Box2D, just pass it in to the World when you do the time step.

// Somewhat condensed...
public class YourGameplayClass {
    private World world;
    private int velocityIterations = 4;
    private int positionIterations = 6;

    public void update() {
        world.step(Gdx.graphics.getDeltaTime(), velocityIterations, positionIteration); 
    }
}

Box2D will move your objects according to the stepped delta time and take their velocity into account for you.

\$\endgroup\$
2
  • \$\begingroup\$ I tried world.step(Gdx.graphics.getDeltaTime(), velocityIterations, positionIteration) and games work but only if you have 60fps. I tested game on my old smartphone and everything worked 2 times faster and game was unplayable \$\endgroup\$
    – Land93
    Dec 1 '16 at 15:58
  • 1
    \$\begingroup\$ That problem has to do with your physics not bring decoupled from your frame rate. Gaffer on Games covers how to fix your time step in this article; gafferongames.com/game-physics/fix-your-timestep . Regardless, you step a World by passing a delta to the step function, not by manually trying to calculate velocities. \$\endgroup\$
    – bornander
    Dec 2 '16 at 5:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .