Emulating a parabola in my game for a jump

1.3k Views Asked by At

I am currently having some trouble understanding how to plot a parabola with the x and y coordinates.In my game a player needs to jump from point a to point b and the jump would look something like this:

enter image description here

Now I know that the equation of parabola is: $y=ax^{2}+bx+c$

Now I am not sure what the values of $a$ and $b$ will be. What I currently am trying to do is achieve a corresponding value of $y$ for every $x$. Any suggestions would be appreciated.

Update : I am trying to implement @Demetri solution. Here is how I am doing it

        jumpXstart = CurrentXCordinate;
        jumpXend   = CurrentXCordinate + 120;
        jumpmidpoint = CurrentXCordinate + (120/2); //X cordinate for jump midpoint
        jumpheight = 60;

Then in the frame update I am doing this

        float x = CurrentXCordinate + 1; //This will continuously increment
        float temp =  (x - jumpXstart) * (x - jumpXend);
        float y = (-1 * jumpheight ) * temp;
        y = y / ( (jumpmidpoint - jumpXstart) * (jumpmidpoint - jumpXend));

Now on my first frame I get a negative value of y which is incorrect. Here are the values x = 51 jumpXstart = 50 jumpXend = 170 jumpheight = 60 jumpmidpoint = 110

The above values give a negative value of y . I believe the first value of y while taking off should be positive.

2

There are 2 best solutions below

2
On

If your first point is $(x_0,0)$ and your second point is $(x_1,0)$ then your parabola will look like $y=a(x-x_0)(x-x_1)=ax^2-a(x_0+x_1)x+ax_0x_1$ where $a$ is arbitrary (negative) and then $b=-a(x_0+x_1)$ and $c=ax_0x_1$). How can $a$ still be arbitrary? Well, it depends not only on the gravitation but also on the time the whole jump takes - whihc you did not specify.

1
On

If you know how long your jump should be, and the height of the jump, then the following will suffice:

$$ y= \dfrac{-\text{jump height}}{(M-\text{start})(M-\text{end})} (x-\text{start})(x-\text{end}) $$

Here, M is the midpoint between the jump start point and jump end point.