• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Mathematical question (maximum jump height)

S

Sake_v2

Guest
I've thought about a couple of ways of doing this, but I'm not sure they're the best ones, so:
How can I determine the highest amount of pixels that an object can go up?
Considering its vsp is being increased (to fall down) 0.1 every step, and it is set to -2.5 when jump is pressed?
I don't necessarily need the amount of time before it reaches its maximum height, just what that height is
 
I would do a while loop to calculate it. When the jump key is pressed:
Code:
var test_y = y;
var test_vsp = vsp; //-2.5 for example
while(test_vsp<0){ //while going up
    test_y+=test_vsp; //apply vertical speed to y value
    test_vsp+=.1; //apply gravity to vsp
}
jump_height = y-test_y;
test_y will give you the actual y value of the top of the jump.
jump_height will give the height from starting point to top of the jump.
 
S

Sake_v2

Guest
I would do a while loop to calculate it. When the jump key is pressed:
Code:
var test_y = y;
var test_vsp = vsp; //-2.5 for example
while(test_vsp<0){ //while going up
    test_y+=test_vsp; //apply vertical speed to y value
    test_vsp+=.1; //apply gravity to vsp
}
jump_height = y-test_y;
test_y will give you the actual y value of the top of the jump.
jump_height will give the height from starting point to top of the jump.
hi. Jump height should be:

jump_height = -( jump_speed + jump_speed * jump_speed / gravity ) / 2

jump_height will be equal to y_at_top_of_jump - y_at_start_of_jump. So if you are jumping up (toward negative y), then jump_height will be negative.
Thank you both for your answers. I actually thought I had to make a loop like SnotWaffle Studios said, but I'm very surprised it can be calculated in one line of code. Thank you flyingsaucerinvasion, I'm not too sure how that formula works but it does perfectly. <3
 
@Sake_v2

if you want to know where that equation came from, look in the spoiler:
calculating y (offset, not absolute) at time t.
v = initial velocity (in this case our jump speed. Is going to be negative if jumping toward -y)
a = acceleration (in this case gravity. Is going to be positive if falling toward +y)
t = time (counting in steps)

initial velocity component: v*t

acceleration component = a*t*(t+1)/2 //this is just "a" times the sum of (1 + 2 + 3.. + t)

together:

y = v*t + a*t*(t+1)/2 //initial velocity plus acceleration components

In order to determine the offset y at the maximum height, we need to know what t is at the max height. We know that our (vertical) speed will be zero at the peak of our jump. So what we can do is take the equation for our speed "s", at time t:

s = v + a*t //speed = initial velocity + acceleration times time.

rearrange it to solve for time t:

t = (s - v)/a

The speed at our jump peak is zero, so the equation for time simplifies to:

t = -v/a

subsitute t with (-v/a) in our equation for y:

y = v*t + a*t*(t+1)/2

becomes

y = v*(-v/a) + a*(-v/a)*((-v/a)+1)/2

and simplify (lucky for us, it simplifies a lot):

y = -(v + v*v/a)/2
 

Nux

GameMaker Staff
GameMaker Dev.
@flyingsaucerinvasion Alternatively, you could use the equation of motion without a time base where final velocity is zero, giving the equation:
Code:
s = (-u²) / (2a)
This is derived from the original equation for final velocity:
Code:
v² = u² + 2as
As you can see, substituting v = 0 and rearranging you get the previous equation.
Translated into code that would be:
Code:
var jump_height = (-jump_speed*jump_speed)/(2*grav);
EDIT: forgot to add a negative sign to u²
 
@Nux, you've got a different result than me. And I think that is because I'm starting with mothing like this: speed += accelation; position += speed; It makes me think I should stop doing it like that. Although I think it is pretty standard practice for a lot of us.
 
Last edited:
Top