Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by spirit_2 · May 25 at 06:36 PM · curvebeziertangent

Tangent for 5th order Bezier curve

I'm using Bezier curves for gameobject placement. Mostly it's enough to use cubic curve but in some cases I need more precision. I'm not strong in math, so I found 5th order equation in wiki and the curve looks fine. Now I just need set angles. Can anyone help me with tangent equation for 5th order Bezier curve? [1]:

 //Cubic
 public Vector3 GetPosition(float t)
         {
             return (1 - t) * (1 - t) * (1 - t) * _p0 
                 + 3 * (1 - t) * (1 - t) * t * _p1
                 + 3 * (1 - t) * t * t * _p2 
                 + t * t * t * _p3;
         }
 
         public Vector3 GetTangent(float t)
         {
             float u = 1 - t;
             float uu = u * u;
             float tu = t * u;
             float tt = t * t;
 
             Vector3 P = _p0 * 3 * uu * (-1.0f);
             P += _p1 * 3 * (uu - 2 * tu);
             P += _p2 * 3 * (2 * tu - tt);
             P += _p3 * 3 * tt;
 
             return P.normalized;
         }
 
 //n5
 public Vector3 GetPosition(float t)
         {
             float u = 1 - t;
             float uu = u * u;
             float uuu = uu * u;
             float tt = t * t;
             float ttt = t * tt;
 
             Vector3 P = _p0 * uu * uuu;
             P += _p1 * 5 * uu * uu * t;
             P += _p2 * 10 * uuu * tt;
             P += _p3 * 10 * ttt * uu;
             P += _p4 * 5 * tt * tt * u;
             P += _p5 * ttt * tt;
 
             return P;
         }


screenshot-2.jpg (10.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · May 26 at 03:58 PM

Since I like math I just derived the tangent function myself (actually two times in two different ways):

 public Vector3 GetTangent(float t)
 {
     float u = 1f - t;
     float uu = u * u;
     float uuu = uu * u;
     float tt = t * t;
     float ttt = t * tt;
     Vector3 T = _p0 * (uuu * u)
         - _p1 * (uuu * (5 * t - 1))
         + _p2 * (2 * t * uu * (5 * t - 2))
         - _p3 * (2 * tt * u * (5 * t - 3))
         + _p4 * (ttt * (5 * t - 4))
         - _p5 * (ttt * t);
     return T;
 }

I have no time to test it, so feel free to try it out. Note I would recomment you also put brackets around your factors in your GetPosition method. Otherwise a lot performance would be wasted. A multiplication is carried out left to right by the compiler. So in your code you calculate _p0 * uu which returns a new vector3 and then you multiply that result by uuu. So those would be two Vector*float multiplciations. When you group the float factors in brackets they are calculated first and you only do a single Vector - float multiplication per control point. So something like this:

 public Vector3 GetPosition(float t)
 {
     float u = 1f - t;
     float uu = u * u;
     float uuu = uu * u;
     float tt = t * t;
     float ttt = t * tt;

     Vector3 P = _p0 * (uu * uuu)
         + _p1 * (5 * u * uuu * t)
         + _p2 * (10 * uuu * tt)
         + _p3 * (10 * ttt * uu)
         + _p4 * (5 * t * ttt * u)
         + _p5 * (tt * ttt);
     return P;
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spirit_2 · May 27 at 12:38 PM 0
Share

Thanks for the answer. Although it didn't worked for me as is and I had already found another solution, it still may be useful in future. I'll keep in mind vector/float multiplication order, thanks!

avatar image
0

Answer by spirit_2 · May 26 at 09:48 AM

Suddenly it hit me

         public Vector3 GetTangent(float t)
         {
             Vector3 p1 = GetPosition(t - 0.01f);
             Vector3 p2 = GetPosition(t + 0.01f);
 
             return (p2-p1).normalized;
         }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the math behind AnimationCurve.Evaluate? 5 Answers

iTween for Dynamically Moving Objects around a Sphere 0 Answers

Curving a cuboid mesh 1 Answer

Bezier Curve for Waypoint System 0 Answers

Curve vertex array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges