GML Keep platformer jump height consistent with different frame rates

Discussion in 'Programming' started by fedeali, Dec 6, 2018.

  1. fedeali

    fedeali Member

    Joined:
    Apr 11, 2017
    Posts:
    10
    Hi all,
    I want to achieve a platformer jump similar to that of Super Mario Bros. (keep pressed the jump button to jump higher) and I want it to be frame rate independent.
    At 30 FPS (i.e. Room Speed = 30) it works as I want. However, I noticed that, if frame rate is lower (e.g. 10 FPS) the jump height is much lower.
    The following is the code I'm currently using. What am I doing wrong? Thank you in advance for your help. :)

    Code:
    # dt Script
    /// dt()
    // Returns delta time in seconds. If FPS goes below a certain threshold
    // (e.g. below 5 FPS = above 0.2 sec) then this value is capped, to
    // avoid to skip collisions in case of too low FPS.
    
    var d = delta_time * 0.000001;
    if (d > MAX_DELTA_TIME)
        d = MAX_DELTA_TIME;
    return d;
    Code:
    # obj_hero Create Event
    // Tunable variables
    jump_force = 4000;
    jump_max_speed = 800;
    Gravity = 2000;
    
    // Internal variables
    _yspeed = 0;
    _is_jumping = false;
    Code:
    # obj_hero Step Event
    var dy = 0; // delta y-position for this frame
    var grounded = !place_free(x, y + 1);
    
    if (_yspeed != 0 && grounded)
    {
        _yspeed = 0;
    }
    
    if (mouse_check_button(mb_left) && grounded)
    {
        _is_jumping = true;
    }
    else if (_is_jumping && !mouse_check_button(mb_left))
    {
        _is_jumping = false;
    }
    
    if (_is_jumping)
    {
        _yspeed -= jump_force * dt();
        if (_yspeed < -jump_max_speed)
        {
            _is_jumping = false;
            _yspeed = -jump_max_speed;
        }
    }
    
    _yspeed += Gravity * dt();
    
    dy = _yspeed * dt();
    
    if (dy > 0 && !place_free(x, y + dy))
    {
        move_contact_solid(270, ceil(dy));
        _yspeed = 0;
    }
    else
    {
        y += dy;
    }
     

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