- Home /
Linear Interpolation of Array Values
Hey all,
Im currently working on a car physics simulation and I am implementing the torque curve (in Nm)/RPM of an MX5. So I created an Array with a step for every 1000RPM so from : 0 RPM to 7000 RPM :
public int[] maxMotorTorqueAtRPM = new int[8] { 0, 80, 169, 196, 210, 215, 195, 0 };
// arrayposition * 1000 = RPM > maxMotorTorqueAtRPM
So lets say RPM = 2000, then torque is 169nm But because the array is in steps of 1000RPM, i would need to use some interpolation to read the
ex. : torque value for 3421 RPM.
So I make a method :
public float CheckInterpolatedMaxMotorTorqueAtRPM (int []maxMotorTorqueAtRPM, int currentMotorRPM)
{
int interpolatedMaxMotorTorqueAtRPM = Mathf.Lerp (I dont know what to here)
}
I've looked all over the web but I cant seem to find the proper syntax to do this
Image for clarification :
The green dots are the points in the Array, and the purple line is what i want to interpolate.
Thanks, Olivier Wierda
Answer by Bunny83 · Jul 19, 2018 at 09:01 PM
Just forget about your array and just define your values as an AnimationCurve. Just make a public AnimationCurve variable:
public AnimationCurve motorTorque;
You can simply sample the curve by using Evaluate
float val = motorTorque.Evaluate(rpm);
Keep in mind that you can use actual curves or you can set the tangents to be flat is you really want linear interpolation. However since the torque / rmp curve is actually a curve you probably want to actually make it a curve ^^.
Oh wow! Thank you, this was exactly what I needed indeed!
Your answer
Follow this Question
Related Questions
How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers
How can you do calculations on two lists? 1 Answer
Unity C# Highscore table 2 Answers
Help with for loop and arrays 3 Answers
Get all GameObjects by variable value 2 Answers