Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a location above the spline
#1
I might be bad at explaining what exactly I'm looking to do but,

I'm looking for a way to get a position above(as in Y axis) a spline that has been drawn on XZ axis.


Easiest way to describe it might be to have the ray stop as it reaches the XZ coordinates along the spline and wish to get the world position straight above the spline at the cross-section.

   

Currently I'm using ScreenPointToRay to get the position when clicking, but I'm not sure on what would be the best method for what I'm looking to do.
 
I have thought of just looking through the clicked ray that is being cast and using the GetNearestPoint to get the other comparable point, then look for position when the angle from the spline point to the ray is 0, but this method might have various issues if I'm using too curvy splines and the nearest point isn't "below" the clicked point.


The fact I'm looking it to be above is somewhat irrelevant, as I just want the coordinates relative to the click, but due to colliders I believe there will not be a case where the ray from mouse click would hit the location below the spline before hitting anything else.
Reply
#2
Hi

There is no ready out of the box solution for this issue in Curvy Splines. You will have to code something. Here is what seems to me like a good logic to implement:
Simplify the problem by restricting it to the XZ plane (this means you will have to project the ray on that plane). Then find the nearest point on the spline from that ray. Then finally restore the Y coordinate to get the point on the real XYZ ray.

About finding the nearest point from the spline to the ray. There might be an optimized way to do it that I am not aware of, but an easy way would be to test multiple points on the ray and compute their distance from the spline. To do so use GetNearestPoint and compute the distance between the result point an the ray point. To reduce the number of points tested, if their high number is an issue, you can use some optimization strategy such as Binary Search.

I hope this helped
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
(07-11-2023, 09:30 AM)_Aka_ Wrote: Hi

There is no ready out of the box solution for this issue in Curvy Splines. You will have to code something. Here is what seems to me like a good logic to implement:
Simplify the problem by restricting it to the XZ plane (this means you will have to project the ray on that plane). Then find the nearest point on the spline from that ray. Then finally restore the Y coordinate to get the point on the real XYZ ray.

About finding the nearest point from the spline to the ray. There might be an optimized way to do it that I am not aware of, but an easy way would be to test multiple points on the ray and compute their distance from the spline. To do so use GetNearestPoint and compute the distance between the result point an the ray point. To reduce the number of points tested, if their high number is an issue, you can use some optimization strategy such as Binary Search.

I hope this helped
Have a nice day

Thank you for the quick answer, but after few tests I'm not sure if I got the hang of it.

Currently the fact that the hit location for the ray and the curviness of the spline cause some misalignment with the closest point.

Here's a more comprehensive image about the situation
   

 
I had a thought of using the "player"- or camera position as and offset of sorts to somehow get the correct location, but I don't think it would scale correctly when used on more straight splines.
Reply
#4
Hi
Here is the implementation of a GetNearestPointTF. Just add these two files to ToolBuddy\Assets\Curvy\Scripts\Splines
With this, you should be able to find the nearset point on a spline to a ray.


Attached Files
.cs   CurvySpline_Ray.cs (Size: 2.99 KB / Downloads: 1)
.cs   CurvySplineSegment_Ray.cs (Size: 2.79 KB / Downloads: 1)
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
And here is a zip with test scene and script. The two cubes define a ray's origin and direction, and the red cross is the nearest point. The script runs in playmode.


Attached Files
.zip   Test.zip (Size: 4.98 KB / Downloads: 0)
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#6
The script is not optimized, feel free to enhance it.
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
  Curvy Line Renderer for UI Spline? gekido 3 6 04-04-2024, 12:56 PM
Last Post: _Aka_
  Get position of all control points for a spline gekido 1 6 03-28-2024, 10:08 PM
Last Post: _Aka_
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 14 03-04-2024, 11:06 AM
Last Post: _Aka_

Forum Jump: