Calculate surface under a curve from an animationcurve

if I have a curve from a public animationcurve in the inspector that is not linear, but something like x², is there a way to calculate the integral of that function between two points set on the x-axis? Currently I feel like the possibilities with the animationcurve in terms of scripting are limited :confused:

This question is a bit old, but in case anyone else runs across this like I did, the solution I went with is to instead compute the piecewise integral, simple solutions for which are older than steam, unlike analytical derivatives for bezier splines (which is what an animationcurve is).

The code I ended up using came from this Stackoverflow answer, which can be applied to an AnimationCurve as such:

    // Integrate area under AnimationCurve between start and end time
    public static float IntegrateCurve(AnimationCurve curve, float startTime, float endTime, int steps)
    {
        return Integrate(curve.Evaluate, startTime, endTime, steps);
    }

    // Integrate function f(x) using the trapezoidal rule between x=x_low..x_high
    public static float Integrate(Func<float, float> f, float x_low, float x_high, int N_steps)
    {
        float h = (x_high - x_low) / N_steps;
        float res = (f(x_low) + f(x_high)) / 2;
        for (int i = 1; i < N_steps; i++)
        {
            res += f(x_low + i * h);
        }
        return h * res;
    }

An animation curve is a set of multiple bezier curves between each control point. See this answer how to calculate the 4 control points of one bezier curve.

A bezier curve has a quite simple polynominal. If you have trouble integrating a cubic bezier curve, have a look at this.

This should be everything you need.

Of course if an appriximation is enough, @Trepidation’s answer would be the most simplest solution.

I also needed to calculate the area under a Unity AnimationCurve. I’m using AnimationCurves to accelerate and decelerate rigid bodies. The animation curves create a nice organic custom feel. But in order to predict where the rigid body will move to or stop at requires knowing the area under the curve.

So I combined @Bunny83’s code with jcoffland’s solution for calculating the integral of a cubic bezier function. After a lot of trial and error, I finally got the code to work!

Hope someone finds it helpful.

public float AreaUnderCurve(AnimationCurve curve, float w, float h)
	{
		float areaUnderCurve = 0f;
		var keys = curve.keys;

		for (int i = 0; i < keys.Length - 1; i++)
		{
			// Calculate the 4 cubic Bezier control points from Unity AnimationCurve (a hermite cubic spline) 
			Keyframe K1 = keys*;*
  •  	Keyframe K2 = keys[i + 1];*
    

_ Vector2 A = new Vector2(K1.time * w, K1.value * h);_
_ Vector2 D = new Vector2(K2.time * w, K2.value * h);_

  •  	float e = (D.x - A.x) / 3.0f;*
    
  •  	float f = h / w;*
    

_ Vector2 B = A + new Vector2(e, e * f * K1.outTangent);_
_ Vector2 C = D + new Vector2(-e, -e * f * K2.inTangent);_

_ /*_
_ * The cubic Bezier curve function looks like this:_
_ *_
_ * f(x) = A(1 - x)^3 + 3B(1 - x)^2 x + 3C(1 - x) x^2 + Dx^3_
_ *_
_ * Where A, B, C and D are the control points and,_
_ * for the purpose of evaluating an instance of the Bezier curve,_
_ * are constants._
_ *_
_ * Multiplying everything out and collecting terms yields the expanded polynomial form:_
_ * f(x) = (-A + 3B -3C + D)x^3 + (3A - 6B + 3C)x^2 + (-3A + 3B)x + A_
_ *_
_ * If we say:_
_ * a = -A + 3B - 3C + D_
_ * b = 3A - 6B + 3C_
_ * c = -3A + 3B_
_ * d = A_
_ *_
_ * Then we have the expanded polynomal:_
_ * f(x) = ax^3 + bx^2 + cx + d_
_ *_
_ * Whos indefinite integral is:_
_ * a/4 x^4 + b/3 x^3 + c/2 x^2 + dx + E_
_ * Where E is a new constant introduced by integration._
_ *_
_ * The indefinite integral of the quadratic Bezier curve is:_
_ * (-A + 3B - 3C + D)/4 x^4 + (A - 2B + C) x^3 + 3/2 (B - A) x^2 + Ax + E_
_ */_

  •  	float a, b, c, d;*
    

_ a = -A.y + 3.0f * B.y - 3.0f * C.y + D.y;_
_ b = 3.0f * A.y - 6.0f * B.y + 3.0f * C.y;_
_ c = -3.0f * A.y + 3.0f * B.y;_

  •  	d = A.y;*
    

_ /*_
_ * a, b, c, d, now contain the y component from the Bezier control points._
_ * In other words - the AnimationCurve Keyframe value * h data!_
_ *_
_ * What about the x component for the Bezier control points - the AnimationCurve_
_ * time data? We will need to evaluate the x component when time = 1._
_ *_
_ * x^4, x^3, X^2, X all equal 1, so we can conveniently drop this coeffiecient._
_ *_
_ * Lastly, for each segment on the AnimationCurve we get the time difference of the_
_ * Keyframes and multiply by w._
_ *_
_ * Iterate through the segments and add up all the areas for_
_ * the total area under the AnimationCurve!_
_ */_

_ float t = (K2.time - K1.time) * w;_

_ float area = ((a / 4.0f) + (b / 3.0f) + (c / 2.0f) + d) * t;_

  •  	areaUnderCurve += area;*
    
  •  }*
    
  •  return areaUnderCurve;*
    
  • }*