标签:
1 /// <summary> 2 /// 一般贝塞尔曲线 (两个控制点) 3 /// </summary> 4 public class Bezier 5 { 6 private Vector3 Start = Vector3.zero; //开始点 7 private Vector3 StartControl = Vector3.zero; //开始控制点 8 private Vector3 EndControl = Vector3.zero; //结束控制点 9 private Vector3 End = Vector3.zero; //结束点 10 11 private Vector3 P0, P1, P2, P3; 12 13 private float Ax, Bx, Cx; 14 private float Ay, By, Cy; 15 private float Az, Bz, Cz; 16 17 /// <summary> 18 /// 初始化 19 /// </summary> 20 /// <param name="start">开始点</param> 21 /// <param name="sControl">开始控制点</param> 22 /// <param name="end">结束点</param> 23 /// <param name="eControl">结束控制点</param> 24 public void Init(Vector3 start, Vector3 sControl, Vector3 end, Vector3 eControl) 25 { 26 Start = start; 27 StartControl = sControl; 28 End = end; 29 EndControl = eControl; 30 31 P0 = Start; 32 P1 = StartControl; 33 P2 = EndControl; 34 P3 = End; 35 36 //x多项式系数 37 Cx = 3.0f * (StartControl.x - Start.x); 38 Bx = 3.0f * (EndControl.x - StartControl.x) - Cx; 39 Ax = End.x - Start.x - Cx - Bx; 40 41 //y多项式系数 42 Cy = 3.0f * (StartControl.y - Start.y); 43 By = 3.0f * (EndControl.y - StartControl.y) - Cy; 44 Ay = End.y - Start.y - Cy - By; 45 46 //z多项式系数 47 Cz = 3.0f * (StartControl.z - Start.z); 48 Bz = 3.0f * (EndControl.z - StartControl.z) - Cz; 49 Az = End.z - Start.z - Cz - Bz; 50 } 51 52 /// <summary> 53 /// 获取当前位置 54 /// </summary> 55 /// <param name="fT">[0.0~1.0]</param> 56 /// <returns></returns> 57 public Vector3 Get(float fT) 58 { 59 float tSquared = fT * fT; 60 float tCubed = tSquared * fT; 61 62 //计算返回值 63 Vector3 result; 64 result.x = (Ax * tCubed) + (Bx * tSquared) + (Cx * fT) + Start.x; 65 result.y = (Ay * tCubed) + (By * tSquared) + (Cy * fT) + Start.y; 66 result.z = (Az * tCubed) + (Bz * tSquared) + (Cz * fT) + Start.z; 67 68 return result; 69 } 70 }
1 /// <summary> 2 /// 高阶贝塞尔曲线 (有N个控制点) 3 /// </summary> 4 public class MutableBezier 5 { 6 private Vector3[] m_Pos; 7 private int m_nCount; 8 9 /// <summary> 10 /// pos 格式(起始点、控制点1,控制点2、... 控制点n、终点) 11 /// </summary> 12 /// <param name="pos"></param> 13 public void Init(Vector3[] pos) 14 { 15 m_Pos = pos; 16 m_nCount = m_Pos.Length; 17 } 18 19 public Vector3 Get(float fT) 20 { 21 float x = 0, y = 0, z = 0; 22 23 int index = 0; 24 while (index < m_nCount) 25 { 26 float f = PascalTriangleRatio(m_nCount - 1, index) * Mathf.Pow(fT, index) * Mathf.Pow(1 - fT, m_nCount - 1 - index); 27 28 x += f * m_Pos[index].x; 29 y += f * m_Pos[index].y; 30 z += f * m_Pos[index].z; 31 32 index++; 33 } 34 35 return new Vector3(x, y, z); 36 } 37 38 /// <summary> 39 /// 阶乘 40 /// </summary> 41 /// <param name="n"></param> 42 /// <returns></returns> 43 public int Factorial(int n) 44 { 45 int result = 1; 46 while (n > 0) 47 { 48 result *= n; 49 n--; 50 } 51 return result; 52 } 53 54 /// <summary> 55 /// 杨辉三角 56 /// </summary> 57 /// <param name="n"></param> 58 /// <param name="i"></param> 59 /// <returns></returns> 60 public int PascalTriangleRatio(int n, int i) 61 { 62 return Factorial(n) / (Factorial(i) * Factorial(n - i)); 63 } 64 }
public class MutablePath { private Vector3[] m_Pos; /// <summary> /// pos 格式(起始点、控制点1,控制点2、... 控制点n、终点) /// </summary> /// <param name="pos"></param> public void Init(Vector3[] pos) { m_Pos = PathControlPointGenerator(pos); } public Vector3 Get(float fT) { if (m_Pos == null) return Vector3.zero; return PathInterp(m_Pos, fT); } /// <summary> /// 得到轨迹path的控制点 /// </summary> /// <param name="path"></param> /// <returns></returns> public Vector3[] PathControlPointGenerator(Vector3[] path) { Vector3[] suppliedPath; Vector3[] vector3s; suppliedPath = path; int offset = 2; vector3s = new Vector3[suppliedPath.Length + offset]; Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length); vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]); vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]); if (vector3s[1] == vector3s[vector3s.Length - 2]) { Vector3[] tmpLoopSpline = new Vector3[vector3s.Length]; Array.Copy(vector3s, tmpLoopSpline, vector3s.Length); tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3]; tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2]; vector3s = new Vector3[tmpLoopSpline.Length]; Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length); } return (vector3s); } /// <summary> /// 取得轨迹上的点 /// </summary> /// <param name="pts">轨迹</param> /// <param name="t">时间[0,1]</param> /// <returns></returns> public Vector3 PathInterp(Vector3[] pts, float t) { int numSections = pts.Length - 3; int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1); float u = t * (float)numSections - (float)currPt; Vector3 a = pts[currPt]; Vector3 b = pts[currPt + 1]; Vector3 c = pts[currPt + 2]; Vector3 d = pts[currPt + 3]; return .5f * ( (-a + 3f * b - 3f * c + d) * (u * u * u) + (2f * a - 5f * b + 4f * c - d) * (u * u) + (-a + c) * u + 2f * b ); } }
标签:
原文地址:http://www.cnblogs.com/cocos2d/p/4693929.html