Diff. Eq. and TDS

Posted by Shork on April 7, 2007, 1:45 a.m.

I have tried making top down and isometric games before, and was never able to get a good looking jump animation. So today, I actually did math involving some easy calculus. Here it is:

jumping objects follow a parabolic path. Parabolas are described by Y=AX^2+BX+C.

If Y = height in pixels and X = steps, then we know that at X = 0 steps, Y = 0 pixels becuase the jump is just started. Therefore, C = 0, so the above equation simplifies to Y=AX^2+BX.

The acceleration due to gravity is the first derivite of the position equation over time. so d/dt(AX^2+BX) = 2AX+B.

The time where the height of the jump is at it's maximum is also the time when the derivitive equals zero, so setting 2AX+B=0 and rearranging yields

X=B/(2A).

Since the maximum height occurs at the midpoint of the time, multipyling the above equation by two yeilds the total time of the jump. X=2(B/(2A))=B/A.

Returning to the parabola, Y=AX^2+BX, and using the equation for time at max height, X=B/(2A), we can get Y=A(B/(2A))^2+B(B/(2A)), which simplifies to Y=B/(4A)+(B^2)/(2A).

We know have two unkowns, A and B, and two equations, Y=B/(4A)+(B^2)/(2A) and X=B/A. You must know determine what you want the max height of the jump to be, and the total length of time for the jump. Remember, this is in steps, that will be important later.

Substitute the max height for Y and the time for X. By rearrangement, Y=B/(4A)+(B^2)/(2A) becomes

Y=(1/4)(B/A)+(B/2)(B/A). The B/A term is simply X, so just replace it with your choice for the time and get

Y=(X/4)+(BX/2).

This equation know has only one unknown, B. Rearrange to get B=2((Y/X)-(1/4)).

Now you have B in terms of known values, so just plug in the values and solve for B. Rearrange the X=B/A to get A= B/X and plug in the known value for B to get A.

Now A and B are both known, so just plug into the original Y=AX^2+BX equation.

There you have it, your jump equation! Now it must be implemented into GML for your game.

First, make some variables in the create event so that they can be used later, lets call the jump height Y JY and the jump step X JX. Make a switch too, call it Jumping. Make all three equal to zero.

Now make an event to start the jump, most likely a press key type event. It should start by checking if Jumping = 0, so that you don't jump during a jump. Then if Jumping = 0, set Jumping to 1 and JX to 0.

Now make a step event and add this code:

if Jumping = 1{
     JX += 1
     JY = -'X'*(A*power((JX/'n'),2)-B*(JX/'n'))
     if JX = 'X'*'n'{
          Jumping = 0
          JX = 0
          }
     }

NOTE: 'X' is the total jump time you chose in the math part above, but X alone will not work. 'n' is a correction factor that divides the total steps for the jump so that the equation works. For example if you chose X as 5 and total steps as 30, then n = 30/5 = 6.

Now to draw the jump. Make a draw event and use the draw sprite code of your choice with the appropriate sprite and image indeces, but instead of just y in the y term, use y-JY. This will move the sprite up and down as the JX increases in the step event, making the character look like it's jumping.

Anyway, hope that made sense and works for you. I just made it work for me, so it should work…. this might make a good example, but then I'd have to comment it and that takes work… oh well…

Comments

Xxypher 17 years, 7 months ago

it doesnt have to be that complex :P

Shork 17 years, 7 months ago

Right, I tried using an absolute value function to get the jump to work, but it looked triangly, so I wanted a better one.

Xxypher 17 years, 7 months ago

oh i get it

flashback 17 years, 7 months ago

I didn't need to read that right after I woke up >.<