Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tangent incorrect.
#1
I am supporting a "Snap" to line feature. When doing this, I noticed a problem.

Here is a simple example of three control points A, B, C along the X-axis so all Y values are zero. Z values are also initially zero.
The Handle<In,Out> of all 3 cps are locally set along the X-axis also +-0.39f. This produces the expected two straight segments AB, and BC.

See attached image "Before Move CP3" (not sure how to place in in-line here without posting them to a URL?)

In the Unity Console window you can see the output of calling [CP0000].GetTangent(1).z and [CP0001].GetTangent(0).z These 'z' values are correct (full value is (1,0,0)). I tried both CPs to be sure I understood they should be the same.

Next I move the position of CP0003 only in Z to create a curve setting its AutoHandles = true (Does the order matter here: update position first, then auto-handle or vise-versa?

See attached image "After Move CP3"

 Note that in the Console window, the same GetTangent calls are now returning 0.008 for what the tangent now is at CP0002. This was unexpected.

Later, I was use this tangent to snap CP0003 (segment BC) back to a align with segment AB. Because the tangent of AB is no longer (1,0,0) but instead (1,0,0.008) the snap doesn't align.
It is an easy work around by calculating the tangent on my own (A-B).Normalize.

But is this a bug, or did I do something wrong setting up CP0003 for the curve, or do I just not understand GetTangent? In the later case, I assumed it would return a the (1,0,0) vector because the curve starts after CP0002 so at CP0002 it is still straight.

Please let me know how this should work as a full understanding is needed for other places where we may use GetTangent.


Attached Files Thumbnail(s)
       
Reply
#2
Hi

The issue you are highlighting does exist, it's not you misconfiguring/misusing the spline.
The issues comes from the fact that Spline.GetTangent does not calculate the real tangent using a mathematical formula for each spline type, but does a delta between two positions which are really close. That gives this unwanted behavior at the CPs.

A solution is to go to the following method in CurvySplineSegment
Code:
public Vector3 GetTangent(float localF, Vector3 position, Space space = Space.Self)
and add the following after its code contract
Code:
if (Spline.Interpolation == CurvyInterpolation.Bezier)
                return CurvySpline.BezierTangent(threadSafeLocalPosition.Addition(HandleOut),
                    threadSafeLocalPosition,
                    threadSafeNextCpLocalPosition,
                    threadSafeNextCpLocalPosition.Addition(cachedNextControlPoint.HandleIn),
                    localF).normalized;
I don't like this solution because it is not consistent, it fixes the problem for a type of splines, but not for all of them. Also, it might have side effects I am not thinking of right now.

A solution I prefer is to use the CP's handles, to get exactly the values of the handles (that define the tangents). Those are correct.

Sorry about this issue.
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
(06-27-2022, 08:06 PM)tairoark Wrote: not sure how to place in in-line here without posting them to a URL?


Go to Preview Post to have the full set of posting tools, then upload a picture, then click the Inset Into Post button
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#4
(06-27-2022, 08:06 PM)tairoark Wrote: Does the order matter here: update position first, then auto-handle or vise-versa

The order does not matter
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
Question Spline.length incorrect dazz777 3 367 10-07-2021, 10:23 AM
Last Post: _Aka_
  Auto Handle from CP tangent Josenildo 3 1,074 05-18-2021, 02:23 PM
Last Post: _Aka_
  Bezier tangent points constantly change coordinates valyard 3 6,460 02-26-2014, 11:56 AM
Last Post: Jake

Forum Jump: