// // Split a curve into sections with some resolution // //Steps is the number of sections we are going to split the curve in //So the number of interpolated values are steps + 1 //tEnd is where we want to stop measuring if we dont want to split the entire curve, so tEnd is maximum of 1 public static List <Vector3> SplitCurve(_Curve curve, int steps, float tEnd) { //Store the interpolated values so we later can display them List <Vector3> interpolatedPositions = new List <Vector3>(); //Loop between 0 and tStop in steps. If tStop is 1 we loop through the entire curve //1 step is minimum, so if steps is 5 then the line will be cut in 5 sections float stepSize = tEnd / (float)steps; float t = 0f; //+1 becuase wa also have to include the first point for (int i = 0; i < steps + 1; i++) { //Debug.Log(t); Vector3 interpolatedValue = curve.GetPosition(t); interpolatedPositions.Add(interpolatedValue); t += stepSize; } return(interpolatedPositions); }
// // Estimate the derivative at point t // //https://www.youtube.com/watch?v=pHMzNW8Agq4 public static float EstimateDerivative(_Curve curve, float t) { //We can estimate the derivative by taking a step in each direction of the point we are interested in //Should be around this number float derivativeStepSize = 0.0001f; Vector3 valueMinus = curve.GetPosition(t - derivativeStepSize); Vector3 valuePlus = curve.GetPosition(t + derivativeStepSize); //Have to multiply by two because we are taking a step in each direction Vector3 derivativeVector = (valuePlus - valueMinus) * (1f / (derivativeStepSize * 2f)); float derivative = Vector3.Magnitude(derivativeVector); return(derivative); }
public static InterpolationTransform GetTransform_FrenetNormal(_Curve curve, float t) { //Position on the curve at point t Vector3 pos = curve.GetPosition(t); //Forward direction (tangent) on the curve at point t Vector3 forwardDir = curve.GetTangent(t); Vector3 secondDerivativeVec = curve.GetSecondDerivativeVec(t); MyQuaternion orientation = InterpolationTransform.GetOrientation_FrenetNormal(forwardDir, secondDerivativeVec); InterpolationTransform trans = new InterpolationTransform(pos, orientation); return(trans); }
// // Alternative 1.5. Similar to Alternative 1, but we know the up vector at both the start and end position // public static InterpolationTransform GetTransform_InterpolateBetweenUpVectors( _Curve curve, float t, Vector3 upRefStart, Vector3 upRefEnd) { //Position on the curve at point t Vector3 pos = curve.GetPosition(t); //Forward direction (tangent) on the curve at point t Vector3 forwardDir = curve.GetTangent(t); //Interpolate between the start and end up vector to get an up vector at a t position Vector3 interpolatedUpDir = Vector3.Normalize(BezierLinear.GetPosition(upRefStart, upRefEnd, t)); MyQuaternion orientation = InterpolationTransform.GetOrientation_UpRef(forwardDir, interpolatedUpDir); InterpolationTransform trans = new InterpolationTransform(pos, orientation); return(trans); }
public static InterpolationTransform GetTransform_UpRef(_Curve curve, float t, Vector3 upRef) { //Position on the curve at point t Vector3 pos = curve.GetPosition(t); //Forward direction (tangent) on the curve at point t Vector3 forwardDir = curve.GetTangent(t); //A simple way to get the other directions is to use LookRotation with just forward dir as parameter //Then the up direction will always be the world up direction, and it calculates the right direction //This idea is not working for all possible curve orientations //MyQuaternion orientation = new MyQuaternion(forwardDir); //Your own reference up vector MyQuaternion orientation = InterpolationTransform.GetOrientation_UpRef(forwardDir, upRef); InterpolationTransform trans = new InterpolationTransform(pos, orientation); return(trans); }
//Not defined for a single point, you always need a previous transform //public static InterpolationTransform InterpolationTransform GetTransform_RotationMinimisingFrame() //{ //} public static List <InterpolationTransform> GetTransforms_RotationMinimisingFrame(_Curve curve, List <float> tValues, Vector3 upRef) { List <InterpolationTransform> transforms = new List <InterpolationTransform>(); for (int i = 0; i < tValues.Count; i++) { float t = tValues[i]; //Position on the curve at point t Vector3 position = curve.GetPosition(t); //Forward direction (tangent) on the curve at point t Vector3 tangent = curve.GetTangent(t); //At first pos we dont have a previous transform if (i == 0) { //Just use one of the other algorithms available to generate a transform at a single position MyQuaternion orientation = InterpolationTransform.GetOrientation_UpRef(tangent, upRef); InterpolationTransform transform = new InterpolationTransform(position, orientation); transforms.Add(transform); } else { //To calculate the orientation for this point, we need data from the previous point on the curve InterpolationTransform previousTransform = transforms[i - 1]; MyQuaternion orientation = InterpolationTransform.GetOrientation_RotationFrame(position, tangent, previousTransform); InterpolationTransform transform = new InterpolationTransform(position, orientation); transforms.Add(transform); } } return(transforms); }