- Home /
Need to find a point in a line using a percentage and a Vector3 list.
Hello!
I need some advice or direction on how to take on a problem I'm facing.
I have a Vector3 list. These Vectors resemble a line (Visualised by a Line Renderer). Image example:
This line is created using the Vector3 list which is created by "drawing" with the mouse. I have the length of the line. Since the length varies, it won't be a fixed value. What I want to accomplish, is to use a percentage (of the length of the line), to find a Vector3 point in this line.
Example:
In the picture above, say I would like to get the point at 20% This is the point where I'd like to get the Vector3 of:
(Not sure if this the exact point, but I hope you get the idea)
I have no idea how to do this, so could you please help me?
I have googled around using percentages ect. but those were for Vector's between 2 points; this is a whole list of Vectors. I couldn't find anything about that.
Friendly regards.
If you know the length of the whole list, L, and the fraction of that length, P, then the target length, from the start to your desired output point, is their product, L * P.
(If P > 1 or P < 0, your output point is an endpoint.)
For each point in your list, add the distance from there to the next point, until you exceed the target length.
Then your output point is the last point you ended at, offset by a vector with direction from there to the previous point and magnitude equal to the difference between your accumulated length and the target length.
pseudocode:
if ( p < 0 ) return list[0] ;
else if ( p > 1 ) return list[ list.length-1 ] ;
else {
targetLen = totalLen * p ;
traveledLen = 0 ;
for ( i=0 ; traveledLen<targetLen ; i+=1 ) {
start = list[i] ;
end = list[i+1] ;
traveledLen += distance( start, end ) ;
}
backtrack = new Vector3( direction:start-end, magnitude:traveledLen-targetLen ) ;
return end + backtrack ;
}
Just a quick note, this seems pretty solid. I am not able to implement it until tomorrow, so I will come back to this! Thank you for your answer though, it definetly helped me already!
This together with the other answer gave me the direction I needed, so I want to thank you as well! If you posted your answer as an answer, and not a comment, I would also select it as an accepted answer. Thank you again!
Answer by Scribe · Jan 09, 2015 at 04:55 PM
This is untested but should work, note that the length of the curve supplied is obviously not just the last point minus the first point!
Vector3 FindPoint(Vector3[] vectors /* your vector array */, float length /* length of curve */, float p /* percentage along the line 0 to 1 */){
if(vectors.Length < 1){
return Vector3.zero; // if the list is empty
}else if(vectors.Length < 2){
return vectors[0]; //if there is only one point in the list
}
float dist = length*Mathf.Clamp(p, 0, 1);
Vector3 pos = vectors[0];
for(int i = 0; i < vectors.Length-1; i++){
Vector3 v0 = vectors[i];
Vector3 v1 = vectors[i+1];
float this_dist = (v1-v0).magnitude;
if(this_dist < dist){
dist -= this_dist; //if the remaining distance is more than the distance between these vectors then minus the current distance from the remaining and go to the next vector
continue;
}
return Vector3.Lerp(v0, v1, dist/this_dist); //if the distance between these vectors is more or equal to the remaining distance then find how far along the gap it is and return
}
return vectors[vectors.Length-1];
}
Hope that helps, let me know if it produces any errors :)
Scribe
Thanks you for your answer! Looks like this should work as well, so thank you! I'm unable to test this until tomorrow, but I'll get back to this then!
While I couldn't implement it out of the blue, this gave me the right direction (as well as the other comment). So thank you very much for your time and effort! You've helped me a lot =]
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to Sync List 1 Answer
How do you generate a line between objects that are only instantiated upon game Start ? 1 Answer
slowly rotate a object *need quick fix* 0 Answers
How to combine Vector3 arrays? 3 Answers