Jump to content

AE_Grad

New Members
  • Posts

    2
  • Joined

  • Last visited

Everything posted by AE_Grad

  1. I found this topic because I am trying to model KSP engines in python and I wasn't sure how to set up the interpolation for floatCurves in python. I weighed not commenting on an old thread but I thought that this is such a good repository of knowledge on the subject and I think it's better for me to add my 2 cents here rather than creating a new thread and letting that info be scatted to the winds. It really helped me to read everyone discuss their take on how to reverse engineer how KSP and Unity are handling these float curves. And this post intend for someone in my shoes is come to this thread wanting to learn about these float curves but is perhaps a little frustrated to see that a lot of the images don't work. From what I gather from reading this is the algorithm for generating a floatCurve... KSP take n number of keys in one of two forms form 1: key = x1 y1 key = x2 y2 ... key = xn yn or form 2: key = x1 y1 key = x2 y2 InSlope1 OutSlope1 ... key = x(n-1) y(n-1) InSlope(n-1) OutSlope(n-1) key = xn yn If slopes are not explicitly passed, as in form 1, then KSP will calculate them as the mean of the InSlope and the OutSlope. For the first and last point the slope will be based on the one adjacent point. In the case of form 2 where a slope in and out are explicitly given I'm less clear on how to recreate that but for my purposes I don't need to know. This is the python script I made. # imports modules import numpy as np from scipy import interpolate import matplotlib.pyplot as plt ## input your keys here # key1 key2 key3 key4 key5 key6 key7 key8 key9 x = [0.00, 0.08, 0.15, 0.18, 0.42, 0.60, 0.83, 0.92, 1.00] y = [0.01, 0.13, 0.52, 0.59, 0.84, 0.74, 0.99, 0.98, 0.92] ## calculates the slopes at every key # the slope at every key is the average of the slope on the right and left # the first and last slope are just the slope of the left OR right d = [] for i in range(1,len(x)): d.append((y[i]-y[i-1])/(x[i]-x[i-1])) dydx = [d[0]] for i in range(1,len(d)): dydx.append((d[i-1]+d[i])/2) dydx.append(d[-1]) ## F is the function we are seaking F = interpolate.CubicHermiteSpline(x, y, dydx) ## this chunk of code generated the plot X = np.linspace(min(x), max(x)) Y = F(X) fig, ax = plt.subplots(figsize=(10, 6),dpi=80) # Create a figure containing a single axes. ax.plot(x, y, 'or', X, Y, '-b') ax.autoscale(1040) As you can see it generates the same curve as Felbourn's example. And to check against Meithan/NathanKell's example... x = [0.0, 1.0, 7] y = [300, 280, 0] d = [] for i in range(1,len(x)): d.append((y[i]-y[i-1])/(x[i]-x[i-1])) dydx = [d[0]] for i in range(1,len(d)): dydx.append((d[i-1]+d[i])/2) dydx.append(d[-1]) print(dydx) F = interpolate.CubicHermiteSpline(x, y, dydx) print(F(0.4463469)) output>>> [-20.0, -33.33333333333333, -46.666666666666664] 292.5437532826969 So this method works as far as I can tell (I haven't tested against in game data yet) Hopefully this helps someone.
×
×
  • Create New...