forrex
forrex

Reputation: 565

Calculate time to decelerate using initial speed and decay

I'm trying to calculate the time it will take a movieclip in Flash to decelerate to zero. The starting speed will vary, but for purpose of example lets say:

Frames Per Second: 30

Speed: 50

Decay: .8 * current speed each frame

onEnterFrame(event:Event):void
{
    movieClip.x += speed;
    speed *= .8;

}

How would I figure out the time in seconds or in total frames it would take to decelerate to zero?

Thanks!

Upvotes: 1

Views: 509

Answers (1)

Alexey Frunze
Alexey Frunze

Reputation: 62106

First of all, what you call acceleration is in fact speed/velocity in pixels per frame.

Starting from the very first frame (i-th) when you start adjusting velocity by a factor of 0.8 you can express the velocity as:

v(i) = v(i-1) * 0.8
and v(0) = 50

You can reexpress v(i) using v(0) as:

v(i) = v(0) * 0.8i

I can think of 2 different stop conditions:

1. v(i) < 1 (meaning: velocity drops below 1 px/frame)
2. v(i) - v(i+1) < 0.1 (meaning: velocity changes by less than 0.1 px between frames)

For the first you get:

v(0) * 0.8i < 1

0.8i < 1 / v(0)

i > log0.8(1 / v(0))

changing the logarithm base using logb(x) = loga(x) / loga(b):

i > ln(1 / v(0)) / ln(0.8)

i > ln(1 / 50) / ln(0.8)

i > 17.531

For the second you get:

v(0)*0.8i - v(0)*0.8i+1 < 0.1

v(0)*0.8i - v(0)*0.8i * 0.8 < 0.1

v(0)*0.8i * (1 - 0.8) < 0.1

0.8i < 0.1 / (v(0) * (1 - 0.8))

i > log0.8(0.1 / (v(0) * (1 - 0.8)))

i > ln(0.1 / (v(0) * (1 - 0.8))) / ln(0.8)

i > ln(0.1 / (50 * (1 - 0.8))) / ln(0.8)

i > 20.638

So, with these numbers you get about 20 frames worth of time till the movement stops. Tweak the numbers as you see fit.

Upvotes: 2

Related Questions