public Vector3 FindPositionAt(float p) { if (worldPoints == null) { return(Vector3.zero); } if (worldPoints.Length == 0) { return(Vector3.zero); } if (worldPoints.Length == 1) { return(points[0]); } if (worldPoints.Length == 2) { return(AnimMath.Lerp(worldPoints[0], worldPoints[1], p)); } Vector3 result = Vector3.zero; float leftValue = 0; //this is how far we have walked down the line for (int i = 0; i < curveLengths.Length; i++) { float rightValue = leftValue + curveLengths[i]; float rightPercent = rightValue / splineLength; if (rightPercent >= p) { float leftPercent = leftValue / splineLength; float curvePercent = (p - leftPercent) / (rightPercent - leftPercent); Vector3 a = worldPoints[i]; Vector3 b = worldPoints[i + 1]; Vector3 c = worldPoints[i + 2]; if (i > 0) { a = AnimMath.Lerp(a, b, .5f); } if (i < curveLengths.Length - 1) { c = AnimMath.Lerp(b, c, .5f); } result = AnimMath.QuadraticBezier(a, b, c, curvePercent); break; } leftValue = rightValue; } return(result); }
void DrawCurve(Vector3 a, Vector3 b, Vector3 c) { int res = 10; Vector3 pos1 = new Vector3; for(int i = 0; i <= res; i++) { float p = i / (float)res; Vector3 pos2= AnimMath.QuadraticBezier(a, b, c, p); if(i>0)Gizmos.DrawLine(pos1, pos2); pos1 = pos2; } }
void DrawCurve(Vector3 a, Vector3 b, Vector3 c) { Vector3 pos1 = new Vector3(); for (int i = 0; i <= RESOLUTION; i++) { float p = i / (float)RESOLUTION; Vector3 pos2 = AnimMath.QuadraticBezier(a, b, c, p); if (i > 0) { Gizmos.DrawLine(pos1, pos2); } pos1 = pos2; } }
/// <summary> /// Calculate the length of beziar curve from a to c /// </summary> /// <param name="a"> anchor point<param> /// <param name="b">curve handle</param> /// <param name="c">anchor point</param> /// <returns>the length of the curve</returns> float lengthOfCurve(Vector3 a, Vector3 b, Vector3 c) { float result = 0; Vector3 pos1 = new Vector3(); for (int i = 0; i <= RESOLUTION; i++) { float p = i / (float)RESOLUTION; Vector3 pos2 = AnimMath.QuadraticBezier(a, b, c, p); if (i > 0) { result += (pos2 - pos1).magnitude; } pos1 = pos2; } return(result); }