- Edited
- Best Answerset by Catapult
Since I found too complicated - of course for me - to create separate curves the way you suggested (I'm very new to Godot and, for example, I have no idea of how to move my Sprite from a curve to another one), I tried investigating my latest idea, that is, tweening the progress_ratio
of my PathFollow2D
.
My latest question was: how could I get the progress_ratio
of the path in the position where I have a point?
The progress_ratio
is basically a float
representing the distance between the first and the last point, in the range 0.0 - 1.0
.
By searching the docs, I found the Path2D
curve has a get_baked_points
method: summarizing a lot, this method returns an array of all the points in the curve.
This led me to this function:
func get_progress_at_point(point_coords:Vector2) -> float:
var baked_points = curve.get_baked_points()
var total_pixel_length = 0.0
var point_index = baked_points.find(point_coords)
# add control for find==-1
for i in range(0, point_index):
var p2 = baked_points[i+1]
var p1 = baked_points[i]
# sqrt(x2-x1)^2+(y2-y1)^2 or replace with distance_to
var d = sqrt(pow(p2[0] - p1[0], 2) + pow(p2[1] - p1[1], 2))
total_pixel_length += d
return total_pixel_length
I can use the function this way to get the ratio:
var dist = get_progress_at_point(point_coords)
var baked_length = curve.get_baked_length()
var ratio = dist/baked_length
and finally pass the ratio to the Tween function