Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by alexanderameye · Oct 20, 2016 at 03:59 PM · functionmathaxisgraphanimationcurve

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 :/

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MitchStan · Sep 08, 2017 at 05:43 AM 0
Share

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.

avatar image
0

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;
     }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image huulong · Feb 24, 2018 at 08:14 PM 0
Share

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;
avatar image huulong · Feb 24, 2018 at 09:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Problem with Clamping Values for Object's Rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges