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
256
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top