Jump to content
  • Advertisement
Sign in to follow this  
kerryhall

Good way to do jump physics in a sidescroller?

This topic is 3213 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have tried using const velocity for gravity and for jumping. If the player is jumping, yvel = -500, then we hit the top of the jump, set to 500. This gives a pleasant amount of control to the player, but does not look very realistic. I have also tried making gravity an acceleration, and using an initial velocity for jumping, the way it works in real life as if the player were fired out of a cannon. This is super realistic, but doesn't afford the same feeling of control as the previous method. I have tried tweaking the jump and grav constants, but without finding any good results. I am now considering a hybrid between those two methods, or perhaps using some sort of lookup table? How have you seen jumping and gravity implemented in side scrollers in a way that affords good control combined with good realism?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
This is super realistic, but doesn't afford the same feeling of control as the previous method.


How'd that be? You mean being able to use the left-right arrows to make the player control the jump?

Share this post


Link to post
Share on other sites
How is the formula you're using to emulate gravity? Have you tried with higher values (for gravity)?

[Edited by - owl on October 30, 2009 11:14:13 PM]

Share this post


Link to post
Share on other sites
You could try to decouple the jump physics into two components. The vertical component is controlled using gravity acceleration and damping, while left-right motion is controlled by player by applying force which translates into acceleration, and add some damping.

F - force
Vy - vertical velocity
Vx - horizontal velocity
g - gravity acceleration (9.8 or 10)
m - player mass
t - time since last update

Pseudocode:
if (arrowLeft) F = -1 else if (arrowRight) F = 1 else F = 0
y = Vy - gt
x = Vx + (F/m)t

cap min and max velocity for y
cap min and max velocity for x

player.position.y += y
player.position.x += x


You should have a constant framerate to use acceleration, otherwise its better to use impulse. Edit: *its better to use momentum (I have read the following comment and realized I have the terms mixed up).

[Edited by - lexn82 on October 31, 2009 2:32:00 AM]

Share this post


Link to post
Share on other sites
whenever i do jumping in side scrollers, i just have a momentum x and momentum y stored on the player.

when they jump, i set the y momentum to some number (you have to tune it based on your game's physics equations) but what that does is push the player into the air as if they jumped, and every frame gravity decreases the y momentum a little bit until they hit the job of the jump and then start coming back down to the earth.

It works well and is very simple so i like it (:

pseudo code below...

a) game loop begins
b) if player presses the jump key and they are on the ground set MomY to -1
c) handle other controls etc
d) momy = momy + gravity
e) playerpos = playerpos + (momx,momy)
f) if player is on ground, set momy to 0
g) goto a

make sense?

there's a lot of game loop and control scheme stuff left out in the above but hopefully you get what i mean (:

Share this post


Link to post
Share on other sites
Quote:
Original post by lexn82
You could try to decouple the jump physics into two components. The vertical component is controlled using gravity acceleration and damping, while left-right motion is controlled by player by applying force which translates into acceleration, and add some damping.

F - force
Vy - vertical velocity
Vx - horizontal velocity
g - gravity acceleration (9.8 or 10)
m - player mass
t - time since last update

Pseudocode:
if (arrowLeft) F = -1 else if (arrowRight) F = 1 else F = 0
y = Vy - gt
x = Vx + (F/m)t

cap min and max velocity for y
cap min and max velocity for x

player.position.y += y
player.position.x += x


You should have a constant framerate to use acceleration, otherwise its better to use impulse. Edit: *its better to use momentum (I have read the following comment and realized I have the terms mixed up).


This is essentially what I am doing, but I didn't think to cap the min velocity for y. I think this is pretty much exactly what I need to do. That was pretty much the issue, that the player seems to move too slow in the y direction at the top of the arc. I will give this a try and report back.

I am not using a constant framerate. But the acceleration is based upon time, so it should be frame independant.

Thanks!

Share this post


Link to post
Share on other sites
Even though your acceleration is based on time, it can still cause problems. This is best illustrated by an example:

Assume you start at x = 0, with v = 0, and a = 2.
Lets compare 3 framers with time = 1 and 1 frame with time = 3. So the total time is the same.

v += v + at
x += x + vt

v0 = 0
x0 = 0

// case with time = 1 and 3 frames
v1 = 0 + 2*1 = 2
x1 = 0 + 2*1 = 2

v2 = 2 + 2*1 = 4
x2 = 2 + 4*1 = 6

v3 = 4 + 2*1 = 6
x2 = 6 + 6*1 = 12

// case with time = 3 and 1 frame
v1 = 0 + 2*3 = 6
x1 = 0 + 6*3 = 18

So with the same time difference, but different framerate you get the same speed. But your position changes much more rapidly if you have a framerate drop. When you have a significant drop in framerates your player is catapulted far and in unexpected way.

[Edited by - lexn82 on October 31, 2009 6:32:45 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!