- Home /
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 :/
Answer by Trepidation · Apr 12, 2017 at 08:54 PM
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;
}
Answer by Bunny83 · Apr 12, 2017 at 08:56 PM
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.
Thank you very much, @Bunny83. The two links you provided helped me out with this very same issue.
$$anonymous$$uch appreciate you sharing your knowledge Bunny83. Very best to you.
Answer by MitchStan · Sep 20, 2017 at 05:58 AM
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[i];
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;
}
I added support for constant tangents:
// Define A and D...
float intervalIntegral; // just renamed area since it also works for negative values
// If this portion of the curve is constant (i.e. either this key has a right tangent constant,
// or the next key has a left tangent constant), compute the integral directly as the signed area of a rectangle
if (float.IsInfinity($$anonymous$$1.outTangent) || float.IsInfinity($$anonymous$$2.inTangent)) {
intervalIntegral = A.y * (D.x - A.x);
}
else {
// computation for non-constant tangents...
intervalIntegral = ((a / 4.0f) + (b / 3.0f) + (c / 2.0f) + d) * t;
}
integral += intervalIntegral;
I added unit tests for all kinds of tangents and they succeeded (with a precision of 3e-7 for time and values in the range of -5 to 5, and not more than 4 points).
Your answer
Follow this Question
Related Questions
3D Grapher - Function input problem 1 Answer
Increment a variable by using an AnimationCurve graph 1 Answer
Problem with mathf function 2 Answers
Find angle between two gameobjects? 1 Answer