Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | newold |
I’m trying to create my own 2D curve editor, and I’m stuck in the next step (see image):
I Know the position of green point (i get it with function curve.get_closest_point(_mouse_pos) from the Curve2D) and I need to know between which points it is (in this case is beetween point 1 and point 2) to add at the current Curve2D at this position (i this case, i need add the green point at index = 1)
Can you help me? Thank you
A couple of days ago I asked this question:
Convert a vector2 position to an array index in Curve2D - Archive - Godot Forum
Is it the same thing that you mean?
estebanmolca | 2020-06-10 12:21
yes is the same. I had not seen your question.
I want get the index in which i need put the closest point return by the curve2D function (curve.getclosestpoint(to_point))
newold | 2020-06-10 12:38
I was about to propose as a feature in github a function that returns the id in the array. But for doubts and ignorance I did not. Now that we are 2 with the same problem it would be worth proposing the feature if there is no other solution for this.
estebanmolca | 2020-06-10 12:45
I have solved the problem myself, I put the solution here in case someone else was looking for it (Although I don’t know if this will be the best way):
curve = a valid Curve2D
- Get closest point in curve2D
var closest = curve.get_closest_point(at_position)
- get offset for the closest point
var offset = curve.get_closest_offset(closest)
- call function below to get right index
var index = get_curve_point_index_from_offset(offset)
func get_curve_point_index_from_offset(curve, offset): var curve_point_length = curve.get_point_count() if curve_point_length < 2: return curve_point_length for i in range(1, curve.get_point_count()): var current_point_offset = curve.get_closest_offset(curve.get_point_position(i)) if current_point_offset > offset: return i return curve_point_length
newold | 2020-06-10 15:12
Thanks newold, sometimes it just takes a little motivation …
estebanmolca | 2020-06-10 22:01