# Speed of the knockback is <= 0

1. Jul 8, 2009

### wraithseeker

I can explain them by 2 ways, I am making some sort of a game. I am doing a some sort of a knockback but the unit will be flying.

Imagine that the knockback ends only when the speed of the knockback is <= 0 and there is decrement & speed which is user specified.

The unit is moved by a timeframe of 0.03 seconds and gravity = -981. I know it is initially 9.81 but in the game it is calculated as -981.

Every 0.03 seconds untill speed <= 0, speed is constantly reduced by decrement and how would I go about making the unit perform a parabola from point A (initial position of the unit) to point B(final area).

I do not really know how to calculate distance with speed and decrement but is it like

-1 * speed * speed / (2 * -decrement / Timeframe)

And I want to know how can I get the time of the knockback.

???

In another way I could say it like unit A has 3 vectors. Vector position, acceleration and velocity(They get updated every timeframe). Position stores the position of the unit and acceleration is actually deacceleration as it is negative. Velocity is just initial speed of the unit and gets decreased every timeframe by acceleration.

Velocity vector adds the acceleration and then Position vector adds the velocity every timeframe. The knockback would end when the dotproduct of velocity and acceleration >= 0.

Distance used to calculate is the same as the following one above.

So How would I go about doing a perfect parabola?

2. Jul 11, 2009

### Staff: Mentor

Re: Parabola

This is possibly a stupid question, but... what is a "knockback"?

3. Jul 11, 2009

### rcgldr

Re: Parabola

I assume this is like an angled trampoline for a ball. The USA name for this is a pitchback:

http://www.sportsworldwholesalers.com/2770fs.html [Broken]

Using parametric equations:

Position:

Px(t) = Vx t + Px0
Py(t) = 1/2 Ay t2 + Vy0 t + Py0

Velocities:

Vx(t) = Vx
Vy(t) = Ay t + Vy0

Accelerations:

Ax(t) = 0
Ay(t) = -981

In the classical numerical integration case, you only know the equations for acceleration for a given set of velocities and positions, but this is a case where the equations for velocities and position are also known.

The acceleration equations, combined with initial velocities and positions, are what you need to create a parbola using numerical integration. Since the integrals of acceleration and velocities are known equations, you can use these to confirm your method of numerical integration

In cases like this one, where acceleration is constant or can be calculated based on time without knowing velocities or position, an averaging method can be used if the steps are small enough:

Vn+1 = Vn + 1/2 (An+An+1) Δ t

In this case, since acceleration is a constant, you just end up with the known equations for velocities. Position calculations become:

Pn+1 = Pn + 1/2 (Vn+Vn+1) Δ t

Again, because acceleration is a constant, position calculations will also end up as the known equations for positions. Post #8 shows this for Py.

Numerical_intergration_wiki.htm

Predictor-corrector_wiki.htm

Runge_Kutta_wiki.htm

A more classic example of numerical intergration produces a sine or cosine wave using:

A = -P

Along with initial velocity V0 and position P0.

Last edited by a moderator: May 4, 2017
4. Jul 11, 2009

### wraithseeker

Re: Parabola

is T time or is T timeframe?

initial position z = the height of the object
initial acceleration = -981/2

in the timeframe function itself (TIME = TIMEFRAME)

velocity.z = GRAVITY + (TIME*TIME) + acceleration.z* TIME
position.z = position.z + velocity.z

5. Jul 11, 2009

### rcgldr

Re: Parabola

T is elapsed time, Δt is time step. I don't know what you mean by time frame.

6. Jul 11, 2009

### wraithseeker

Re: Parabola

I'm doing some sort of programming and I have to move a object by a timeframe which lets say is 0.03.

velocity.z = velocity.z + acceleration.z * (timeperiod*timeperiod)
position.z = position.z + velocity.z * timeframe
timeperiod = timeperiod + timeframe

Here are my current calculations, are they correct?

I don't know why did you multiply initial velocity by the timeperiod.

7. Jul 12, 2009

### rcgldr

Re: Parabola

first one should be:

velocity.z = velocity.z + acceleration.z * (timeperiod)

That's part of the formula for position. (note - I editted my previous post to clean up the names in the formulas.)

If acceleration is constant:

Δt = t1 - t0

