Using Bezier curves in Unity games

Source: Internet
Author: User

Sun Guangdong 2015.8.15

For example, in 3D RPG games. We want to set ballistics, different track types!


Purpose: The main purpose of this article is to give you a basic idea of how to use Bezier curves in the game.

Bezier curve is the most important curve, generally used in computer graphics and image processing.

Bezier curves can be used to create smooth curved roads, curved paths like Zuma games, curved rivers, and so on.

A Bezier curve is a set of defined control points P0 to Pn, in the order in which N calls it (n = 1 is linear. 2 is two times, etc.). The first and last control points always have the curve of the endpoint, however, the middle two control points (if any) are generally not located on the curve.

Bezier curve consists of two control points i.e. N = 2 is called linear Bezier

Bezier curves consist of three control points i.e. N = 3 called two Bezier curves

The Bezier curve consists of four control points, n = 4, so it is called a three-time Bezier curve.


Bezier function that returns the point of the Bezier curve. Use the concept of linear interpolation as the basis. So let's see what the first is linear interpolation.

The point of linear interpolation between two points gets between those two points, 0 <= T <= 1, like Mathf.lerp.

Interpolation points. With P formula P0 and P1 can be written,

P = p0+ t (p1-p0). 0 <= T <= 1

Here, in order to get interpolated points we join TTH point P to the fraction of the distance between the two 0. So.

for T = 0,p = P0.

For T = 1. P = P1.

for T = 0.5,p = Point between P0 and P1.


Linear Bezier Curve:

The linear Bezier curve has two control points. To give the two points P0 and P1 a linear Bezier curve is just a straight line between the two points. The curve is equivalent to the linear interpolation given,

B (t) = p0+ T (p1-p0) = (1-t) P0 + tP1, 0 <= t <= 1

How the linear Bezier curve is calculated is, for example, what is seen below:




Quadratic Bezier curve:

A quadratic Bezier curve has three control points.

Quadratic Bezier curves are linear interpolation of two linear Bezier curves of point-to-point.

In order to give three points P0, P1 and P2 a two-time Bezier curve, in fact, two linear Bézier curves.     P0 and P1 of linear Bezier curves and linear Bezier P1 and P2. So, give the Bezier curve two times:

B (t) = (1-T) bp0p1 (t) + t BP1P2 (t). 0 <= T <= 1

B (t) = (1-T) [(1-t) P0 + tP1] + t [(1-t) p1+ tp2],0 <= T <= 1


By arranging the above equation again,

B (t) = (1-T) 2p0+ 2 (1-t) tP1 + t2p2, 0 <= t <= 1

The quadratic Bezier curve animation is calculated as seen below:



Three times Bezier curve:

A three-cubic Bezier curve has four control points.

Quadratic Bezier curves are linear interpolation of two two Bezier curves of point-to-point. For the given four points P0, P1, P2, and P3 three quadratic Bezier curves. is a linear interpolation of two Bezier curves P0, P1 and P2 and two Bezier curves P1, P2, and P3. So, give three cubic Bezier curves.

B (t) = (1-T) bp0,p1,p2 (t) + t BP1,P2,P3 (t), 0 <= T <= 1

B (t) = (1-T) [(1-T) 2p0+ 2 (1-t) tP1 + t2p2] + t [(1-t) 2p1+ 2 (1-t) tP2 + t2p3]. 0 <= T <= 1


By arranging the above equation again,

B (t) = (1-T) 3p0 + 3 (1-t) 2tp1+ 3 (1-t) T2P2 + t3p3 0 <= t <= 1

The three-time Bezier curve is calculated as seen below:


So. It is generally possible to obtain a Bezier curve of degree n from two points of the degree n-1 of two corresponding Bezier curves (that is, two low-level linear interpolation), as a point-to-point linear interpolation.

In most applications, two two-or three-cubic Bezier functions are used.

However, you can always draw more complex curves using higher-level Bezier functions, but higher-level Bessel functions are computationally complex and add processing overhead.

So. Instead of drawing more complex curves using a higher degree Bessel function, you can use two two or three square Bezier functions multiple times. Over here. I created an ∞ curve to demonstrate and draw, using a three-time square Bezier function to loop. For example, see below.


To create a curve. As seen above. Please create a scene, for example, as seen below:



Now, attach the Bezier.cs script to the Bézier Manager

Bezier.cs:


Using unityengine;using System.Collections.Generic;    [Requirecomponent (typeof (Linerenderer))]public class bezier:monobehaviour{public transform[] controlpoints;        Public Linerenderer Linerenderer;        private int curvecount = 0;    private int layerorder = 0;                private int segment_count = 50;        void Start () {if (!linerenderer) {linerenderer = getcomponent<linerenderer> ();        } Linerenderer.sortinglayerid = Layerorder;    Curvecount = (int) CONTROLPOINTS.LENGTH/3;    } void Update () {drawcurve (); } void DrawCurve () {for (int j = 0; J <curveCount; J + +) {for (int i = 1; I <= S Egment_count;                i++) {Float T = I/(float) segment_count;                int Nodeindex = J * 3; Vector3 pixel = calculatecubicbezierpoint (t, controlpoints [Nodeindex].position, controlpoints [Nodeindex + 1].position , Controlpoints [nodEindex + 2].position, controlpoints [Nodeindex + 3].position);                Linerenderer.setvertexcount ((((J * segment_count) + i));            Linerenderer.setposition ((J * segment_count) + (i-1), pixel); }}} Vector3 Calculatecubicbezierpoint (float t, Vector3 P0, Vector3 p1, Vector3 p2, Vect        Or3 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; }}

Over here. The Calculatecubicbezierpoint function is the cubiz Bessel function that I have explained above to run. The DrawCurve function draws two three-cubic Bezier curves.


Between P0, P0-control Point1, P1-control Point1 and P1.

Between P1, P1-control Point1, P0-control Point2 and P0.


The curvature of the corresponding curve can be handled regardless of the control point. You can change the curve at any time by dragging the discretionary control points, as seen in the following:




Using Bezier curves in Unity games

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.