Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 singlearrowgames · Aug 30, 2019 at 05:17 PM · bezier

Combining bezier curves

I'm trying to create endless road racing game. In order to generate the road, I'm planning to use bezier curves and combine them to create the road. I have the code create a cubic bezier curve using 4 points but how can I join them together. This is the code I have so far:

 public class Bezier : MonoBehaviour
 {        
     public LineRenderer lineRenderer;    
     public Transform[] point0, point1, point2, point3;
     private int numPoints = 50;
 
     private Vector3 positions = new Vector3[50];
     void Start()
     {
         lineRenderer.positionCount = numPoints;       
         DrawCubic();
     }
 
     private void DrawCubic()
     {
         for (int i = 1; i <= numPoints; i++)
         {
             float t = i / (float)numPoints;
             positions[i - 1] = CalcCubic(t, point0.position, point1.position, point2.position, point3.position);
         }
         lineRenderer.SetPositions(positions);
     }
 
 void Update()
     {        
         DrawCubic();
     }
  private Vector3 CalcCubic(float t, Vector3 p0, Vector3 p1, Vector3 p2,Vector3 p3)
     {
         float u = 1 - t;
         float tt = t * t;
         float uu = u * u;
         float uuu = uu * u;
         float ttt = tt * t;
         Vector3 p = uuu * p0;
         p += 3 * uu * t * p1;
         p += 3 * u * tt * p2;
         p += ttt * p3;
         return p;
     }    
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
0

Answer by sjhalayka · Aug 31, 2019 at 06:35 AM

If you want the path to be automatically continuous, you must do Bezier curves of arbitrary order:

 // https://stackoverflow.com/questions/785097/how-do-i-implement-a-bézier-curve-in-c
 vector_4 getBezierPoint(List<Vector4> points, float t)
 {
     int i = points.Count - 1;
 
     while (i > 0)
     {
         for (int k = 0; k < i; k++)
         {
             points[k].x += t * (points[k + 1].x - points[k].x);
             points[k].y += t * (points[k + 1].y - points[k].y);
             points[k].z += t * (points[k + 1].z - points[k].z);
             points[k].w += t * (points[k + 1].w - points[k].w);
         }
 
         i--;
     }
 
     return points[0];
 }
Comment
Add comment · Show 4 · 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 singlearrowgames · Aug 31, 2019 at 03:40 PM 0
Share

@sjhalayka , thanks for the code.....i will try this out...

avatar image Bunny83 · Sep 01, 2019 at 10:47 AM 0
Share

This does not work the way you might think as this method is actually modifying all the points in the array. So you could only run this once for a given curve. That's why the original code actually creates a temporary copy of the whole array / list. It's also not very efficient if you have many points. It's much more common to join together several simple bezier curves or use Catmull Rom splines. Unity's own curve editor (which are also used by the animator) are actually just cubic bezier curves.


Higher order Bezier curves also get more and more disconnected from their control points as each and every control point has an effect on the whole curve. The time complexity of manually going through all lerp levels is O(n²). That's why catmull rom splines are nice. Each segment only depends on the 4 control points around it and always passes through the actual control points unlike bezier curves which only passes through the first and last point.


The key for a continuous and smooth curve using just cubic bezier segments is to have the same in-tangent for the second segment as you have used for the out-tangent of the first segment.

avatar image sjhalayka Bunny83 · Sep 01, 2019 at 04:59 PM 0
Share

Yes, that's why I don't pass the List in by ref. I use it to get the paths in this memo: http://vixra.org/abs/1807.0418

Thanks for your insight into Catmull-Rom splines!

avatar image Bunny83 sjhalayka · Sep 02, 2019 at 02:54 AM 0
Share

I don't get your by ref comment. List is a class and you modify the content of the class. It doesn't matter how you pass in the List. Classes are always reference types in C#. Don't confuse C# with C++. In C++ there is no difference between a class and a struct besides the default member visibility. In C# a class is always allocated on the heap and a stack is always a value type.

avatar image
0

Answer by sjhalayka · Aug 30, 2019 at 07:27 PM

This shows how to combine two Bezier curves, by making sure that the end and beginning control points align:

http://www.inf.ed.ac.uk/teaching/courses/cg/d3/bezierJoin.html

I wrote an answer showing how to do any order of Bezier curve, but it may not be accepted.

Comment
Add comment · Show 2 · 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 sjhalayka · Aug 31, 2019 at 09:29 PM 0
Share

As you can see from the solution to combine two Bezier curves (where each curve consists of 4 points), you have to make the following calculations:

 int num_control_points_per curve = 4;
 Vec v = P_0[num_control_points_per curve - 1] - P_0[num_control_points_per curve - 2];
 
 where
 P_1[0] == P_0[num_control_points_per curve - 1]

 and so:
 P_1[1] = P_1[0] - v

avatar image sjhalayka sjhalayka · Sep 01, 2019 at 05:32 PM 0
Share

Bunny83 also mentions Catmull-Rom curves. Worth checking out, since they apparently are continuous by nature.

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

110 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

Related Questions

Create a circle or ellipse with the GL class 1 Answer

How to convert cubic Bezier curve into AnimationCurve 3 Answers

Correct normals on a cylindrical rail (hairy ball theorem) 1 Answer

Ship movement in space-based RTS 0 Answers

Bezier movement is erratic 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