/// <summary> /// Get position evaluated at testLerpValue. /// </summary> /// <param name="testLerpValue"></param> /// <returns></returns> public Vector3 getPosition(float testLerpValue) { Vector3 position = Vector3.zero; //if length isnt 0 and endpoints isnt null if (curves.Count > 0) { testLerpValue = Mathf.Clamp(testLerpValue, 0, 0.999f); //determine total allocated lerp time float totalTimeBlockSize = 0; for (int i = 0; i < CurvesCount; i++) { totalTimeBlockSize += 1 / curves[i].speedCurveAverage; } float timeBlockStartValue = 0; int offset; float lerpValue = 0; for (offset = 0; offset < CurvesCount; offset++) { timeBlockStartValue += (1f / curves[offset].speedCurveAverage); //if is correct animation curve to determine area for if ((testLerpValue * totalTimeBlockSize) <= timeBlockStartValue) { float lerp = ((testLerpValue * totalTimeBlockSize) - (timeBlockStartValue - (1f / curves[offset].speedCurveAverage))) / (1f / curves[offset].speedCurveAverage); lerpValue = BezierCurveUtilities.calculateCubicBezierArea(0, lerp, curves[offset].speedCurvePoints) / curves[offset].totalCurveArea;//determine percentage of area under curve on the left of lerp value in animation curve break; } } if (offset < curves.Count) { float subtractedLerpVal = 1 - lerpValue; //cubic polynomial. (1-lerpVal)^3 * P0 + 3(1-lerpVal)^2 * lerpVal * P1 + 3(1-lerpVal) * lerpVal^2 * P2 + lerpVal^3 * P3 position = (subtractedLerpVal * subtractedLerpVal * subtractedLerpVal * curves[offset].endPoints[0]) + (3 * subtractedLerpVal * subtractedLerpVal * lerpValue * curves[offset].controlPoints[0]) + (3 * subtractedLerpVal * lerpValue * lerpValue * curves[offset].controlPoints[1]) + (lerpValue * lerpValue * lerpValue * curves[offset].endPoints[1]); } } return(position); }
public bool setAnimationCurve(int ofCurve, AnimationCurve curve) { if (isWithinArrayCheck(ofCurve)) { totalArea -= curves[ofCurve].speedCurveAverage; curves[ofCurve].speedCurve = curve; BezierCurveUtilities.CubicBezier2D bezier = BezierCurveUtilities.getAnimationCurvePositions(curves[ofCurve].speedCurve); curves[ofCurve].speedCurvePoints = bezier; float area = BezierCurveUtilities.calculateCubicBezierArea(0, 1, bezier.position1.y, bezier.position2.y, bezier.position3.y, bezier.position4.y); float testArea = BezierCurveUtilities.calculateCubicBezierArea(0, 0.5f, bezier.position1.y, bezier.position2.y, bezier.position3.y, bezier.position4.y); curves[ofCurve].totalCurveArea = area; totalArea += (area); return(true); } return(false); }