1
\$\begingroup\$

I have a list of line segments that run through a track model and I am looking to achieve two things:

  • smooth the overall shape of the path
  • being able to evaluate a position by querying from 0.0 to 1.0

I have implemented Catmull-Rom spline using the TreeSpline class from Unity (it's not explicitly in the docs but you can see it here), it does work except for one thing: some places have overshoot:

enter image description here

Legend

  • purple: original line segments
  • white: result of the TreeSpline evaluation

After searching for a bit, it appears that Monotone cubic interpolation would be a solution to my problem. However, the formulas only show on how to do with 2D points, not with 3D points which I'm using.

Question:

How can I adapt the formulas in the Wikipedia article to 3D points instead ?

(I am also open to alternative approaches)

Edit:

Thanks to @Jay it's better now:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ There are plenty of free spline creation tools on the Asset Store, if you're interested. \$\endgroup\$ – Alex F Feb 24 at 1:57
  • \$\begingroup\$ In mathematics, "monotone" means always moving in the same direction. Eg, the example in the Wikipedia article is always decreasing in y. Does your track definitely have this property? \$\endgroup\$ – Jay Feb 24 at 2:11
  • \$\begingroup\$ Note that the Unity code you linked to is in the UnityEditor namespace, so it won’t be available at runtime, if that’s a problem for you. \$\endgroup\$ – Ed Marty Feb 24 at 2:11
  • \$\begingroup\$ @AlexF I am going to have a look, thanks ! \$\endgroup\$ – Aybe Feb 24 at 5:21
  • \$\begingroup\$ @EdMarty That's right, I didn't really pay attention to that to be honest! \$\endgroup\$ – Aybe Feb 24 at 5:28
1
\$\begingroup\$

Catmull-Rom splines are easy to use because they automatically generate a smooth line, or tangent, at each node. The tangent is computed from the position of neighbouring nodes which can cause weird issues in certain cases.

If the nodes are very far apart then the tangents will be very wide. If the nodes are close then the tangents will be short. If the nodes are far on one side and close on the other then the spline will have to loop around to remain smooth at both nodes.

This is kind of a long explanation of what you found in the image but it's how this kind of spline works.

Possible solutions are

  • To have even spacing between each of the spline points. There are tools to generate this if there are too many.

  • Use a different kind of spline. For example, a cubic b-spline is similar but allows you to manually adjust the tangents.

  • Add dummy nodes. If you can't fix the spacing issue completely, then try adding a few extra nodes that transition from a straight section to a corner.

\$\endgroup\$
  • \$\begingroup\$ This is great, I still need to polish it but since I can predictably know when these overshoots happens (before jumping a hole in the track), Lerping extra points at 0.25, 0.5 and 0.75 and lowering the spline tension to 0.25 it's almost perfect! Thanks :D \$\endgroup\$ – Aybe Feb 24 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.