TimeDelta, part two.

This forum is currently in read-only mode.
From the Asset Store
Footsteps SFX Two contains 450 sounds of steps and jumps on different surfaces.
  • About a year ago i posted asking for help with TimeDelta. It took a while, but eventually you guys managed to get me on my way...

    Then i did some other stuff for a year, and now i am back, and i don't have a clue about TimeDelta again.

    Would anyone who knows how it works mind throwing together some practical examples?

    Perhaps a grenade like arc? That's what i am trying to do at the moment, but it would be really useful to have a good selection of practical implementation examples.

    How to use Timedelta to count up/down private variables

    How to use Timedelta to move objects correctly etc

    Cheers for any help,

    Steven

  • There is not much to TimeDelta. It isn't really complicated.

    The game time advances in ticks. A tick is a slot, giving you the chance to compute things (using ACE).

    The duration of a tick is of a variable time, because sometimes there is much to do for Construct, sometimes not.

    TimeDelta is the duration of one tick. If you add all TimeDeltas from every tick, the all sum up to 1 per second. So TimeDelta is a fraction of a second.

    To alter things over time, you just use the following formula in every tick:

    AmountPerTick = TimeDelta * DesiredAmountPerSecond

    To move a sprite 75 pixels per second you'd use TimeDelta * 75

    To turn a sprite 87� per second you'd use TimeDelta * 87

    To represent the evolving cast time of a spell that lasts 2 seconds (2 seconds being 100%) you'd use TimeDelta * 50

    and so on

  • Say i have :

    (Always - Add 1 to "Move")

    The game is running at 1000fps, so that after a second "Move" would be 1000 right?

    So i would need to "Timedelta" it.

    (Always - Add 1*Timedelta to "Move")

    So here, it is adding 1/1000 every 1/1000 of a second, so it would equal 1?

    And now i want something to move based on this value

    (Always - Set position of "Object" to "Object.X" + "Object value('Move')")

    Do i need to add Timedelta into this line?

    I have something similar to this in my current project, and depending on the frame rate it runs completely differently. I think the main issue i am having is understanding how and where to use Timedelta, not what it is.

    "To represent the evolving cast time of a spell that lasts 2 seconds (2 seconds being 100%) you'd use TimeDelta * 50"

    I only just realised that it is being multiplied by a % in this line, I get the concept, but i don't know how you would actually create this situation.

  • Say i have :

    (Always - Add 1 to "Move")

    The game is running at 1000fps, so that after a second "Move" would be 1000 right?

    Correct.

    So i would need to "Timedelta" it.

    (Always - Add 1*Timedelta to "Move")

    So here, it is adding 1/1000 every 1/1000 of a second, so it would equal 1?

    Yes, if Construct can work reliably in only one millisecond

    And now i want something to move based on this value

    (Always - Set position of "Object" to "Object.X" + "Object value('Move')")

    Do i need to add Timedelta into this line?

    It depends on what you want to achieve. If you want the object to move 1 pixel per second, then you are doing it wrong. The correct line for this would be:

    Always - Set position of "Object" to "Object.X" + TimeDelta

    This way, the object is moved one pixel per second, no matter what the current frame rate is.

    Let us make an easier example to see, why your approach is going wrong.

    TimeDelta always equals 10

    object.x is 0 at start

    We want object.x to move by 10 every tick

    First tick

    You are adding 10 to move. move = 10

    a) You are adding move to object.x. object.x = 10

    b) I am adding TimeDelta to object.x. object.x = 10

    Second tick

    You are adding 10 to move. move = 20

    a) You are adding move to object.x. object.x = 30

    b) I am adding TimeDelta to object.x. object.x = 20

    Third tick

    You are adding 10 to move. move = 30

    a) You are adding move to object.x. object.x = 60

    b) I am adding TimeDelta to object.x. object.x = 30

    See?

    You could instead of adding to .x just replace .x with 'move'. That's the equivalent to my line of code. So:

    Always - Set position of "Object" to "Object.X" + TimeDelta

    and

    Always - Set position of "Object" to "Object value('Move')"

    are the same in your example

    "To represent the evolving cast time of a spell that lasts 2 seconds (2 seconds being 100%) you'd use TimeDelta * 50"

    I only just realised that it is being multiplied by a % in this line, I get the concept, but i don't know how you would actually create this situation.

    I've setup a simple cap showing this in action. That's the most easiest way I think.

    Example (done with .84)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Have you read the TimeDelta article in my signature? It should cover it as you want. The idea is that objects move at a constant speed in real-world time, independent of the framerate, rather than speeding up if the framerate is higher. So:

    Set X to .X + 1

    Every second, this will move the object 1000 pixels sideways at 1000fps and 10 pixels sideways at 10fps. A difference of 100x in speed depending on the framerate!

    Set X to .X + 1 * TimeDelta

    This will move the object precisely one pixel every second, no matter the framerate. Obviously multiplying by 1 is redundant, but it makes it clear that it's one pixel per second.

    If that number is a private variable or some such, you still need the TimeDelta! It makes no difference if that number is hard-coded or a variable, you'd still need to do it like this:

    Set X to .X + Object('Speed') * TimeDelta

  • TimeDelta is the amount of time that has passed during the previous loop of the game engine.

    • For one second in game time, the sum of the TimeDeltas from all of the frames in that second will equal 1.
    • If your game was running at a consistent 50fps, TimeDelta for one of those frames would be 0.02 (1/50).
    • Anything time based (movement, rotation, etc.) should be set to the amount you want it to change per second. For example, 200 pixels per second would be a nice number for movement speed.
    • When you multiply that by TimeDelta, you end up with the correct change per frame, regardless of the actual frame rate.

    So, at 50fps TimeDelta would be 0.02, each frame the object would move 200pixels*0.02 = 4 pixels. However, if the game was running at 100fps then TimeDelta would be 0.01 (1s/100f); 200*0.01 = 2 pixels. This table shows how TimeDelta keeps things like movement consistent:

    fps    TD    Speed (pps)    Distance per frame    Distance per second
    25    0.04      200         200 * 0.04 = 8        8*25 = 200
    50    0.02      200         200 * 0.02 = 4        4*50 = 200
    100   0.01      200         200 * 0.01 = 2        2*100 = 200[/code:133jkltd]
    
    Frame rates won't always be consistent, but TimeDelta will always represent the relative time taken by each frame, so the results will always be consistent. 
    
    Just remember- if it is changing over time, use the amount you want it to change in one second and multiply it by TimeDelta.
  • If that number is a private variable or some such, you still need the TimeDelta! It makes no difference if that number is hard-coded or a variable, you'd still need to do it like this:

    Set X to .X + Object('Speed') * TimeDelta

    Don't confuse him. In his example, that was not the problem. TimeDelta was already taken account of and stored within the variable ("Add 1*TimeDelta to 'Move'"). If he would now multiply it with TimeDelta again, he would get wrong results...

  • Ooh OK. Well, there's several explanations in this thread now

  • Ashley - Cheers for stopping by again I hadn't looked at the Timedelta article recently, whether or not it has been updated in the last year, or i understand things a little better, it does seems better this time around.

    However,

    > If that number is a private variable or some such, you still need the TimeDelta! It makes no difference if that number is hard-coded or a variable, you'd still need to do it like this:

    >

    > Set X to .X + Object('Speed') * TimeDelta

    >

    Don't confuse him. In his example, that was not the problem. TimeDelta was already taken account of and stored within the variable ("Add 1*TimeDelta to 'Move'"). If he would now multiply it with TimeDelta again, he would get wrong results...

    + Every Tick

    Add 500 * TimeDelta to 'Y Speed' (the acceleration)

    Set Y to .Y + 'Y Speed' * TimeDelta (the speed)

    That there is from the Timedelta article, is it not multiplying by Timedelta twice?

    Thanks chaps for the detailed break down of what timedelta is.

    And Tulamide, I downloaded the example, and damn, my 'code' looks disgusting now. Aside from the fact you make timedelta work right, it is so tidy and effective. I am going to play around with Timedelta in a fresh cap(I think i will start my project over again) and will try to incorporate the tidiness alongside the timedeltaness.

    Will post my attempts.

    http://dl.dropbox.com/u/1487524/TimeDelta/TimeDelta.cap

    There is something horribly wrong in my process.

  • + Every Tick

    > Add 500 * TimeDelta to 'Y Speed' (the acceleration)

    > Set Y to .Y + 'Y Speed' * TimeDelta (the speed)

    That there is from the Timedelta article, is it not multiplying by Timedelta twice?

    We are talking about different approaches here. In your example it was all about a constant movement, whereas the above example generates a variable (accelerating) movement. The latter is what I wanted to show with the simplified comparison of your approach and mine a few posts above.

    Remember the formula?

    AmountPerTick = TimeDelta * DesiredAmountPerSecond

    If DesiredAmountPerSecond is a constant value you only apply TimeDelta once. If you want it to change over time, you force another TimeDelta calculation. Let's say, DesiredAmountPerSecond shall raise by a constant value per second, then the formula is:

    DesiredAmountPerSecond = DesiredAmountPerSecond + TimeDelta * DesiredRaisePerSecond

    putting this into the first formula:

    AmountPerTick = TimeDelta * (DesiredAmountPerSecond + TimeDelta * DesiredRaisePerSecond)

    This can be stacked endlessly. Maybe DesiredRaisePerSecond shall lower over time. That would be:

    DesiredRaisePerSecond = DesiredRaisePerSecond - TimeDelta * DesiredLoweringPerSecond

    putting this into the first formula:

    AmountPerTick = TimeDelta * (DesiredAmountPerSecond + TimeDelta * (DesiredRaisePerSecond - TimeDelta * DesiredLoweringPerSecond))

    There are easier ways for acceleration or deceleration or smoothing, etc., this is just to show the "straight forwardness" of using TimeDelta

  • I see where i steered you wrong, Tulamide. I am trying to make a variable movement. I simplified my example so i could understand it and well, threw off all the help right from the start.

    I have constant movement down. It is very simple and works great. Variable is not working, but i will study your post and try to amend my way of doing things.

    Edit-I am thinking that i am not using large enough numbers for Timedelta to work correctly/visibly in my current thinking.

  • I've done another cap, showing three different movements. Maybe it is of help. Covers only the basics of course, there are way to many possibilities to get sprites moved

    Download here (.84 used)

  • Thanks for the example Tulamide, It's all over my head though:P I will hold onto it. Perhaps one day in the future it will be less foreign to me, but for now all your help has gotten me to a point where i can glodge together what i need.

    Cheers to everyone

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