HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Calculating distance per interval..

08-26-2007, 05:45 AM#1
Dil999
Im working on a set of knockback functions, which take the distance you want the unit to be knocked back and the duration of the knockback. The problem I'm having is I want the knockbacked unit to realisticaly slow down as the duration increases, but still reach the distance at the correct time.
For example, you give it a duration of 4 and a distance of 1000. What is the formula to, by giving it the duration so far, return how far it will need to be moved?
08-26-2007, 06:38 AM#2
Pyrogasm
So essentially you've just got negative acceleration, so we'll use the formula d = 1/2*a*t*tsolved for a (acceleration):
Collapse JASS:
d = 1/2*a*t*t
2*d = a*t*t
a = 2*d/(t*t)
So in your example you have the duration (t) 4.00 seconds, the timer callback probably runs every 0.03 seconds, and a distance (d) of 1000.00 units. Thus, your acceleration is calculated as a = 2*1000.00/(4.00*4.00), or a = 125.00 units/second. Then, we need to divide by 33.33 (since that's roughly how many times 0.03 goes into 1.00) to get the acceleration per timer interval which would be, in this case, 3.7504.

Last, you need to get your initial speed. Since linear acceleration is just (Final Speed-Initial Speed)/Time, you simply multiply the acceleration/TimerInterval by the total time (4.00 seconds) to get the initial velocity in units/TimerInterval. Thus, it would be 15.0002.

Of course, you then multiply the acceleration by -1.00 such that the object slows down from a fast velocity to rest instead of the other way around.

So you'd do something like this:
Collapse JASS:
function Callback takes nothing returns nothing
    local real X = HoweverYouGetX()
    local real Y = HoweverYouGetY()
    local real Acceleration = HoweverYouGetTheAcceleration()
    local real Velocity = HoweverYouGetTheVelocity()+Acceleration //We add it because it's negative acceleration
    local real Angle //In radians, of course
    local unit U = HoweverYouGetTheUnit()

    if Velocity > 0.00 then
        set Angle = HoweverYouGetTheAngle()
        call HoweverYouStoreReals(Velocity)

        call SetUnitX(U, X+Velocity*Cos(Angle))
        call SetUnitY(U, Y+Velocity*Sin(Angle))
    else
        call HoweverYouCleanUp()
        //Timer pausing/destroying, etc.
    endif

    set U = null
endfunction
And of course storing the appropriate values at the initiation of the knockback.
08-26-2007, 09:50 AM#3
NightBreeze
Pyrogasm's approach works perfectly, I just have 1 little addition to make (which he solved as well, but just as another option. I gathered that you wanted something like this from your first post) :

Quote:
Originally Posted by Dil999


What is the formula to, by giving it the duration so far, return how far it will need to be moved?

Iteration's velocity:
Collapse JASS:
Acceleration = 2*d/(t*t) * -1           //-125.0 in your example
Initial Velocity = t * acceleration       //500 in your example 
Iteration's a = Acceleration * dt
Iteration's Initial Velocity = Initial Velocity * dt

//For this we have 2 options, depending on what variables you want to give the callback function 
//1 (for this option you will have to store the object's velocity every iteration)
Iteration's velocity = Velocity + a
//2 (for this option you will have to store how many times the timer callback function has run)
Iteration's velocity = Initial Velocity + a * dt * timesrun

Then you can update the unit's position with the angle as Pyro so elegantly described.

Maybe this is a redundant post, but since you asked for a formula for the velocity given a distance and a time.. I figured I'd give you this option as well. The only down side is you'll have to keep track of how many times the timer has run with an integer that is incremented by 1 every iteration and then stored.
08-26-2007, 10:06 AM#4
Pyrogasm
I'm curious... is it easy to calculate exponential acceleration?
08-26-2007, 10:38 AM#5
NightBreeze
Let's say the acceleration of an object is increased by i m/s² every second and the starting velocity is 0. This means that instead of

a(t) = c (constant)
v(t) = at

we get

a(t) = i*t
v(t) = the integral of i*t*dt from 0 to t, which is easy to compute since it's a linear equation. v(t) = 0.5it²

Now for an example. Let's say the object's acceleration starts at 0 and increases by 5 every second. The object travels for 10 seconds, how far does it get and how fast is it going at the end?

This means that i = 5.
The acceleration at any time is calculated with a(t) = 5t
The Velocity at any time is calculated with v(t) = 0.5*5*t² = 2.5*t²

Now for the distance:

s = the integral of the speed from 0 to t. For a constant acceleration this means going from:

v = at

to

s = 0.5at².

Now for our increasing acceleration, it means taking the integral of 2.5t² from 0 to 10, which means:

s(t) = 1/3 * 2.5 * t³
s(10) = 1/3 * 2.5 * 10³
s(10) = 2500/3

yay.
08-26-2007, 07:00 PM#6
Dil999
One question, Nightbreeze,
In your first post, what does dt stand for?
08-26-2007, 07:23 PM#7
NightBreeze
delta time, the interval of time :) 0.03 for example
08-26-2007, 10:02 PM#8
Pyrogasm
I see now, NightBreeze. Thanks!
08-26-2007, 10:27 PM#9
PipeDream
What do you mean by exponential acceleration?
08-26-2007, 10:41 PM#10
Pyrogasm
Not... linear...?

Precisely this:
Quote:
Originally Posted by NightBreeze
Let's say the acceleration of an object is increased by i m/s² every second and the starting velocity is 0.
08-26-2007, 10:46 PM#11
PipeDream
That's linear
08-26-2007, 10:48 PM#12
NightBreeze
He means the acceleration as a linear function of the time instead of being constant. The velocity is then increased as a function of t^2, hence the name exponential. It's not actually exponential acceleration :P
08-26-2007, 10:50 PM#13
Pyrogasm
Bah. I'll get the terminology right when I take physics this year. Just pardon me 'till then.
08-26-2007, 10:55 PM#14
PipeDream
Exponential acceleration would mean e^t, which will arise whenever position/velocity/acceleration are related linearly through a spring, viscosity or other worldly phenomenon

t^2 I call quadratic or polynomial