Problems with velocity and big deltaTimes

Anything libgdx related goes here!

Problems with velocity and big deltaTimes

Postby sr.lucas » Tue May 15, 2012 10:17 pm

Hi,
I have a body that I am shooting using libgdx.

the code I am using to shoot it is the following (I don't want it to accelerate, just to leave the cannon at a specific speed)

Gdx.app.log("SHOOT", "s: " + sideA + ", " + sideB);
body.setSpeed(sideA, sideB);
Gdx.app.log("SHOOT", "s: " + body.getSpeed().x + ", " + body.getSpeed().y);

sideA and sideB are constants that are in meters, when everything is OK the body leaves at the desired speed. But when delta time is > 0.1f then the body leaves very slowly.

The output shows the following:

SHOOT:s: -0.12255889, 9.9992485
world:Changing linear velocity to -0.12255889, 9.9992485
SHOOT:s: -0.12255889, 9.9992485
SPEED:s: -0.12255889, 9.9992485
SPEED:s: -0.010300772, 0.8404122
SPEED:s: -0.010300772, 0.8404122

is can be clearly seen that the body speed is changed dramatically from 9.9992485 to 0.8404122 on the Y axis....

any ideas why? Libgdx is changing the body speed on the step loop, but any ideas why?

on this world 1m is about 32px.....
sr.lucas
 
Posts: 8
Joined: Wed Nov 30, 2011 6:04 pm

Re: Problems with velocity and big deltaTimes

Postby cuellarjmcg » Tue May 15, 2012 10:27 pm

Dont use delta time for steps. Just use a fixed value, like 1/60f and you'll be ok.
cuellarjmcg
 
Posts: 226
Joined: Tue Feb 21, 2012 8:57 pm

Re: Problems with velocity and big deltaTimes

Postby sr.lucas » Wed May 16, 2012 3:49 am

It worked, i used our cap value 0.04 and everything workd ok..

what I don't understand is if the deltaTime used to simulate the world is BIGGER, why would the speed be DECREASED?

plus, If I am simulating a fixed time for every update call, would't things move faster on faster devices and slow on slower devices? Cause I am calling the step function more times with a smaller deltaTime value...
sr.lucas
 
Posts: 8
Joined: Wed Nov 30, 2011 6:04 pm

Re: Problems with velocity and big deltaTimes

Postby Mats » Wed May 16, 2012 10:17 am

sr.lucas wrote:plus, If I am simulating a fixed time for every update call, would't things move faster on faster devices and slow on slower devices?

You can accumulate, read about different ways to do it in this article: Fix Your Timestep!
   Image  Image  Image
Mats
 
Posts: 130
Joined: Fri Sep 23, 2011 1:17 pm

Re: Problems with velocity and big deltaTimes

Postby sr.lucas » Wed May 16, 2012 2:15 pm

yes thanks a lot. I came accross this very useful post:

viewtopic.php?f=11&t=1845&p=10046&hilit=step+deltatime#p10046

I would post the solution but can be found in the post above.
sr.lucas
 
Posts: 8
Joined: Wed Nov 30, 2011 6:04 pm

Re: Problems with velocity and big deltaTimes

Postby broken-e » Wed May 16, 2012 4:15 pm

here's my favorite solution

Code: Select all
private float accum = 0f;
private final float step = 1f / 60f;
private final float maxDelta = 1f / 20f; // keep from spiraling to death

private void worldStep(float delta) {
   accum += Math.min(delta, maxDelta);
   while (accum > step) {
      world.step(step, 8, 3);
      accum -= step;
   }
}
broken-e
 
Posts: 667
Joined: Sun Feb 06, 2011 7:20 am


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], hkahleong, Lomelin, tubus and 8 guests