- Home /
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;
}
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];
}
@sjhalayka , thanks for the code.....i will try this out...
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.
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!
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.
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.
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
Bunny83 also mentions Catmull-Rom curves. Worth checking out, since they apparently are continuous by nature.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211447im_/https://answers.unity.com/themes/thub/images/avi.jpg)