AceHart
Your Friendly Neighborhood Admin
- Reaction score
- 1,494
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:
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
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
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:
(*)
An example is, of course, not a proof...
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
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:
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
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
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:
(*)
An example is, of course, not a proof...
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