1
\$\begingroup\$

In my game I use box2d objects and, as I understand it, you can move them only in two ways, SetLinearVelocity, and ApplyImpulse. I need to move the object 32 pixels in 1 turn. So I make a constant speed and the next problem appears. If the laptop is connected to the charge and gives 60 frames, the object begins to move faster. How do I make frameindependent movement? Without connection - 30 FPS and the game makes slowly.

I also know that if you want to be frameIndependent you need to use this formula:

x = x + speedPerSecond * secondsElapsedSinceLastFrame

But i cant use this formula, because i cant set box2d position. It is make box2d itself. I only can set the Velocity :( Or may me i just dont know.

2 - it is meters per second. 64 pixels.
private void handleInput(float delta) {

    if (Gdx.input.isKeyPressed(Input.Keys.W)) {

        b2body.setLinearVelocity(0, 2);
    }

    if (Gdx.input.isKeyPressed(Input.Keys.S)) {

        b2body.setLinearVelocity(0,-2);
    }

    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
        b2body.setLinearVelocity(-2, 0);
    }

    if (Gdx.input.isKeyPressed(Input.Keys.D)) {
        b2body.setLinearVelocity(2, 0);
    }
}

How can i move object in another way or how to make movement frameindependent? SetTransform is not a good decision becasuse collisions are ignored. Thank You!

\$\endgroup\$
5
  • \$\begingroup\$ Box2D has a step ( on your world object ) to update everything, that's where you should put your fixed timestep in. \$\endgroup\$
    – Sidar
    Dec 25, 2017 at 10:33
  • \$\begingroup\$ Yeah! Really! I forgot about box2d update. You are genius! Will try and i write if succesfull \$\endgroup\$
    – Dastin_DV
    Dec 25, 2017 at 10:36
  • \$\begingroup\$ SIDAR! You save my live :) box2DWorld.step(delta, 6, 2); -- just put delta in update world and it become frameindependent. But i don't know. May be it is bad for phisics modelling if FPS is small @Sidar \$\endgroup\$
    – Dastin_DV
    Dec 25, 2017 at 11:01
  • 1
    \$\begingroup\$ You should use a fixed timestep for physics though. I'm not sure if box2D handles it internally but you could try and calculate the delta by dividing 1 by your target fps : 60 fps = 1/60. 30fps = 1/30 etc \$\endgroup\$
    – Sidar
    Dec 25, 2017 at 11:02
  • \$\begingroup\$ But delta is the same that 1/FPS. Now all working good, flying like witcher 3 in low configures. Thank you again :) @Sidar \$\endgroup\$
    – Dastin_DV
    Dec 25, 2017 at 11:06

1 Answer 1

1
\$\begingroup\$

Box2D has a step ( on your world object ) to update everything, that's where you should put your timestep in.

\$\endgroup\$

You must log in to answer this question.

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