How do I Use dt (delta time) correctly

0 favourites
  • 8 posts
From the Asset Store
Time rewind like in "Braid". Choose objects that will be affected by time rewind
  • Hi everyone,

    I have read a lot about dt, lots of forum questions and the very helpful post:

    scirra.com/tutorials/67/delta-time-and-framerate-independence

    but every time i try to put it in to practice, I realize that I actually don't understand it.

    This is what I'm using, 2 PC's running chrome only with webgl enabled

    one PC runs at 60fps, the other 20-40 fps when playing the game

    Here are 2 scenarios that I'm struggling with.

    1.

    I have a sprite animation of 34 frames, its initial animation speed is 40,

    so I expect this to run a little faster than a second.

    obviously this slows down considerably on the bad fps PC

    = so I saw "NewOne" I think his name was, saying that dt basically means 60/1

    now i set my animation speed like this:

    set sprite speed to 40*60*dt

    this looks more or less fine on paper when i log it,

    60fps: speed: 40.0079999585

    +-30fps: speed: 79.9752000254 OR 60 OR 40.005600079 depending on dt at that time before I start it

    so it sounds like they should finish same time, but bad-fps-pc is always lagging behind on screen,

    I suppose because I set the speed once before it starts and not update while animating, since dt would be changing right.

    also, in the case of speed > 60fps, doesn't it just max to 60fps thus making it animate slower than dt said it should to keep up?

    this is getting long, sorry about this, but thank you for your time

    and here is scenario 2: (and this is interesting because i read Ashley saying the (Every X second event, is frame independent), but I observe it differently)

    2.

    I have a SpriteFont that updates it's text to the time that is counting down.

    Originally I had:

    (int) Timer = 60

    Every 1 second -> subtract 1 from Timer

    -> SpriteFont set text to Timer

    60fps: Runs just fine

    +-30fps: lags behind, and the more time passes, the wider the time gap between the two

    then I tried:

    Every Tick -> subtract 1*dt from Timer

    Every Second -> set text to floor(Timer)

    makes no difference, or makes too small a difference to say that it works,

    Time still lags behind and increases in difference the longer the time

    ______________________________________________________________________

    So basically I'm just stupid and don't know how to implement this properly

    Or I'm seriously missing something about dt,

    or how dt, timers, sprite animations and chrome all fit together.

    _____________________________________________________________________

    Thank you for taking the time to read this, if there's any clarity I can provide please let me know

    I greatly appreciate any insight and help on this.

    Thank you

  • Animations, many system events and behaviors are automatically adjusting their speed to fps, they are already using dt for this.

    Same animation on 60fps and 30fps PCs should take the same time (ideally), "Every 1 second" event should take 1 second, bullet with speed 1000 will cover 1000px etc.

    If your second PC is so slow and laggy that C2 engine can't compensate it, then you will not be able to do this with dt either.

    Normally you only need to use dt when you are controlling objects with events, like moving a sprite every x pixels every tick, or scaling/scrolling the view.

    Edit: I did some testing, looks like dt can't be bigger than 0.0333. So if fps is below 30, the dt will still be 0.0333 and the game will run slow. You can't fix this with dt, because the actual frame duration is longer than 0.033

    Maybe you can do this by getting system time with JS or some addon..

  • Thank you for your response, and your time, i really appreciate it

    [quote:i3a41aej]"Every 1 second" event should take 1 second, bullet with speed 1000 will cover 1000px etc.

    If your second PC is so slow and laggy that C2 engine can't compensate it, then you will not be able to do this with dt either.

    I hear what you are saying, so if there is not enough juice supplied to the C2 Engine, then the internals run slower yeah?

    but if I look at the Windows' clocks on both machines, they tick at exactly the same rate,

    So is the C2 timer internal and not getting its time from the PC's internal clock? is there a way to access that?

    I read somewhere as well that Chrome doesn't have a high performance clock built in, does that affect C2's clock integrity?

    I'm also going to try set my project to webgl Disabled, to see what happens when it's being processed by the CPU and not the GPU,

    Not holding my breath but will be interesting to see what happens

  • OK, so interestingly enough this happened:

    and I know you said its wrong, but hear me out for now

    remember the code of:

    every tick -> subtract 1 * dt from Timer

    every second -> set text to floor(Timer)

    now that i exported with webgl disabled, the timer jumps

    so every third "supposed" second that the text updates

    the text indicates 2 seconds have passed and not one

    eg. it will go: 60,59,57,56,54,53,51... etc.

    so either the tick is happening correctly and the <Every second event> is lagging behind

    or the tick*dt comes out to more than 1 second.

    <EDIT>

    So i restarted both and subsequently it has not skipped time again.

    the bad pc's animation is still a little behind but not by as much

    and the timer actually ticks in uniform now.

    meaning that it may still be 1second + something extra,

    but both machines do it at the same rate

    so when the timer starts on both and there is maybe a 1second difference between the 2

    that 1 second difference stays constant no matter how much time elapses.

    Anyone know why this is different between processing on GPU and CPU?

    specifically why time can lose integrity when GPU doesn't have enough juice?

  • Hi first of all

    Make some "cuts" on your's spritefont. If you are using only 1,2,3,4,5,6,7,8,9,0 for timer remove ale other chars from it.

    Second

    Change the size of your's spritefont don't use any 1024x1024 take 256x256 or smaller if it is possible in that way You will save lots of RAM and CPU perf.

    In my case it helped.

  • Lordshiva1948 , really, what's the point of posting the link to hundreds of demo projects without any description/index?

    What exactly people are supposed to do with it?

    If there is a project that's relevant to this post, you should give the direct link to that file. Otherwise what you are doing is basically spamming.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Dop2000 if you check the link you will find TIMER how to use DT

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)