user1017882
user1017882

Reputation:

'Motion easing' effect in C#

I need to increment an integer, but I want to ease the speed at which it's incremented. So say for example I have an int that is equal to 0, I would like this int to get to 100 eventually but increment progressively slower. Does anyone have experience with this?

Upvotes: 0

Views: 1148

Answers (2)

GazTheDestroyer
GazTheDestroyer

Reputation: 21261

hcb's answer works for 100, but a different value would require a different ease value.

A more generalised answer would be to use a sine, which means the ease would be the same no matter what final value you wanted, or however many steps you take.

private void EaseIn(int easeTo)
{
     for (int n = 0; n < easeTo; n++)
     {
          double degrees = (n * 90) / easeTo;
          double easedN =  easeTo * Math.Sin(degrees * (Math.PI / 180));

          Console.WriteLine("Eased n = " + easedN.ToString());
     }
}

Upvotes: 2

hcb
hcb

Reputation: 8357

Like this?:

for (int i = 0; i <=100; i++)
{
    Thread.Sleep(i);
}

Or like this:

float j = 1;
float ease = 0.005;
for (float i = 0; i <=100; i+=j)
{
    j -= ease;
}

Upvotes: 1

Related Questions