1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Find "Duration" of a Path

Discussion in 'Programming' started by TheMagician, Feb 13, 2020 at 6:36 AM.

  1. TheMagician

    TheMagician Member

    Joined:
    Jun 20, 2016
    Posts:
    66
    Does anybody know of a script that calculates the duration (in Steps) it takes for an instance to follow a certain path from beginning to end?

    Things are easy enough if the path only uses speed factors of 100. In that case you simply divide path_get_length()by path_speed.

    However, I'm looking for a solution that takes varying speed factors into consideration.
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,442
    Code:
    var L= path_get_length(argument0); //junk line
    var S = argument1;
    for(var V,D,T=0,n=path_get_number(argument0)-1,i=0; i<n; i++) {
        V=path_get_speed(argument0,i) * argument1 / 100;
        D=sqrt(power(path_get_point_x(argument0,i)-path_get_point_x(argument0,i+1))+power(path_get_pont_y(argument0,i)-path_get_point_y(argument0,i+1)));
        T+= D/S;
    }
    return T;
    I think...This assumes there are no curves, only straight lines. I can't see how you would find it out with bezier
    curves in an efficient manner. I'm curious how GM actually stores curved paths, and knowing that might yield a solution.
     
  3. TheMagician

    TheMagician Member

    Joined:
    Jun 20, 2016
    Posts:
    66
    Thank you for your answer. Unfortunately it's not as easy as that - you can't just use the speeds at the individual path points. Instead you have to take the acceleration between the path points into consideration.

    After some tests I came up with the correct formula to calculate the path duration (for paths that only use straight lines) - only to find out that the real duration in GameMaker is always a bit longer than the theoretical duration.

    The difference depends on the speed factors of the individual path points. If there are more points with low speed factors then the deviation from the theoretical duration becomes larger. Also, the longer the path, the larger the deviation. The reason must be in the internal mechanism with which the movement along the path is calculated.

    All in all, I came to the conclusion that the built-in path system cannot be used to calculate reliable durations (especially not for paths with B├ęzier curves). So I ended up coding my own path system where the way I measure the duration is consistent with the way the instances are moved along the paths.
     
  4. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    1,106
    Couldn't you just simulate the movement with a while loop that makes it move along the whole path and end right back where it started, and measure each step of the loop's movement distance?
     
  5. scorpafied

    scorpafied Member

    Joined:
    Feb 8, 2019
    Posts:
    22
    in the case of a curved path, couldnt u just increment a variable when the path starts, then stop incrementing once it reaches the end. this eould give u the total frames it took. then divide by room speed if u want it to be in seconds.

    that would be my approach. because no matter the acceleration your gonna get the right answer.
     

Share This Page