initial velocity = Vy0
final velocity = Vy1= Vy0 + Ay Δt
average velocity = 1/2 (Vy0 + Vy1) = 1/2 (Vy0 + (Vy0 + Ay Δt)) = Vy0 + 1/2 Ay Δt

initial position = Py0
final position = Py0 + (average velocity) Δt = Py0 + (Vy0 + 1/2 Ay Δt) Δt = Py0 + Vy0 Δt + 1/2 Ay Δt2

Last edited: Jul 12, 2009
8. Jul 15, 2009

### wraithseeker

Re: Parabola

I don't really get some parts understood but I think I got some part of it right.

Object A

velocity = vx,vy,vz (any numbers will do)
acceleration = ax,ay,az ( any number will do except az which is gravity*0.5 like you said)
distance = the distance covered by the ball
time = distance/(speed/2) (Finding the distance covered by the ball when thrown)

Position vector is available but not random numbers.

Current calculations in the timeframe are as followed

vx = vx + ax
vy= vy + ay
vz = vz + az * timeframe
px = px + vx * timeframe
py = py + vy * timeframe
pz = pz + vz * timeframe

If you do not know what the variables mean, they are;

velocity vector = vx,vy,vz
position vector = px,py,pz
acceleration vector = ax,ay,az.

Final velocity are all 0.

I don't understand much of why are you using average velocity so I posted this. To sum it up, how would I do a parabola if velocity z can be inputted by any number? Setting it higher or lower makes it look stupid.

Last edited: Jul 16, 2009
9. Jul 16, 2009

### rcgldr

Re: Parabola

Say a vehicle is experiencing constant acceleration. At time t0 the vehicle is moving at 30 kph, one second later, at time t1, the vehicle is moving at 50kph. How much distance (position) the vehicle travel during this time?

To solve this type of problem you use distance = average velocity x time.

For numerical integration, one simple method is to use linear interpolation for each interval, change in position = "average" velocity x time step, this the equivalent of "trapezoidal" rule (for finding area under a curve).

Last edited: Jul 16, 2009
10. Jul 17, 2009

### wraithseeker

Re: Parabola

Do I do them for the x y z coordinates or just the z coordinates?

Which should I use?

Or

I am abit confused by those 2 statements.

11. Jul 17, 2009

### rcgldr

Re: Parabola

You can do them for all coordinates, but velocity only changes where there is acceleration, and in your example acceleration only occurs on the z axis. Calculating the average velocity when it's not changing still works. If the velocity is 30kph at the start and 30kph at the end, than average velocity = 1/2 (30 + 30) = 30, so might as well be consistent and use the same method for x, y, and z, while noting that acceleration only occurs on the z axis.

The initial speed of the vehicle is 30 kph, the final speed is 50 kph, the average speed = 1/2 (30 + 50) = 40 kph.

http://en.wikipedia.org/wiki/Trapezoidal_rule

In your case, we can calculate velocity based on elapsed time, so given Vz0, Vz1 can be calculated via Vz1 = Vz0 Az x Δt. The "a" used in the wiki article would be Vz0, and the "b" would be Vz1.

Note that this type of numerical integration only works when acceleration is function of time. If acceleration is a function of velocity and position, then standard numerical integration methods are required. For a simplified aerodyamic drag equation, A = -V2 is a better example, although it's simple enough to integrate directly. I already mentioned A = -P which generates a sine wave as another example.

Last edited: Jul 17, 2009
12. Jul 17, 2009

### rcgldr

Re: Parabola

Here is an example of a crude "predictor corrector" algorithm. To simplify things, I'll use an example that where only one level of integration is required, such as A = -c V2 where c is some constant.

Initial state V0

Time step = Δt

A0 = -c V02

Make first guess assuming A is constant:
Ag 1 = A0

Calculate a velocity guess based on average acceleration
Vg 1 = V0 + 1/2 (A0 + Ag 1) Δt

Make a new guess for A based on Vg1
Ag 2 = A(Vg 1) = -c Vg 12

Calculate another velocity guess based on average acceleration
Vg 2 = V0 + 1/2 (A0 + Ag 2) Δt

If Δt is small enough that an inflection point isn't an issue, this process will converge, so repeat this process:
Ag n+1 = A(Vg n)
Vg n+1 = V0 + 1/2 (A0 + Ag n+1) Δt

