| 08-26-2007, 05:45 AM | #1 |
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 |
So essentially you've just got negative acceleration, so we'll use the formula d = 1/2*a*t*tsolved for a (acceleration): JASS:d = 1/2*a*t*t 2*d = a*t*t a = 2*d/(t*t) 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: 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 |
| 08-26-2007, 09:50 AM | #3 | |
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:
Iteration's velocity: 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 |
I'm curious... is it easy to calculate exponential acceleration? |
| 08-26-2007, 10:38 AM | #5 |
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 |
One question, Nightbreeze, In your first post, what does dt stand for? |
| 08-26-2007, 07:23 PM | #7 |
delta time, the interval of time :) 0.03 for example |
| 08-26-2007, 10:02 PM | #8 |
I see now, NightBreeze. Thanks! |
| 08-26-2007, 10:27 PM | #9 |
What do you mean by exponential acceleration? |
| 08-26-2007, 10:41 PM | #10 | |
Not... linear...? Precisely this: Quote:
|
| 08-26-2007, 10:46 PM | #11 |
That's linear |
| 08-26-2007, 10:48 PM | #12 |
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 |
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 |
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 |
