Jump to content

[INFO] KSP floatCurves and you - the magic of tangents.


Taverius

Recommended Posts

  • 1 month later...

Is there an alternate program/plugin that can display those tangents? I don't want to install Unity, it's bloated and most of it's features are of no use to me.

Link to comment
Share on other sites

  • 2 weeks later...
Is there an alternate program/plugin that can display those tangents? I don't want to install Unity, it's bloated and most of it's features are of no use to me.

Unless you got some code that mimic Unity behaviour, not ! The issue is how Unity work with your data is not what you think it is actually and what many people thought it was when they wrote their documentation which have unfortunately spread a lot before Taverius comes and point the "everyone is wrong while believing to be right" thing. (Even Squad engines configs are wrong !)

Proof of this topic by picture:

i9rqXyw.jpg

(this is from my stack version of EM engine)

Edited by Justin Kerbice
Link to comment
Share on other sites

  • 3 weeks later...
Yeah. Stock turbojet velocity curve:

https://dl.dropboxusercontent.com/u/42665548/KSP/Modeling/Screenshot_188.png

Mine from TVPP/B9 rebalance of it:

https://dl.dropboxusercontent.com/u/42665548/KSP/Modeling/Screenshot_189.png

The basic jet is just as bad, and the RAPIER just uses a copy of the TJets.

Torque/Steering curves for the wheels have a few fun shapes mixed in there too :)

Exactly what I found when trying to make the BJE. That approach seems futile

Link to comment
Share on other sites

From my testing long ago starting AdvSRBs (which actually features a floatcurve editor in game), two points defining a curve with no tangents specified form a line. I recall having some trouble getting the game to see 0 tangents as 0s, sometimes it would ignore them and just create a line anyway :/

Link to comment
Share on other sites

  • 2 months later...

So after reading about this float curve editor I'm going to guess that what I hoped it could do it actually wont be able to do, but I'm going to ask any ways.

Say I have defined a curve that I am using to define how a particular engine flame changes based on atmospheric density.

It looks great and I now want the effects for my other engines to behave the same way, use the same curve essentially.

However even though I am using the same particle effect due to the fact that the next engine I am working on is a different size, my starting value has to be changed in order for it to fit in the bell.

is there a way I can define the new start value and get it to create a new curve with the same profile and give me the numbers I would have to use to accomplish this?

Link to comment
Share on other sites

  • 3 months later...

I followed the instructions on how to install the float curve editor, but it refuses to show up in Unity 4.2.2... Anyone else have trouble installing it or have a workaround?

Edit: Nevermind, got it working!

Edited by Quiznos323
Link to comment
Share on other sites

I hear Taverius is not around, but I'll ask anyway in case somebody else knows: how are the tangents defined when the atmosphereCurve doesn't state them explicitly?

Specifically, I'm trying to compute the Isp of an engine at arbitrary pressure values. Take the LV-T30 for instance. Its 1.0.2 .cfg file says:

atmosphereCurve {
key = 0 300
key = 1 280
key = 7 0.001
}

I hear it's a piecewise cubic Hermite spline defined by those three points, but the actual curve is ambiguous without the tangents.

The Unity docs for AnimationCurve say "Smooth tangents are automatically computed for the key", but I don't understand what they mean by "smooth", other than the inTangent and outTangent are probably the same (which is not enough to define them).

I tried defining them with the "finite difference" method specified in the Wikipedia article (which is just taking the average of the slopes to the left and right), and I get something like this:

B8ipGxkm.png

Doesn't look too "smooth" to me (sure, it's mathematically smooth in the sense that the curve and its first derivative are continuous, but I feel something else is meant by "smooth" here).

So does anyone know how I can compute the Isp at arbitrary pressure values?

Link to comment
Share on other sites

If memory serves, smooth tangents means that the tangent slope is the average of the straight slope between k-1 and k, and k and k+1 for any key k. When the extra key doesn't exist, it's probably just ignored and you get linear.

That said, you can verify by grabbing Unity and the curve editor in the OP, and pasting the curve in, and then right-click each key and change it from auto to broken, and see what numbers appear.

Link to comment
Share on other sites

If memory serves, smooth tangents means that the tangent slope is the average of the straight slope between k-1 and k, and k and k+1 for any key k. When the extra key doesn't exist, it's probably just ignored and you get linear.

That said, you can verify by grabbing Unity and the curve editor in the OP, and pasting the curve in, and then right-click each key and change it from auto to broken, and see what numbers appear.

Well, the plot I posted above was computed precisely by taking the average of the left and right slopes (for the middle point; one-sided slopes for the endpoints). But I still feel I'm missing an ingredient. I guess I have no option but to grab Unity and find out with the curve editor. Thanks for the help.

Link to comment
Share on other sites

Checked in Unity.

Inputs:

key = 0 330

key = 1 295

key = 15 0

outputs:

key = 0 330 -35 -35

key = 1 295 -28.03571 -28.03571

key = 15 0 -21.07143 -21.07143

