Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Closest Spline & control points loop in both directions
#1
Hi, 
first of all - thanks for this amazing asset. I really enjoying it so far. I've got a couple of (probably silly) questions. I've searched the forums a bit, and dig into the API documentation, but haven't found the answers. So here I am:

1) what is the best way to get the closest spline from the world position? 
2) what is the best way to get the closest point on a spline from the given word position?
Reply
#2
Hi
Thank you
Via the API, use the CurvySpline.GetNearestPointTf method. It is used in an example scene which name I forgot, something like "closest point"
If and when you feel like it, please leave a review for the asset, that helps a lot.
Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
Yeah, I figured it out while my post was waiting for approval. Thanks anyway! Besides - I assume that there is no built-in method that allows us to find closest point on spline closest to the world position? I mean - it is okay, I can do it myself. Just wanted to be sure if I am missing something obvious.

As for the review - I'll surely write one, but after I get a little bit more familiar with.
Reply
#4
That method do exist. Here is how to use it
CurvySpline.GetNearestPointTf(inputPosition, out outputPosition, Space.World)
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#5
(01-20-2021, 07:37 PM)_Aka_ Wrote: That method do exist. Here is how to use it
CurvySpline.GetNearestPointTf(inputPosition, out outputPosition, Space.World)


Yeah, I know that, but I meant something different. Given only a Vector3 world position - is it possible to find the closest CurvySpline from it? Or do I have to manually get all the splines and find the closest point on each and then select the one with the smallest distance (from the input world position to the closest point)?
Reply
#6
Sorry, my bad, I didn't understood you correctly.
You will indeed have to do your test in a loop and test all the splines. If performance is an issue, then consider testing against the bounding box of splines as an elimination step (CurvySpline.Bounds)
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#7
Since it is still a basic question I'll reply here not to create another thread.

Basically, I want to get the closest control point for a given world position and then loop all the control points of a spline (in both directions). My problems start when the world transform is exactly on one of the control points.

Some snippets:
Code:
var nearestPointTf = spline.GetNearestPointTF(transform.position, out var output, Space.World);
var nearestSegment = spline.TFToSegment(nearestPointTf);

and then I can call
Code:
var nextSegment =  spline.GetNextSegment(nearestSegment);


to get the nextSegment. The problem is when the nearestPointTF is equal to 1 and I have a spline with loop enabled. The method won't return me the correct next segment (I mean correct for my understanding).

The situation is even more complicated when I want to traverse it in reverse.

Are there any samples that I may be missing? Or maybe I am mixing some of the terminology and it leads me to weird situations?
Reply
#8
Hi,
1- Thanks for your consideration, but feel free to create as many threads as needed. It's better since it makes your problem easily searchable by other users
2- You don't need to compute nearestSegment, the value is already given in the variable you named output
3- Try using GetNextControlPoint instead of GetNextSegment. If the result is still not what you expect, please explain to me with an example what you expect vs what you get
Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#9
Please look at the screenshot below. I cannot understand why is this happening. 
I am getting the nearest segment, and then I try to get next and previous control point. Next one is represented by green sphere, and previous is yellow sphere. 

Notice what is happening when input world position is exactly at first CP position (CP0000).

[Image: IinsYhT.gif]

Code:
public override void DrawGizmos () {

            if (GizmoContext.InSelection(this))
            {
                var nearestPointTf = spline.GetNearestPointTF(transform.position, out var output, out var nearestSegment, out var nearestPointLocalF, 0, -1, Space.World);
                Draw.WireSphere(output, 0.5f, Color.black);
           
                var nextControlPoint = spline.GetNextControlPoint(nearestSegment);
                Draw.WireSphere(nextControlPoint.transform.position, 0.4f, Color.green);
           
                var prevControlPoint = spline.GetPreviousControlPoint(nearestSegment);
                Draw.WireSphere(prevControlPoint.transform.position, 0.4f, Color.yellow);   
            }
        }

Basically i would like to traverse all the controlpoints in both directions starting from any given world position.

Also, nearestSegment is also giving me inconsistent results. 

black sphere: nearest point on a spline
blue sphere: nearest segment
yellow sphere: previous control point from closest segment
green shpehe: next control point from closest segment
transform handle: world point that I am using to calculate everything


[Image: 2tNXnQI.png]
[Image: Ymko2eR.png]
[Image: NWhHUKV.png]
Reply
#10
Hi
I will test your code later today or tomorrow, but from a first look I think that by using "output" instead of "nearestSegment" when getting the next and previous control point.
Let me know if it worked
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Changing spline connection in inspector causes splines to revert to defaults lacota 3 6 03-18-2024, 07:55 PM
Last Post: _Aka_
  GO can't fit end of the spline GameDeveloperek4123 3 13 03-04-2024, 11:06 AM
Last Post: _Aka_
  Avoiding runtime GC allocations on control point position change Ell223 8 18 02-24-2024, 10:43 AM
Last Post: _Aka_
  Keeping a fixed spline length jh092 3 16 02-21-2024, 06:25 AM
Last Post: Primrose44

Forum Jump: