-1

I am trying to animate an object's position using a quadratic equation to get a Rapid-Start-Slow-Stop kind of movement.

This is already available in Delphi XE6 but I am using Delphi XE2 and Lazarus and I have created my own animation handling system which works great actually.

I already have a linear formula and a half-sinus formula, but now I want a quadratic formula.

The half-sinus formula does actually move my object rapidly at start and then slows down, but I want a more exaggerated curve, which is very rapid at start and then quickly slows down a lot and finally halts.

Unfotunately I have no code available since it is code that I am looking for.

I have used our dear friend mr Google, but no luck finding any information that I can comprehend.

Basically what is needed is a formula which gives a floating point number rangin from 0 to 1.

I then take the delta of the position of the object that needs to be moved and multiply it by the formula needed.

In my animation system I have two values regarding the animation process:

var CurrentPos, Resolution: single;

The value "CurrentPos" represents the position of the formula. For instance with Sinus I can apply "CurrentPos" as the angle, or the X axis of a plot, and "Resolution" will be the max angle (360 degrees) or the whole plot view in the X axis.

The current formula that I have found so far for the quadratic equation is the following:

formula := (-b+(Sqr(Power10(b, 2)-4*(a*c))))/(2*a);

Where in that formula do I put in the value of "CurrentPos"?

Or is the real question, have I gotten this whole mathematical process completely wrong?

xaid
  • 740
  • 6
  • 18

1 Answers1

1

You want an equation in an unknown x that is:

  1. Quadratic in x.
  2. Has a value of 0 when x = 0.
  3. Has a value of 1 when x = 1.
  4. Varies rapidly when x is close to 0 and does not vary rapidly when x is close to 1.

Your quadratic could therefore be of this form: 1 - (1-x)2.

This has derivative 2 when x = 0 and 0 when x = 1.

You'll need to translate your input variable to be in the range 0 to 1. Once you done that feed it into the above formula as x.

If you want to use different exponents you can simply replace the exponent. For instance a cubic version would be: 1 - (1-x)3.

In code you might write this:

function OutPoly(x: Real, exponent: Integer): Real;
begin
  Result := 1 - IntPower(1-x, exponent);
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I have tried what you are suggesting, but it gives me sort of a diagonal line, there is no curve – xaid Dec 06 '14 at 17:26
  • How can that be? Perhaps you made a mistake. The expression in my answer is quadratic rather than linear. Of course it's possible my attempts to understand your question are no good. You didn't post any code or any maths. – David Heffernan Dec 06 '14 at 17:31
  • well if x is between 0 and 1 and the formula (in code) is as following: f := 1 - Power10((1-x), 2); is that correct? – xaid Dec 06 '14 at 17:32
  • What is Power10 all about? Your expression is linear. You need `1 - Sqr(1-x)`. But if you want OutQuint then you need `1 - IntPower(1-x, 5)`. – David Heffernan Dec 06 '14 at 17:39
  • Isn't Sqr the Sqr root? I thought it was? I changed Power10 to Sqr and now I get a decent curve. Thanks David! – xaid Dec 06 '14 at 17:42
  • You should read the documentation for Power10. Your expression is `1 - 100*(1-x)`. – David Heffernan Dec 06 '14 at 17:43
  • No `Sqr(x)` is `x*x`. You don't need to ask me. This is all documented. – David Heffernan Dec 06 '14 at 17:44