GM:S 1.4 Delta Time help

Discussion in 'Programming' started by yuki, Sep 5, 2017.

  1. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    Okay so although i've gotten pretty far in my game, im still roughly in the alpha stages, meaning right now may be the optimal point in setting up delta time. I have a few enemies and a player but most of them use the same style to move. I recently downloaded this to check out how it works

    https://marketplace.yoyogames.com/assets/1149/steady-delta-time

    and i get the gist of what he's doing and what not. But there are some questions before i make the leap in converting my game into delta time. My game is built around the room speed of 60 (the ideal fps)

    So i know that Alarms and other built in features are not going to work, luckily i have very minimal alarms to begin with. As for the Sprites, what ive been doing so far is duplicating my frames to get precise animations sometimes it racks in around 40 frames. does that mean Image_speed wont work anymore?

    in the example he wrote
    Code:
    image_speed = imagesPerSecond * global.dt_steady;
    i noticed that he put the imagepersecond variable to 10 for animations with non duplicated sprites so when i put in my sprites to test it didnt work. Instead I put 60 and that pretty much set it to the correct animation.

    Another thing i wanted to ask is how would i convert the custom gravity and speed engine to work with delta timing because i cant seem to figure it out. Would i multiply everything by 60*delta time?
    Code:
    ////Step Event
    //Set up gravity
    if !place_meeting(x,y+1,Wall)
    {
    if pause=false
    {
    //Vspeed + the gravity
    vsp+=grav
    }
    
    }
    ///Update Y
    cy += vsp;
    vspNew = floor(cy);
    repeat(abs(vspNew))
    {
        if (!place_meeting(x, y + sign(vspNew), Wall))
            y += sign(vspNew)
        else
        {
            vsp = 0;
            break;
        }
    }
    
    ///Pressed Jump Key
    if (place_meeting(x, y+1, Wall))
    {
        if (keyboard_check_pressed(vk_up))
        {
            vsp=-5.5
            sprite_index=jump;image_index=0
       
        }
    }
    
    
    

    the reason why i say * by 60 for everything is because when i created a custom timer called time
    Code:
    time-=round(60*global.dt_steady)
    i had to do this for the desired effect of subtracting by 1 "per step"


    I also noticed that the alternate synchronization method keeps the game at a solid 60 fps, if i check this on does that mean i won't have to do Delta timing?
     
    Last edited: Sep 5, 2017
  2. kamiyasi

    kamiyasi Member

    Joined:
    Jul 22, 2016
    Posts:
    236
    The way delta timing works is a bit confusing at first, but once you understand how it works, it's actually really simple.
    Take a look at the following script.
    Code:
    var deltaspeed = max(1,(room_speed/1000000*delta_time));
    Now let's break down what this all means.
    Typically, the rate of steps per second in which your game runs will sync to your monitor's refresh rate which makes the room speed and the actual rate of steps per second match up. However, in certain cases such as particular hardware configurations, target platforms, or the use of v-sync, delta_time is needed instead. Multiplying your variables by the script above ensures that your game won't run too fast or too slow relative to how you'd expect it to given your chosen room speed.
    Now, the important thing to keep in mind is that you want to multiply every change in variable that increases or decreases at a set rate (usually in your step events) such as something like "acceleration += 5*deltaspeed" HOWEVER, you do not want to multiply using delta_time for events such as alarms because it's value is always fluctuating based on the difference in the frames per second your game is running.
    I prefer to make the script I posted above a macro because if you use it, you have to use it for pretty much everything.
     
  3. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    @kamiyasi i think i'll give ur method a shot, but does it also account for scaling? Because i do want to add some nice Time slow down for my game in the future. Also shouldnt the room speed that you placed it should just be put to 60 since that's what i want my game at and what i've been building it towards this whole time?

    taking in everything you said i should, i assume that the 1 in the front might be for scaling? and then probably modify it something like this

    Code:
    ///IN some Init obj thats persistent
    
    global var
    dscale=1
    deltsp = max(dscale,(room_speed/1000000*delta_time)),
    
    
    ////Step Event
    //Set up gravity
    if !place_meeting(x,y+1,Wall)
    {
    if pause=false
    {
    //Vspeed + the gravity
    vsp+=grav*deltsp
    }
    
    }
    ///Update Y
    cy += vsp*deltsp;
    vspNew = floor(cy);
    repeat(abs(vspNew))
    {
        if (!place_meeting(x, y + sign(vspNew)*deltsp, Wall))
            y += sign(vspNew)*deltsp
        else
        {
            vsp = 0;
            break;
        }
    }
    
    ///Pressed Jump Key
    if (place_meeting(x, y+1, Wall))
    {
        if (keyboard_check_pressed(vk_up))
        {
            vsp=-5.5
            sprite_index=jump;image_index=0
     
        }
    }
    
    
    
     
  4. kamiyasi

    kamiyasi Member

    Joined:
    Jul 22, 2016
    Posts:
    236
    The "max(1 , " is just so you're not telling your game to run at less than 1 frame per second! you don't want that. if you want to account for time slow, just change it to something like...
    Code:
    var deltaspeed = max(1,(room_speed/1000000*delta_time));
    var slowmospeed = 0.5;
    var playerspeed = (5*slowmospeed) * (deltaspeed);
    
    
     
  5. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    @kamiyasi
    Oh i see. That would've been pretty bad lol. Also can i just do this?

    Code:
    //Controller obj
    global var deltaspeed ,slowmospeed,deltreal ;
    slowmospeed= 0.5; //change accordingly to whatever we want
    
    //Controller Begin Step
    deltaspeed = max(1,(60/1000000*delta_time));
    global.deltreal = (slowmospeed) * (deltaspeed);
    
    ///in player event
    hsp = 5 * global.deltreal
    Edit; i just put it in what in what i wrote, if i leave room_speed in there nothing changes, which i was under the assumption that delta time was meant to make it look like it was still playing in 60 fps, if i swap out room_speed with 60 it becomes a bit better but now the difference is in the jump height...
     
    Last edited: Sep 6, 2017
  6. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    Another question is how do i check if delta time is working correctly??? For instance do i set the room fps of the room to 60 but if i want to test if the delta time is working do i just set up a room-speed change in game ?? Like lets say i want the f6 key to change the speed to 30 but i want the f5 to change it back to 60 .
     
  7. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    bump
     
  8. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    So first of all, you have to identify anything that is usually measure in somethings per second.
    Speed (Meters per second), acceleration (meters per second per second), etc.

    When doing anything with these values, you first have to multiply by delta_time.

    So instead of x += hspeed, you get x += hspees * delta_time.

    That easy.

    To test your delta timing, you could just add an object that sets the room speed to random values. See if your gamep lay stays the same. (Wo th delta timing, everything should be independent of room speed.)
    Advanced note: If you use stable integration, like verlet integration. You should get exactly the same behavior
     
  9. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    @Fel666 The thing is i have been doing that, i'm not sure if you've read the code itself or what i wrote above but right now i'm using the steady delta time stuff to help me along with it, the only thing i change in his code is
    Code:
    global.dt_steady = 60  * (dt*scale);
    but the issue right now is that the gravity is fluctuating depending on the room speed, i've been trying now for a long time to get it to work, ive been getting weird results whenever i change the code based on delta time. The lower the room speed the higher the jump for some reason... or sometimes the player gets clipped in the ground

    i did alot of digging and research and eventually led me to this
    velocity = velocity + gravity*delta_time/2
    position = position + velocity*delta_time
    velocity = velocity + gravity*delta_time/2
    i've tried implementing it but it just doesnt want to work. Can you take a look at my code see what i'm doing wrong? I'm really about to just give up on Delta time entirely, this seems like to much work to handle ... and in the end itll run like crap regardless on a bad computer, my next question is , does the alternative vyscn method option take care of this for me? because i noticed that even though i changed the room_speed to like 30 or 10 it still looks and feels like it plays at 60 fps

    Code:
    ///In Player Step event
    //Set up gravity
    if !place_meeting(x,y+1,Wall)
    {
    if pause=false
    {
    vsp+(grav)*(global.dt_steady/2)
    }
    
    }
    ///Pressed Jump Key
    if (place_meeting(x, y+1, Wall))
    {
    if (keyboard_check_pressed(vk_up))
        {
        vsp=-5.5
        sprite_index=jump;image_index=0
    
        }
    }
    
    // Handle sub-pixel movement(originally in end step)
    var hspNew,vspNew;
    
    cy += vsp;
    vspNew = round(cy);
    cy -= vspNew
    
    // Vertical
    repeat(abs(vspNew))
    {
        if (!place_meeting(x, y + sign(vspNew), Wall))
        {
        y += sign(vspNew)*global.dt_steady
        }
        else
        {
            vsp = 0;
            break;
        }
    }
    
    
    if !place_meeting(x,y+1,Wall)
    {
    if pause=false
    {
    vsp+(grav)*(global.dt_steady/2)
    }
    
    }
    
    

    i also found this now sure how to apply it to my game
    Code:
    vsp += grav * delta/2
    < vertical collision code here >
    y += vsp * delta
    vsp += grav * delta/2
     
    Last edited: Sep 6, 2017
  10. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    Look up simplified verlet. It should help with inconsistent gravity and delta timing.

    Also not sure the whole smooth delta time is worth it. Just use the raw delta time values
     
  11. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    Okay i understand but how about instead of sending me on some some wild goose chase to search for some complicated math, why not look at the code i provided ? Like seriously does anybody look at the code anymore or do we just say figure this out on your own nowadays... I already explained that many people have achieved this effect through the code i already mentioned, threes no reason to reinvent the wheel when its already been done. Sorry to sound like a jerk.
     
    Last edited: Sep 6, 2017
  12. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    I wrote my own animation script for delta time if you want to use it.
    Frame is basically used as the image_index like-> draw_sprite(sprPlayer,Frame,x,y)



    ///scrAnimate(start, end, rate)
    var framestart = argument0; // Starting frame of animation
    var frameend = argument1; // End frame of animation
    var framerate = argument2; // Time between animations

    animationTimer -= global.secondsPassed; // secondsPassed = delta_time / 1000000;

    // Set start animation
    if(Frame<framestart or Frame>frameend)
    {
    Frame=framestart;
    animationTimer=0;
    }

    // Change frame after time is up
    if(animationTimer < 0)
    {
    // Reset Timer
    animationTimer = framerate;

    if(Frame < frameend)
    {
    Frame += 1;
    }
    else
    {
    Frame = framestart;
    }
    }
    return Frame;
     
  13. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    Oh thanks i'll take a jab at it a little bit later, do you have anything for Gravity? Cause thats what im mostly stuck on right now
     
  14. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    I'm not very experienced with physics, but I'm pretty sure it's just this ->

    gravity = 128;
    gravitySpeed = gravity*(delta_time/1000000);
    vspd += lengthdir_y(gravitySpeed, 270);


    Irl gravity is 9.8 meters per second, but that likely won't feel right for your game. Just adjust the gravity value to whatever feels right to you. You'll also want to cap your delta-time because if there's any lag the player can potentially go through the floor or other objects.

    You can cap it with a modulus like this instead of an if statement
    global.secondsPassed = (delta_time/1000000) % 0.25;

    gravitySpeed = gravity*global.secondsPassed;
    vspd += lengthdir_y(gravitySpeed, 270);

    Anyways, let me know how that works out if you try it.

     
    Last edited: Sep 6, 2017
  15. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    lengthdir? why use trig here?
     
  16. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    So that he can change the direction of the gravity if he wants. I don't know what kind of game it is.
    If the gravity never changes direction, you can just add the gravitySpeed to your vspd.
     
    Last edited: Sep 6, 2017
  17. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    Okay so i just tried it out in my game this is how i did it
    Code:
    ///Controller Begin Step Event
    global.secondsPassed = (delta_time/1000000) % 0.25;
    
    //Player Create event
    gravity=0.25
    gravitySpeed = 0
    
    //Player Step Event
    gravitySpeed = gravity*global.secondsPassed;
    vspd += lengthdir_y(gravitySpeed, 270);
    when i put it in i noticed a few problems, 1 is that the players gravity is no longer incrementing by 0.25 speed but rather 0.01 the next thing is he flies off the screen
     
  18. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    Your gravity value is too low. The speed is in pixels per second when you use delta time. Try something like 128 for gravity instead of 0.25. 128 should mean at 60 fps that you'll move down 2.13 pixels per frame for 60 frames, being 128 pixels in 1 second.

    Character flys off the screen in which direction? make sure you don't have other things affecting the player's position like game makers physics.
     
    Last edited: Sep 6, 2017
  19. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    ohh i see. I just adjusted the values, by the character flies off screen he flies upwards offscreen. Btw i'm not using any of the physics stuff, and to answer ur previous comment i am working on a 2D platformer. So right now i adjusted the gravity to 15 and at 60fps its just alot closer to my original jump that i wanted. The problem now is that the slower the FPS the lower amount of height in his jump. So a good example would be lets say its at 10 fps, he just does a short hop, and as the fps increases he gets higher and higher
     
    Last edited: Sep 6, 2017
  20. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    That was because your gravity was only 1/4th a pixel per second. I'd have to see the code you use for your jump. Are you still using the room speed in your deltatime calculation somewhere? That might cause that behavior. If so, just remove that and use the secondsPassed variable you created instead (i.e. delta_time/1000000).
     
    Last edited: Sep 6, 2017
  21. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    I posted above lol. Which i think alot of people just sorta skipped over, thats the main issue is that the code i have, also accounts for sub pixel movement. I originally posted that code on the end step but it didnt seem to work to well so i just made up update in the step event.
     
  22. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    Btw, if you still need a timer I'd do something like this

    //Set time in seconds
    timer = 5.0; // 5 second timer

    if(timer > 0)
    timer -= global.secondsPassed;
    if(timer <= 0)
    //Do something
    Oh, okay. I'll look at the jump code real quick.
     
  23. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    I think the reason your jump height is changing when you change room speed is that you didn't multiply all your other values by delta time and are still using pixel speed, not pixels per second.

    anywhere you use pixel speed like this
    vspd = 5;

    needs to be changed to something like
    vspd = 256*global.secondsPassed;

    Because if you don't multiply by delta time and just use 5, 5 at 30 FPS = 150 pixels per second and 5 at 60 FPS = 300 pixels per second. Let me know if that fixes it.
     
    Last edited: Sep 6, 2017
  24. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    I just tried it and it doesn't seem to work still , causing the same problem as the last even if i increased it to 330 since by what ur telling me 5.5*60 =330,
    so it basically looked like this
    vsp=-330*global.secondsPassed;

    also i might be misunderstanding but where does the global.secondsPassed update? i put it in my controllers begin step event. Is that the correct place? I have a dummy project if you want to take a look at it, maybe it'll be easier to see if its directly in front of you
     
  25. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    I really dont understand. Delta timing should be easy:
    Code:
    var dt = delta_time / 1000000; //Convert delta time to seconds.
    
    vspd += grav * dt; //Update speed;
    
    y += vspd * dt; //update position.
    
    vspd and grav should be in pixels per second, and pixels per second per second respectively.

    If you want something a bit more stable, use this modified verlet integration:
    Code:
    var dt = delta_time / 1000000; //Convert delta time to seconds.
    y += vspd * dt + 0.5*grav*dt*dt; //update y pos
    vspd += grav * dt; //update velocity
    
    The second example gives very stable results when using constant gravity, but for most cases, the first example will do.

    remember, all you values should be in pixels per second. not pixels per step.
     
  26. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    I always like to create a "System" object where I define my enums and update my global delta time in its step event. This way you only have to calculate the delta time or secondsPassed once and can use it everywhere in your project.
     
  27. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    I also just noticed that.
    Dont use modulus like that. You will end up with incredibly weird results.
    cap using min()
    Code:
    var dt = min(delta_time/1000000, 0.25);
    
     
  28. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    Min() is a safer suggestion, but I don't think you'd get weird results with modulus unless someone was playing at like 5 fps because delta time should never be higher than 0.25 otherwise, except for maybe one frame which wouldn't really affect much and I think modulus is faster than min(), but I could be wrong.

    It's just a failsafe. I wrote a game engine in C++ with which I made a platformer to test it out and when you drag the window it stopped updating the game for a moment which caused the deltatime to spike and become a high number like 5+ for a frame which made the player teleport through the floor. That's the only reason I know about and suggest you cap it at all.
     
    Last edited: Sep 7, 2017
  29. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    use the right tool for the job.
    min caps a value to an upper limit.
    % gets the remainder of a division.

    If you need a remainder, use %.
    if you need to cap a value, use min.

    Dont get caught up in inefficiencies, thats how you end up with bad code.
     
  30. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    Won't argue with you there, just saying It's never broke anything I've made an "incredibly weird results" is quite an exaggeration.
     
  31. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    @Fel666
    if you had read the code, i don't just do y+=vsp
    i have another calculation behind it, (which im following an example) that takes care of subpixel movement which i am completely at a lost here as to where i would apply it and how
    please read and let me know what you think, which at this point i think it might be this thats screwing it up also ima be straight up honest i suck at math, so if you tell me to go read something off the internet chances are i wont get it unless i see it through code which the code u provided is a bit more helpful but i'm just curious as to what the 0.5 is supposed to be in this
    y += vspd * dt + 0.5*grav*dt*dt; //update y pos again
    if you'd like the dummy file i have it uploaded on drive and u can give it a shot to see what it pertains. its unmodified so it shows the ideal jump position id like to have when converting it into delta time

    Code:
    //Gravity
    if !place_meeting(x, y + 1,Wall )
    {
    vsp+=grav
    }
    
    ///Update Y
    var vspNew;
    cy += vsp;
    vspNew = floor(cy);
    cy -= vspNew
    
    repeat(abs(vspNew))
    {
        if (!place_meeting(x, y + sign(vspNew), Wall))
            y += sign(vspNew)
        else
        {
            vsp = 0;
            break;
        }
    }
     
    Last edited: Sep 7, 2017
  32. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,360
    Then just use your code (which at first glance seems correct to me), but with the method i outlined to update the speed and position.

    Also, the .5 comes out of verlet integration. Its a way of doing 2nd order integrals.
    If you know cqlculus you should understand the mateirql you see online.
    If not, ignore it. Just know that this formula gives you very stable integration.
    (Integration is what you use to calculate velocity and positions from acceleration)
     
  33. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    @Fel666 Okay So

    i just tested it out, and still it does not work, not only does it produce even negative results than before but i've tried both ways that you mentioned.
    when i used the code in the step event and removed my own code for the vspeed and what not and only had the basic place_meeting check your code prevents me from even walking left or right and moves the player at insanely low speeds.

    When i used my code integrated with yours he does a super jump then gets clipped in the floor sometimes when after landing. Also The problem with the change in height depending on the room speed is still there.

    This is my project file if you want to take a look at it, im about to just give up on delta time and just use the room speed

    https://drive.google.com/open?id=0B_UGYmCWO6PXdnFnU1NvTEVhNFk
     
  34. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    I wouldn't give up when you're this close. Sounds like my code worked fine, aside from your jump height changing when you changed the FPS, which is surely something small you're overlooking. Would look over your project, but I'm a little preoccupied with a game, map editor and game engine I'm working on.

    Your jump code is a little messy. I'd suggest watching a gamemaker platformer tutorial, copy their code, then apply the delta time to it. There's also a better way to handle your collision so you don't get stuck in the floor



    ///scrMove(hspd, vspd)
    var hspd = argument0;
    var vspd = argument1;

    if (place_meeting(x+hspd,y,objWall))
    {

    while(!place_meeting(x+sign(hspd),y,objWall))
    {

    x+=sign(hspd);
    }
    hspd=0;
    }
    if (place_meeting(x,y+vspd,objWall))
    {

    while(!place_meeting(x,y+sign(vspd),objWall))
    {

    y+=sign(vspd);
    }
    vspd=0;
    }

    y+=vspd;
    x+=hspd;
     
    Last edited: Sep 7, 2017
  35. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    I added delta time to an old platformer I made when learning GML a while back real quick and got it working. Having the same problem you are with room speed affecting the jump height, not sure what's happening. Don't want to spend too much time on it, but am looking it over.

    Update:
    added /(60/room_speed) in 3 places which fixed the height problem, but doesn't maintain speed. It's just a band-aid, not a solution.

    Here's my jump code if you want it:
    Code:
    //===========================================
    // Create
    //===========================================
    grav = 48/((60/room_speed));
    hsp = 0;
    vsp = 0;
    jumpspeed = 512/((60/room_speed));
    movespeed = 128;
    Dir = 0;
    Last_Dir = Dir;
    bFacingForward = true;
    bAttacking = false;
    animationTimer = 0;
    Frame = 0;
    
    //===========================================
    // Step
    //===========================================
    key_up = keyboard_check(ord("W"));
    key_down = keyboard_check(ord("S"));
    key_right = keyboard_check(ord("D"));
    key_left = keyboard_check(ord("A"));
    key_jump = keyboard_check_pressed(vk_space);
    bAttacking = keyboard_check_pressed(vk_left) && alarm[0]<=0;
    
    move = key_right - key_left;
    hsp = move * (movespeed*global.deltaTime);
    
    // Jump, if we're on the ground.
    if(place_meeting(x,y+1,obj_wall))
    {
        if(key_jump)
            audio_play_sound(sndJump,0,0);
        bFalling = false;
        vsp = key_jump * (-jumpspeed*global.deltaTime);
    }
    else
    {
        bFalling = true;
    }
     
    // Apply Gravity
    if(vsp < ((256/(60/room_speed))*global.deltaTime))
    {
        vsp += (grav*global.deltaTime);
    }
    else
    {
        vsp = 0;
    }
    
    // Horizontal Collision
    if(place_meeting(x+(hsp),y,obj_wall))
    {
        while(!place_meeting(x+(sign(hsp)),y,obj_wall))
        {
            x += sign(hsp);
        }
        hsp = 0;
    }
    
    x += hsp;
    
    // Vertical Collision
    if(place_meeting(x,y+vsp,obj_wall))
    {
        while(!place_meeting(x,y+sign(vsp),obj_wall))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    
    y += vsp;
    
     
    Last edited: Sep 7, 2017
  36. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    @Reign
    Thank you again for being incredibly patient with me i cant thank you enough... So i decided to test out what i had link in the beginning of the post, i had removed my repeat code for the vspeed because i just dont know where to apply delta time to it to make it work right, i figured that i'd try to simplify it a bit and maybe i can work my way up to applying it on my previous code. This is what i nailed it down to this.

    for jump speed to gravity i kept the same aka 5.5 for jump height and gravity=.25
    Code:
    if !place_meeting(x, y +sign(vsp),Wall )
    {
    vsp = vsp+grav*global.dt_steady/2
    y = y+vsp*global.dt_steady
    vsp = vsp+grav*global.dt_steady/2
    }
    else
    {
    vsp=0
    }
    
    and after testing it on 60 fps and then 10 fps it finally works in that they both get the same height maybe just like 1 or 2 pixels different. Now the main issue is the collision. For some odd reason when i use this code the collisions are all out of wack and the player clips through when at a low fps, even at a higher fps like 60 there is still an imperfection. At 10 FPS the clipping is extreme and at 60fps the clipping is roughly around 1-2 pixels. Another problem that arises is that for some odd reason the vspeed is always constant like it never changes back to 0 after hitting the ground it goes from 0 to .25 over and over again... I'm not sure if it was the repeat code or not, but really it shouldn't have mattered that much should it?
     
  37. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    @Reign Thank you Reign Again for the awesome help, i finally got it to work after following a tedious process, although the gravity engine is a bit different than what i had intended i think ima just keep it like this for now since it works. Now hopefully i can solve the hsp as well delta time seems like super tedious work though, since now the primary issue is it messing up with my ramping and other collisions...
     
    Last edited: Sep 7, 2017
  38. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    It's no problem, I'm happy to hear you got it working. What are your collision needs? If everything's square scrMove() should work fine and you can tweak it to handle 45-degree ramps.
     
  39. yuki

    yuki Member

    Joined:
    Sep 4, 2017
    Posts:
    82
    Well How mine works is just adding .5 until a max speed has been reached, so something like

    if keyboard_check(vk_left)
    {
    hsp+=0.5
    }

    I'm assuming that i'd have to apply delta time to that directly? and then x+=hsp*delta again?

    Update:
    @Reign I seem to have made it work, unlike with Vsp i dont really have an indicator of how accurate it is or how to test if it flows the same as if 60FPS.
    What i did is set up a timer and only increment by 60*delta_time or i guess i can call it 1 second.
    I set up a wall away at a considerable amount of distance, the timer only activates when a Key is held down.

    When i Try it at 60 FPS the timer reaches to 79 before stopping at the wall
    when i try it at 10-30FPS the time reaches to 78 before stopping at wall.

    I dont know if this is truly considered "Accurate" or does it really make that much difference at all?

    How would i test if the speed is truly the same everytime? Because Right now at 10FPS it seems that hes moving faster than what im thinking it is. I think that the ramps aren't really delta timed correctly or i might not have to do it at all? it works if i dont put any delta time to it like this
    Code:
     {y-=2}
    so again not really sure about the accuracy. is there a way to truly test if the hsp is working properly?
     
    Last edited: Sep 8, 2017
  40. Reign

    Reign Member

    Joined:
    Jul 14, 2017
    Posts:
    81
    Just use this for your collision, it's ready for delta time
    Code:
    ///scrMove(hspd, vspd)
    
    hspd = argument0;
    vspd = argument1;
    if (place_meeting(x+hspd,y,obj_wall))
    {
        while(!place_meeting(x+sign(hspd),y,obj_wall))
        {
            x+=sign(hspd);
        }
        hspd=0;
    }
    
    x+=hspd;
    
    if (place_meeting(x,y+vspd,obj_wall))
    {
        while(!place_meeting(x,y+sign(vspd),obj_wall))
        {
            y+=sign(vspd);
        }
        vspd=0;
    }
    
    y+=vspd;

    Off the top of my head, you could use a timer and show_debug_message() to output the distance between your last position and current position every second to check if the speed is consistent while moving.
     
  41. Christopher Rosa

    Christopher Rosa Member

    Joined:
    Dec 8, 2016
    Posts:
    33
    Does anyone know how to do frame checks and positions checks with delta time i'm doing something wrong.
    ```
    x +=5*FRAME_DELTA;

    if (floor(x) = 250)
    {
    SonicJumpAnimation = 3;
    }
    ```
    This doesn't work because my player will jump at the wrong x position it never jumps exactly at 250 which is what i want its always a few pixels off.
    Same goes for frame checks such as this.

    ```
    if (floor(SonicJumpAnimation) = 3)
    {
    DO SOMETHING;
    }
    ```

    this doesn't work for the same reason being that SonicJumpAnimation never executes the code when its exactly on frame 3. it will execute at different frames.

    I don't use image_index or image_speed i create my own variables and write the sprite code like this:
    ```
    draw_sprite_ext(spr_SonicJump,SonicJumpAnimation,x,y,1,1,0,c_white,1);
    ```
    that being said i initialized delta time so i understood i have to * any variable being incremented by delta time. So how the image_speed works is like this.

    ```
    SonicJumpAnimation += 0.3*FRAME_DELTA;
    ```
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice