Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Express AnimationCurve as a Math formula

Discussion in 'Animation' started by Porky12458, May 6, 2024.

  1. Porky12458

    Porky12458

    Joined:
    Apr 19, 2024
    Posts:
    2
    Hey folks!

    I am going through the code of a game a really like in order to contribute to the wiki (which is pretty barren). I found out a key feature is partially given by an AnimationCurve, basically how hard the game gets as time passes. I was able to get the JSON output of the AnimationCurve but my smooth brain has no idea how to interpret that or how to transform that into a math equation where I can simply apply the input (in this case time passed since game begun) and get an output (like .Evaluate() does).

    My goal is to have a usable math expression that I can put on the wiki and players can understand and calculate the "score to beat" at a given point in the game.
     
  2. Porky12458

    Porky12458

    Joined:
    Apr 19, 2024
    Posts:
    2
    Well, AI solved it. I will post the reply here in case a future lost soul needs this info too.

    I started with the following JSON I got from Unity (click copy on the curve and past it in notepad)

    {
    "curve": {
    "m_Curve": [
    {
    "time": 0.0,
    "value": 0.0035858154296875,
    "inSlope": 0.0,
    "outSlope": 0.0,
    "inWeight": 33333432674408,
    "outWeight": 0.3333333432674408
    },
    {
    "time": 1.0,
    "value": 1.0,
    "inSlope": 2.0,
    "outSlope": 2.0,
    "inWeight": 0.3333333432674408,
    "outWeight": 0.3333333432674408
    }
    ]
    }
    }


    After prompting Copilot a few times and removing the fat from the json this is what I learned:
    time, value, inslope and outslope are important, the rest is fat.

    Here is the AI's verbatim response when asked to interpret this data:


    "The JSON data you provided appears to represent a curve in a 2D space, possibly for an animation or a graph. The m_Curve array contains two points on this curve, each with a set of properties:

    • time: This could represent the x-coordinate or a moment in time for an animation.
    • value: This could represent the y-coordinate or a value at that point in time.
    • inSlope and outSlope: These could represent the slopes of the curve at the point, coming into and going out of the point, respectively. This is often used in animation to smooth transitions.
    • inWeight and outWeight: These could represent the weights for the incoming and outgoing slopes, respectively. This might be used to adjust the influence of the slope on the curve’s shape.
    The first point is at time = 0.0 with a value = 0.0035858154296875, and the second point is at time = 1.0 with a value = 1.0. The inSlope and outSlope for the second point are both 2.0, indicating a linear increase.

    Please note that this is a general interpretation based on common uses of these terms. The exact meaning could vary depending on the specific context or software where this data is used. If you have more specific information about where this data is from, I might be able to provide a more accurate interpretation."

    When asked for a math expression for that, it replied saying it was a Cubic Hermite Spline (obviously, I mean, who doesn't know THAT!)

    This is the reply with the expression


    "The mathematical expression for the curve can be represented using a cubic Hermite spline, which is commonly used for smooth interpolation of values.

    A cubic Hermite spline is defined by its values and first derivatives at the end points of an interval. In this case, the interval is from time = 0.0 to time = 1.0. The values (value) and first derivatives (inSlope and outSlope) at these points are given in the JSON data.

    The general form of a cubic Hermite spline is:

    H(t)=(2t3−3t2+1)y0+(t3−2t2+t)m0+(−2t3+3t2)y1+(t3−t2)m1

    where:

    • t is the normalized time in the interval [0, 1],
    • y_0 and y_1 are the values at the start and end of the interval,
    • m_0 and m_1 are the slopes at the start and end of the interval.
    Substituting the given values into this equation gives:

    H(t)=(2t3−3t2+1)∗0.0035858154296875+(t3−2t2+t)∗0.0+(−2t3+3t2)∗1.0+(t3−t2)∗2.0

    This equation represents the curve defined by the given JSON data. Please note that this is a simplified model and actual curves can be influenced by many other factors."


    I've put the expression in wolfram and it plots the same graphic as unit and when resolved for a value t it gives the same result as .Evaluate in Unity. =)