Until | Ag n+1 - Ag n | < some tolerance factor.

You can put a limit on this, if | Ag n+1 - Ag n | is not less than the tolerance factor after some specific number of steps, say 16, then decrease Δt and repeat the step. If convergence occurs in less than say 4 steps, increase Δt.

Then you start a new step using

A1 = Ag n+1
V1 = Vg n+1

This is a crude process but most of the methods require generating a few initial steps before the iteration (looping) process can be eliminated. This iterative process could also be used with the better algorithms listed in the links above for each step.

Last edited: Jul 17, 2009
13. Jul 18, 2009

### wraithseeker

Re: Parabola

I tried to use this one on your post above

But it ended up backfiring.

initialization before throwing
s = speed

Inside Timeframe function

set position.z = position.z + s*time+0.5*GRAVITY*(time*time)
set time = time - timeframe

14. Jul 18, 2009

### rcgldr

Re: Parabola

s = initial speed.z

set position.z = position.z + s*timeframe+0.5*gravity*(timeframe*timeframe)
set time = time + timeframe

15. Jul 18, 2009

### wraithseeker

Re: Parabola

Yep, s is initial speed z, I probably didn't get it clear for you sorry.

time is actually the full time for the thrown ball but now edited to be 0 at start.

position.z = position.z + s*timeframe+0.5*GRAVITY*(timeframe*timeframe)
time = time + timeframe

So how can time fit in now? I followed your example above and objects launched in the air never gets down.

16. Jul 19, 2009

### rcgldr

Re: Parabola

Note that gravity is negative. It's -9.80665 meters/second2 or -32.174 feet / second2.

17. Jul 19, 2009

### wraithseeker

Re: Parabola

Gravity is -981 since it's converted to the game's units.

What about that time variable that gets increased in the timeframe function?

18. Jul 19, 2009

### rcgldr

Re: Parabola

The time variable isn't being used in the other equations. You'd might use it to make a graph of position or velocity versus time.

19. Jul 20, 2009

### wraithseeker

Re: Parabola

Well that was actually what I was doing all along. timeframe was timestep actually, I think you misunderstood.

Right now I am using a parbolic equation that calculates things and allows people to specify a maxheight but there will no longer be any gravity.

function ParabolaZ2_v2 takes real y0, real y1, real h, real d, real x returns real
return ((4 * h / d) * (d - x) + y1 - y0) * (x / d) + y0
endfunction

y0 = initial z coordinate of object
y1 = z coordinate at end of the ball
h = maxheight
d = distance
x= distance travelled

It makes the thrown ball drop exactly when the velocity is 0 but the old one doesn't and I was wondering if it was possible to do it with physics.

20. Jul 20, 2009

### rcgldr

Re: Parabola

I understood that timeframe was timestep or delta time. I don't understand why there is a parabola if there's no gravity. I don't understand what your parabola function is trying to calculate.

21. Jul 20, 2009

### wraithseeker

Re: Parabola

It was slowly done from a quadratic equation.

The general parabola function looks something like this:
f(x) = a * x^2 + b * x + c

Obviously, it's unusable.
Well, given sufficient motivation, it will most likely work out nicely.
Still, way too many constants to deal with... let's look for something simpler.

We want to determine y, given the current x, between 0 and some max distance, such that y is heighest "in the middle".
Let's call that distance d and the height h.

It follows that:
On x = 0, y will be 0 too, that's the starting point.
Same when x = d, the max distance we want here, y = 0 too in that case. We have landed back on the ground.
And, in the middle, i.e. when x = d / 2, we want the heighest point, i.e. y = h.

Given that d / 2 is a point of interest, where the function starts to "turn around",
a good starting point for a formula is
x - d/2

We also want this to be curvy, so let's add some square to that:
(x - d/2)^2

Additionally, that also makes sure we get the same result for x between 0 to d/2 and x between d/2 to d.
Yes, math can be all the fun really...

That needs some scaling factor too:
a * (x - d/2)^2

Why does it need one?
Because we want to add the max height here.
Multiplying that expression by a constant is a good way to get proportionally smaller or higher values while still being simple to calculate.

When x = d / 2 that expression will be 0 and we want y to be h at that point.

We end up with:
y = a * (x - d/2)^2 + h

