Snippet Jump parabola

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Yes, I guess there's a least 50 already out there...

I needed one for the Catch them All map.
So I looked around.
Didn't really see one that would be usable here.

So, FWIW, here's my own:


Description:
Code:
A unit "jumps" from... well, 0 to some maximum distance.
It should reach some maximum height in the middle.
Preferably in a somewhat curvy way.
Given a point between 0 and the max distance,
what's the unit's height along the way?

JASS:
function GetParabolaZ takes real x,real d,real h returns real
    return 4 * h * x * (d - x) / (d * d)
endfunction


The unit presumably jumps from 0 to distance d, passes maximum height h,
and x is somewhere between 0 and d as the unit goes along...

Simple enough.
You may stop reading here, and just start jumping :p


Alternatively, for the curious, here's where that thing is coming from:

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 :p
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.

Valid(*).


Final function:
JASS:
function GetParabolaZ takes real x,real d,real h returns real
    return 4 * h * x * (d - x) / (d * d)
endfunction




(*)
An example is, of course, not a proof... :p

But, in the general case:
If x = 0, the expression will, indeed, return 0 (it multiplies by x).
If x = d, the expression will be 0 too since d - x will give 0.
On x = d / 2,
4 * h * (d/2) * (d - d/2) / (d * d)
= 4 * h * (d/2) * (d/2) / (d * d)
= 4 * h * d * d / (2 * 2 * d * d)
= h
q.e.d.


Yours,
AceHart
 
Reaction score
456
You could use a bit more descriptive names for parameters :p.

EDIT://And, oh why? I want to see instantly what a snippet takes and returns for me.
 

Sirroelivan

Gunnerkrigg Court
Reaction score
95
Cool, can be very useful. And the quadratic function explanition that comes with it... you must be getting bored Acehart, if you're willing to explain it. :p
 

Artificial

Without Intelligence
Reaction score
326
It's funny how similar this is to another parabolic function found ar WC3C. (link)
JASS:
function ParabolaZ takes real h, real d, real x returns real
  return (4 * h / d) * (d - x) * (x / d)
endfunction
And actually even the calculations become the same when worked on a bit (or then I must've made some mistake :D):
JASS:
4 * h * x * (d - x) / (d * d) = (4 * h / d) * (d - x) * (x / d)
	      4hx(d - x) / dd = 4h/d * (d - x) * x/d
	   (4hxd - 4hxx) / dd = 4h/d * x/d * (d - x)
	    4hxd/dd - 4hxx/dd = 4hx/dd * (d - x)
	      4hx/d - 4hxx/dd = 4hxd/dd - 4hxx/dd
	      4hx/d - 4hxx/dd = 4hx/d - 4hxx/dd
I believe that means this type of parabola function must be rather good? :p
 

Hatebreeder

So many apples
Reaction score
381
Did I get this right?
The "x" in the snipplett is any number, between 0 and d?
The "d" is distance from A to B?
The "h" is the Maximum high you want to achieve?

Well,
The only thing I don't real understand, is how to use x... Can you elaborate a bit further, how to use the x?
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Time = 0:
Unit at "Point".

Time = 1 (1 unit... for example some periodic timer with 0.3 or so)
Unit at "Point" + x

Time = 2
Unit at "Point" + 2 * x

...

As in, "x" starts at 0, and goes in more or less small steps towards d.
Your unit starts at... some point :p
And, every time unit, you move it to "original starting point" + "one more x".
Until, some day, x reaches d, at which point you stop.

On the way there, you get (and set) the height with that function.
 

Hatebreeder

So many apples
Reaction score
381
Time = 0:
Unit at "Point".

Time = 1 (1 unit... for example some periodic timer with 0.3 or so)
Unit at "Point" + x

Time = 2
Unit at "Point" + 2 * x

...

As in, "x" starts at 0, and goes in more or less small steps towards d.
Your unit starts at... some point :p
And, every time unit, you move it to "original starting point" + "one more x".
Until, some day, x reaches d, at which point you stop.

On the way there, you get (and set) the height with that function.

Oh I see xD
Thanks for the elaboration =)
 

waaaks!

Zinctified
Reaction score
255
distance traveled by the unit (be sure that x must not be greater than the distance
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top