Hello, I want an object to move along a path, and that is working fine. Now I want to change the velocity of the object depending on where it is in the path. My idea was that during creating the curve for the path, I use path.curve.add_point(point). And while doing that in a loop for all points needed, I could also fill an array of the same length than the curve (item-wise) with the correct speed for each curve segment/point. But to execute that, I would while moving the object check in which curve-point I am, and with that knowledge of the index, I could query the corresponding velocity I need right now.

So my question is: do you know of smart ways how to do this? Of course through usage of the progress function I know where I am length wise on the path/curve, but is there a clever trick, so I wouldn't need to do the math myself about what point would correspond to that length? The curve of the path has a get_closest_point() function, but that would give me the coordinates of the point, not the index. From there I could use a Vector2:index dictionary I guess, but that feels overkill.

Thanks for reading and let me know if you have a good idea 🙂

EDIT: The solution I eventually implemented: So actually the get_closest_point() function of the curve does not give the closest "anchor"-point, but instead the closest point from the fully interpolated curve. So for the purpose of using those points as dictionary keys, that was useless... What I did now instead: While creating the curve, I populated two other arrays, one with the speeds for each curve point, and the second one with the respective curve length for that point. After having done all points, I divide each length point by the curves total length, and now have two arrays with a percentage progress value, and the corresponding speed for that curve section. Then later on in the movement I know I start at zero, so I assign that speed. Then going forward I check each _process if the current length is still the closest to the actual progress value. If not, I move the index one further. I trust that the object never moves too fast to skip full indices, and if it would, well then there is some inaccuracy. If needed, I could make the algorithm more robust of course. Anyway, based on that index I can now look up the speed from the speed array and use that in the progress function. It works well, but feels a bit "manual". I wished the curve or the pathfollow2d had more powerful functions to know which anchor-point one is at.

  • Toxe replied to this.

    M1sterKn1ster Hmmm, maybe you could add a second curve that is basically a duplicate with the same number of points at the same distances but this curve contains the speed values.

      This gets the position of the ith point of a Path2D:

      var path: Path2D = ...
      var i: int = ...
      var point: Vector2 = path.to_global(path.curve.get_point_position(i))

      You could use that to get the positions of all the points, and compare their positions with the result of get_closest_point() to determine the index.

        DaveTheCoder Thanks! I haven't looked at using the get_point_position() function. While moving the object later on, I could keep track of which index I am at and could use this function in combination with get_closest_point() to see if I'm still referring to the correct index!