Is that even a valid formula for a parabola?

Well, yes, it is.
That's the so called "standard form".
(For details, see "Forms of ...": http://en.wikipedia.org/wiki/Quadratic_function)

Makes all the sense until here
And, yes, we might just as well have started there... but, hey, where's the fun in that?

a * (x - d/2)^2 + h = y

Looks good.
We know x, d and h. y will be calculated.

What's "a" though?
No idea...

But, we know that y = 0 if x is either 0 or d.

Let's plug that in (x = d and y = 0):

If x = d, then (x - d/2)^2 = (d - d/2)^2 = (d/2)^2 = d^2 / 4

We get,
a * d^2 / 4 + h = 0
a * d^2 + 4 * h = 0
a = -4 * h / d^2

Back to the original "standard form":
y = h - (x - d/2)^2 * 4 * h / d^2

Simple test, assuming that d = 10, h = 3:
y = 3 - (x - 5)^2 * 12 / 100
With x = 0: 3 - 25 * 12 / 100 = 3 - 12 / 4 = 3 - 3 = 0 q.e.d.
With x = d = 10: 3 - (10 - 5)^2 * 12 / 100 = 3 - 25 * 12 / 100 = 0 q.e.d.
With x = d / 2 = 5: 3 - 0 = 3 = h q.e.d.

Holds so far.
And looks like a usable formula already.

Now, there's still plenty of divisions and squares there.
With any luck, something simpler will show up if we try long enough...

(It's mostly long because I did it in really small steps)
y = h - (x - d/2)^2 * 4 * h / d^2
= h - (x^2 - 2 * x * d / 2 + d^2 / 4) * 4 * h / d^2
= h - (x^2 - x * d + d^2 / 4) * 4 * h / d^2
= h - x^2 * 4 * h / d^2 + x * d * 4 * h / d^2 - 4 * h * d^2 / 4d^2
= h - x^2 * 4 * h / d^2 + 4 * h * x / d - h
= 4 * h * x / d - 4 * h * x^2 / d^2
= 4 * h * x * d / d^2 - 4 * h * x^2 / d^2
= (4 * h * x * d - 4 * h * x^2) / d^2
= 4 * h * x * (d - x) / d^2

Want shorter?
y = h - (x - d/2)^2 * 4 * h / d^2
= h - 4 * h * x^2 / d^2 - 4 * h * (d/2)^2 / d^2 + 4 * h * 2 * x * (d/2) / d^2
= h - 4 * h * x^2 / d^2 - h + 4 * h * x / d
= 4 * h * x * (1/d - x /d^2)
= 4 * h * x * (d - x) / d^2

Result:
y = 4 * h * x * (d - x) / (d * d)

One division only, the rest a couple multiplications and one subtract.
Looks good.

Same test as before, d = 10, h = 3:
y = 4 * 3 * x * (10 - x) / 100
y = 12 * x * (10 - x) / 100

x = 0 => y = 0
x = 10 <=> 10 - x = 0 => y = 0
x = d / 2 = 10 / 2 = 5 <=> y = 12 * 5 * 5 / 100 = 12 * 25 / 100 = 12 / 4 = 3 = h q.e.d.

22. Jul 20, 2009

### rcgldr

Re: Parabola

What I meant is what physical property is it trying to emulate? Previously you were talking about a parabolic path due to gravity for the knockback, but now you're generalizing it to the "standard form". I don't understand the goal you're trying to achieve here.

Initially, I though it was meant as a simple means to test an algorithm for numerical integration, but now you're talking about a parabolic function with no time factor, just y versus x, instead of y and x versus time.

23. Jul 20, 2009

### wraithseeker

Re: Parabola

It returns the height needed for the timestep, it's hard to explain, I didn't make that equation.

24. Jul 20, 2009

### rcgldr

Re: Parabola

Using the math you started with, then you just need to assign an iniitial vertical velocity:

Vz0 = +sqrt(2 x (-Az) x height)

Vz0 = +sqrt(2 x 981 x height)

The time it takes to reach the peak height with initial Vz0 is the time it takes for Vz to equal zero. (Az = -981):

Vz = Vz0 + Az tp = 0
tp = Vz0/(-Az) = sqrt(2 x 981 x height) / 981 = sqrt(2 x height / 981)