Legacy GM How to use delta_time

D

Drago Supremo

Guest
Hi guys,
i'm trying to understand how to use delta_time but checking on the docs i can't understand it completely.

According to the manual for example to adjust the speed of an object you should do:
Code:
speed = spd * (ot - delta_time);
ot = delta_time;
Am i right?

But it seems to me like something is missing. For example in and ideal case, where ot and delta_time are the same value (because there was no lag) ot - delta_time would be 0, so the speed would be 0 but it doesn't make sense.
Or if the difference is 5 microseconds and spd = 3, the speed would be 15, isn't that too much?
 

Smiechu

Member
The example in manual is wrong.

There is a free extension on the markerplace - delta timer... it comes with all the handy things, so you don't need to reinvent the wheel.

Generally you need to divide the target frame time throug delta time (both must have the same unit) to get the ratio.
The extension I mentioned has additionally a threshold implemented which will turn of the dt when the frame rate drops to low.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
That looks wrong to me... @Nocturne?
Ummm, yeah.... I have a bug in about that to get fixed. Must've been drunk the day I wrote it!

Typical delta_time use would be more like this:

Code:
var dt = delta_time / 1000000; //Convert delta time to seconds
vspd += grav * dt; //Update vertical speed with gravity (taking delta time into account)
y += vspd * dt; //Update position (using delta time).
 
D

Drago Supremo

Guest
Ummm, yeah.... I have a bug in about that to get fixed. Must've been drunk the day I wrote it!

Typical delta_time use would be more like this:

Code:
var dt = delta_time / 1000000; //Convert delta time to seconds
vspd += grav * dt; //Update vertical speed with gravity (taking delta time into account)
y += vspd * dt; //Update position (using delta time).
Oh ok but by this way the movement in each step will be different from the starting value right? I mean, if grav = 1 instead of adding 1px to the vspeed each step It will depend on delta_time, so i will have to consider that while chosing the movement Speed right?

Edit: also isn't the dt in y += vspd useless since vspd is already based on dt?
Many thanks
 
Last edited by a moderator:
B

bilouw

Guest
Oh ok but by this way the movement in each step will be different from the starting value right? I mean, if grav = 1 instead of adding 1px to the vspeed each step It will depend on delta_time, so i will have to consider that while chosing the movement Speed right?

Edit: also isn't the dt in y += vspd useless since vspd is already based on dt?
Many thanks
I have exactly the same question! I made delta time work great with horizontal movement, but not with vertical movement. I did exactly the code you provide but jump are inconstant and gravity is weird. Why do we need to apply dt 2 times (one for gravity and another for the position). I made this :

if (keyboard_check_pressed(vk_up)) vsp = jumpheight * dt;

vsp += gravity_ * dt;

y += vsp;

Very simple, but it doesn't work. This delta time make me feel i'm a totally beginner ...

Please give me answers! Thanks.
 

Smiechu

Member
I have exactly the same question! I made delta time work great with horizontal movement, but not with vertical movement. I did exactly the code you provide but jump are inconstant and gravity is weird. Why do we need to apply dt 2 times (one for gravity and another for the position). I made this :

if (keyboard_check_pressed(vk_up)) vsp = jumpheight * dt;

vsp += gravity_ * dt;

y += vsp;

Very simple, but it doesn't work. This delta time make me feel i'm a totally beginner ...

Please give me answers! Thanks.
The current speed should be also multiplied by the delta time.

y += vsp * dt;
 
B

bilouw

Guest
Ok so i dit it, thanks for the tip! But i did it in a sample project, simple collision system (place_meeting). But if i want to integrate this in my project that use tile collision, i'm in trouble! Here is the problem :

Tile collision system need to works with integer values. before, i was doing somethings like this :

Code:
tilemap_get_at_pixel(tilemap, bbox_left, bbox_side_+vsp);
...
y += vsp * dt;
Where vsp is an integer value with a fraction system to round and add the fraction next frame. But, since my vsp is now pixel per second, bbox_side+vsp is not good anymore, i need to do :

Code:
tilemap_get_at_pixel(tilemap, bbox_left, bbox_side_+(vsp * global.dt));
to check correctly where i'm going to be. But this operation give a fraction value, and if i round that give me 0 (not good).
So i tried to round if vsp > 1 but it's not a good idea, the values need to be precise.
I found a trick like this :

Code:
tilemap_get_at_pixel(tilemap, bbox_left, bbox_side_+2);
But it's not good because the check is not anymore relative to my vsp. Also, it give me bug and my character go through platform sometimes.
I'm really stuck with this delta_time system ... i start to believe that i will not use dt but it's frustrate me after all this try. I'm so close ...

Do you have an idea, or do you already had this problem?

Thanks.
 
Top