Original: "Unity" Bezier curve about point, length, tangent calculation in Unity C # implementation
Write in front
Recently, a path to the project has been edited, the basic idea is to meet several basic needs:
"Additional instructions" in fact this article and this does not matter, you can skip the "write in front" section, cross to the body part
When editing:
① arbitrarily increase or decrease, insert, delete road points, as long as the number of road points is greater than 1, draw the curve, the curve must pass the road point.
② adjusts the forward direction of the road point, controls the line tangent direction of the curve, and the direction of the outlet tangent. This lets you adjust the shape of the curve directly by rotating it.
③ control the forward direction, add variables describing the tangent "strength" to further control the shape of the curve.
④ can specify the logical length of each curve, and the program provides a curve approximate length to help determine the logical length.
⑤ the data for the exported curve.
At run time, data can be eradicated:
⑥ a number starting at 0 on the route, using 0.01~0.99 to describe the position on a curve (logically), and then turn it into the actual coordinates.
The ⑦ can obtain a tangent direction at any point on the curve.
Body
Four points from the road point, the road point forward to the Sanche Besel curve
For Bezier curves, visible below
Bezier curve
Bezier Curve Online Demo
Each of the two routes serves as the starting point for the Sanche Besel curve (No. 0 point P0) and the end point (3rd P3).
The forward direction of the starting point is multiplied by the "strength" variable, plus the start coordinate, as the 1th point P1.
The forward of the starting point is multiplied by the "strength" variable, plus the start coordinate, as the 2nd point P2.
After you get the P0~P4 four points, you can use the relevant formula for the Sanche Besel curve.
Draw a Bezier curve, the red line part of the figure
Sanche Besel a point coordinate on a curve line (Unity & C #)
Sanche Besel Curve formula (from Baidu Encyclopedia)
T, P0, p1, p2, p3 in the formal parameters correspond to T in the formula and P0~P3
publicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) { float1 - t; float tt = t * t; float uu = u * u; float uuu = uu * u; float ttt = tt * t; Vector3 p = uuu * p0; 3 * uu * t * p1; 3 * u * tt * p2; p += ttt * p3; return p; }
Approximate length of the Sanche Besel curve (Unity & C #)
The idea of calculating length:
The approximate length of a curve is obtained by taking n points on the Bezier curve, calculating the length of the line between points, and adding and making. The more points you take, the more accurate the length is.
The P0, p1, p2, p3 in the formal parameters correspond to the P0~P3 in the formula respectively. Pointcount represents the number of points, default 30.
PublicfloatBezierlength (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, int pointcount = -) {if(Pointcount <2) {return 0; }//Pick a point default of 30 float length=0.0F Vector3 Lastpoint = Bezierpoint (0.0F/(float) Pointcount, P0, p1, p2, p3); for(int i =1; I <= Pointcount; i++) {Vector3 Point= Bezierpoint ((float) i/(float) Pointcount, P0, p1, p2, p3);length+ = Vector3.distance ( Point, Lastpoint); Lastpoint = Point; }return length; }
Sanche Besel Tangent of a point on a curve line (Unity & C #)
In the case where a Bezier expression is known, you want to know the tangent of a point and differentiate the curve.
You can get:
After finishing, you can get
The whole formula consists only of P0~P3 and T and (1-T), in order to express more directly, without further finishing.
So get the following code
T, P0, p1, p2, p3 in the formal parameters correspond to T in the formula and P0~P3
public Vector3 beziertangent (float T, Vector3 P0, Vector3 p1, Vector3 p2, Vector3 p3) {float u = 1 -T; float uu = u * U; float tu = t * u; float tt = t * t; Vector3 P = p0 * 3 * UU * (-1.0 f); P + = P1 * 3 * (UU-2 * tu); P + = P2 * 3 * (2 * tu-tt); P + = P3 * 3 * TT; //returns the unit vector return p.normalized; }
Write it in the back.
Main references:
Using Bezier curves in Unity games
The position and tangent direction of a time for a two-time, three-time Bezier curve
Reprint please specify, from meow Meow Pill blog (http://blog.csdn.net/u011643833/article/details/78540554)
"Unity" Bezier curve about point, length, tangent calculation in Unity C # implementation