Clearly the first key is linear to the middle (-35/1), and the last key is linear to the middle (-295/14). The middle key does appear to be the average, which means it's the average of the linear slopes to its neighbor keys.

Oh, and your example becomes

key = 0 300 -20 -20

key = 1 280 -33.33325 -33.33325

key = 7 0.001 -46.6665 -46.6665

However, the curve Unity renders doesn't quite look like yours, so maybe you're interpolating differently?

Two spot checks: key = 0.4463469 292.5437 and key = 3.856129 157.1699 (from adding keys at midpoints), if you're not getting those values for those times, your interpolation may be off.

Edited by NathanKell
Link to comment
Share on other sites

Checked in Unity.

Inputs:

key = 0 330

key = 1 295

key = 15 0

outputs:

key = 0 330 -35 -35

key = 1 295 -28.03571 -28.03571

key = 15 0 -21.07143 -21.07143

Clearly the first key is linear to the middle (-35/1), and the last key is linear to the middle (-295/14). The middle key does appear to be the average, which means it's the average of the linear slopes to its neighbor keys.

Oh, and your example becomes

key = 0 300 -20 -20

key = 1 280 -33.33325 -33.33325

key = 7 0.001 -46.6665 -46.6665

However, the curve Unity renders doesn't quite look like yours, so maybe you're interpolating differently?

Two spot checks: key = 0.4463469 292.5437 and key = 3.856129 157.1699 (from adding keys at midpoints), if you're not getting those values for those times, your interpolation may be off.

Thanks a bunch, this is exactly what I needed!

There was a typo in my middle tangent calculation (a 2 instead of a 5 ... man, my dyslexia strikes again). Here's the output now:

tangents: [-20, -33.33325000000001, -46.666500000000006]
0.4463469 292.543744091
3.856129 157.169858986

and here's the resulting curve:

2qQCfsXm.png

Looks "smooth" now :). Thanks!

Link to comment
Share on other sites

  • 1 year later...
On 10/17/2014 at 7:09 AM, Dragon01 said:

Is there an alternate program/plugin that can display those tangents? I don't want to install Unity, it's bloated and most of it's features are of no use to me.

I realize this is half a year late, but I was having the same issue and figured others would, too.

You can actually plot the values yourself without much difficulty! Here's how:

Spoiler

 

1. Find the data

The data is written in config files using the following format

  key  = x1  y1  I1 O1
  key  = x2  y2  I2 O2
  key  = x3  y3  I3 O3
  key  = x4  y4  I4 O4

Where each key defines a point in terms of its x value, y value, incoming tangent (I), and outgoing tangent (O). A good example of these values might be mach values for x (the thing you know), drag coefficients for y (the thing you want to know), and the I and O tangents for how quickly drag coefficients change approaching and leaving this point.

 

2. Form the equation

We'll write an equation for the line that goes between the points of key1 and key2. We could try to make this equation in terms of x, but it would be really hard! So instead, first we'll make a convenience variable called u. The fancy term for it is a unitless transform, and it makes life soooooo much easier. Then we'll write an equation for y (the thing we want!) in terms of this u.

u = (x - x1)/( x2  - x1 )           Eq. 1

y = Au3 +Bu2 + Cu + D          Eq. 2

Cool, so if we make up an x value, and we want to know the corresponding y value we simply plug x into Eq 1, and then plug the resulting u into Eq 2 and we get our value! All's we need are those big letters A-D.

 

3. Find the coefficients

To find the values for A-D, we plug in some numbers from key1 and some from key2. With these values in hand, simply plug and chug into the above equations to get your answers.

  A =  2y1 +  O1  - 2y2 + I2
  B = -3y1 - 2O1 + 3y2  - I2
  C =    O1
  D =    y1
 

4. Do this for each key and plot/use the results

Or just borrow the version I have in google sheets. Google sheets is terrible at plotting, because its x axis literally doesn't work. Seriously, how? I go with excel '03, but does anyone else have a suggestion for plotting datasets? I've been considering writing a javascript plotter that would take in keys and print out results, but those take time. If people are interested let me know, otherwise I'll stick to excel!

Here's a link to my google sheets calculator. The coefficients + y values will update automatically, you need to provide x values and key values. When you plug in key values, you can select the line and click+drag the little blue box on the bottom right of your selection box to copy it down over a large section. When you plug in x values, if you put in 2 numbers (say 0, 0.25) select them and do the same box click+drag, it'll generate a series (0, 0.25, 0.5, 0.75, 1....) so it's easier than it looks.

https://docs.google.com/spreadsheets/d/1tOJqRXW4Wo1SWHrNcMaj5yhGBmKHT_NTiezBKlGGeEI/edit?usp=sharing

 

5. View and admire!

There's a lot of interesting things to notice, and little quirks that finally make sense. Enjoy!

 

I hope that helps!

Edited by Cunjo Carl
Link to comment
Share on other sites

  • 4 months later...
  • 4 years later...

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. 

float-Curve.png

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.

Edited by AE_Grad
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...