poliver
Member
GM Version: Studio (all)
Target Platform: Windows and mobile, HTML5
Download: N/A (see code below)
Links: none
Summary: How to calculate gravity and jumpspeed to achieve jumps of desired height and/or duration.
This is a simple tutorial consisting of a code snippet with a walkthrough.
I think a lot of people who pick up gamemaker and start by making platformers use Shaun Spaldings tutorial as a starting point (I did a year or so ago).
In Shauns tutorial he has a Create event of his player object that goes something like this
'vsp' is a vertical velocity which is variable
'jumpsp' is an initial velocity of the jump (when jump is initiated it sets your vertical velocity(vsp) equal that of an initial velocity(jumsp)
and 'gravity' is a contant force applied to your current vsp
everything is fine with this approach and it produces nice parabolic jump. but my main problem with this approach was - i just couldn't tweek the jump to my liking. the relationship between the grav/jumpsp and jumpheight/speed was very blurry. so i thought lets create variables to define max height and jump time and calculate the gravity and jumpsp(initial velocity) from them.
Target Platform: Windows and mobile, HTML5
Download: N/A (see code below)
Links: none
Summary: How to calculate gravity and jumpspeed to achieve jumps of desired height and/or duration.
This is a simple tutorial consisting of a code snippet with a walkthrough.
I think a lot of people who pick up gamemaker and start by making platformers use Shaun Spaldings tutorial as a starting point (I did a year or so ago).
In Shauns tutorial he has a Create event of his player object that goes something like this
Code:
hsp = 0;
vsp = 0;
grav = 1;
jumpsp = 14;
movesp = 3;
'jumpsp' is an initial velocity of the jump (when jump is initiated it sets your vertical velocity(vsp) equal that of an initial velocity(jumsp)
and 'gravity' is a contant force applied to your current vsp
everything is fine with this approach and it produces nice parabolic jump. but my main problem with this approach was - i just couldn't tweek the jump to my liking. the relationship between the grav/jumpsp and jumpheight/speed was very blurry. so i thought lets create variables to define max height and jump time and calculate the gravity and jumpsp(initial velocity) from them.
- to calculate initial velocity(jumpspeed) i used a kinematic equation
distance = ((initial_velocity + final_velocity) / 2) * time- we know - distance[max height we want]
- we know - time we want
- we know - final velocity = 0 (peak of the jump)
first, this equation is one directional so we have to cut our time in half
let's simplify and rearrange
distance = ((initial_velocity + 0) / 2) * half_time
distance = (half_time/2) * initial_velocity
initial_velocity(jumpspeed) = distance / (half_time/2)
since we're not working not with real time but finite frames we need to account for an extra frame (had me stumped for a while)
initial_velocity(jumpspeed) = distance / ((half_time + 1)/2)
- after this calculating gravity is very straightforward
gravity = initial_velocity(jumpspeed) / half_time
And here's just the code if you want to copy paste that in
Code:
hsp = 0;
vsp = 0;
movesp = 3;
max_jump_height = 48; //displacement in pixels
jump_time = 60; //in frames
//calculations
jump_to_peak_time = jump_time / 2;
jumpsp = max_jump_height / ( (jump_to_peak_time + 1) / 2);
grav = jumpsp / jump_to_peak_time;