Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to detect bend in a spline or strait part of spline at position
#1
More precisely, hot to detect if current relative position is on bend and how strong the bend is?

If that makes sense?

I'm planning to implement bend leaning of my train cart that will have 2 parameters: speed and bend curvature.
Reply
#2
Hi
You can do that by retrieving the tangent of the spline at the position of the train cart, and compare it with the tangent at a point a little further (or behind) on the spline. Use the CurvySpline.GetTangent method.
Did this help?
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
Will try, thank You!
Reply
#4
(07-30-2023, 05:14 PM)velikizlivuk Wrote: Will try, thank You!

Hey guys, just to add to this discussion I tried something like the below to get a variable for the lean angle to use at that point but I suppose there must be a better way if anyone has any ideas as I'm not sure the tf amount I'm using will always represent the same distance. 

                Vector3 Cornernow = Spline.GetTangent(mTF);
                Vector3 Cornercomingup = Spline.GetTangent(mTF + 0.01f);

                Debug.Log("lean angle " + (Cornernow.x-Cornercomingup.x));
Reply
#5
Hi
TF is not proportional to distance. More about this here: https://www.youtube.com/watch?v=rP0zuAEoVJw
It would be best to use GetTangentByDistance
The way you compute the angle is invalid in a lot of cases. You can use Unity's Vector3.Angle, or a custom method if Unity's method is not precise enough.
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
(08-11-2023, 09:58 AM)_Aka_ Wrote: Hi
TF is not proportional to distance. More about this here: https://www.youtube.com/watch?v=rP0zuAEoVJw
It would be best to use GetTangentByDistance
The way you compute the angle is invalid in a lot of cases. You can use Unity's Vector3.Angle, or a custom method if Unity's method is not precise enough.


Thanks so much for the tip! I'm now using this which seems to be working much better: 

                float mTFDistance = Spline.TFToDistance(mTF, CurvyClamping.Clamp);

                Vector3 Cornernow = Spline.GetTangentByDistance(mTFDistance);
                Vector3 Cornercomingup = Spline.GetTangentByDistance(mTFDistance + 0.5f);

                float leanangle = Vector3.SignedAngle(Cornernow, Cornercomingup, Vector3.up);

                Debug.Log("lean angle " + leanangle);
Reply
#7
You are welcome.
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
#8
I used 2 variables for storing tangents during Update method

private Vector3 previousTangent=Vector3.zero;
private Vector3 currentTangent=Vector3.zero;

and in Update:
// Check if we have current tangent and set as previous
if (currentTangent!=Vector3.zero)
{
previousTangent = currentTangent;
}
// Angle on Z axis to lean object on rail
sideLean = (Vector3.Angle(currentTangent - previousTangent, transform.forward) - 90) * (Speed/MaxSpeed)*MaxLeanFactor;
// Apply lean angle
this.gameObject.transform.localRotation = Quaternion.Euler(0, 0, sideLean);

Speed is current speed of object on rail
MaxSpeed is maximum speed on rail
MaxLeanFactor is factor for making lean more or less

I'm not sure if that's working correctly since I discarded that feature.
Reply
#9
No wonder I removed feature since it was not working.
Up Dumping Time: does something similar but now I want to make it work.

How to apply rotation of object on rail since it is constantly corrected by movement on spline?
If Direction Dumping and Up Dumping Time are set to 0 rotating on Y or Z is always in line with spline?
Reply
#10
Figured it out.
Move leaning logic behind base.Update() in my Motor Update() component and:
this.gameObject.transform.localEulerAngles = new Vector3 (this.gameObject.transform.localEulerAngles.x, this.gameObject.transform.localEulerAngles.y, this.gameObject.transform.localEulerAngles.z+sideLean);
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: