Skip to main content
GameDev.net gamedev.net
🔒 Locked

Platformer Jumping

Started by MrNormS Sep 9, 2006 at 6:30 PM 6 replies 4.8k views
Original Post
MrNormS
MrNormS
How do you get proper jumping as found in such games as the SMB games, N, CaveStory etc. The player is supposed slow down significantly when the player releases the jump key. Should jumping be more fixed, use an array of accelerations or something so the jumping can be tweaked? I've mainly just never known how to get the whole variable height thing to work, so the player doesn't jump the same height every time. Thx.
Nypyren
Nypyren
Character::OnJumpCancelled(){  if (m_jumpActive && m_velocity.y > 0.0f) // in this case +y is up  {    m_velocity.y *= 0.5f;    m_jumpActive = false;  }}
Barius
Barius
Another possibility:
Set some sort of timer or counter when the player jumps. Only start applying gravity when the timer runs out or the player releases the jump button.
JBourrie
JBourrie
My current platform engine uses the following method:

When jump is first pressed, a certain upward force is applied immediately to the player's velocity. This is the "minimum jump force", and the player will never jump lower than this force allows.

For each frame (up to N frames) that the key is held, a smaller upward force is added to the players velocity to increase jump height. After N frames, the jump key is no longer in effect until the next time they touch the ground.

In this system, gravity is always in effect, and the jump velocities are greater than the gravitational pull so that gravity doesn't cancel out the jump. This is good, because it avoids alot of headaches with turning on/off gravity and gives a proper parabola to the jump (since gravity creates a deceleration/acceleration automatically).
Dookie
Dookie
Hi, MrNormS.

I'm currently writing a platform game, and I know what type of jumping you're talking about.

When I started my game, I wrote some 'jumping' code that simply made the vertical velocity of my game character equal a negative number (for example, -256.0) the instant I pushed the 'jump' button. Since the vertical velocity became negative when I hit the 'jump' button, my game character jumped! But my game character always jumped the same height. I also wanted that 'Mario' effect that you're talking about, where holding the jump button would make my game character jump higher.

So to make that happen, I slightly modified my 'jump' code. I added a short timer that starts as soon as I touch the 'jump' button. If I'm holding the 'jump' button, I make my game character's vertical velocity always equal a negative number until either I let go of the 'jump' button or the timer expires. As a result, my game character now jumps high if I hold down the 'jump' button, or he jumps a short jump if I quickly tap the 'jump' button.

Hope that helps!
"The crows seemed to be calling his name, thought Caw"
Barius
Barius
Quote:
Original post by JBourrie
In this system, gravity is always in effect, and the jump velocities are greater than the gravitational pull so that gravity doesn't cancel out the jump. This is good, because it avoids alot of headaches with turning on/off gravity and gives a proper parabola to the jump (since gravity creates a deceleration/acceleration automatically).


Actually, if I understand this correctly, it doesn't give you a parabola - it gives you two parts of different parabolas, or a line and a part of a parabola if the additional upward force equals gravity, in which case it will be the same as not applying gravity. Could you elaborate on the headaches?
JBourrie
JBourrie
Quote:
Actually, if I understand this correctly, it doesn't give you a parabola - it gives you two parts of different parabolas, or a line and a part of a parabola if the additional upward force equals gravity, in which case it will be the same as not applying gravity. Could you elaborate on the headaches?


Sorry, "parabola" was the wrong word. Arc would have been a better word, really it was a description of getting a nice curve to the jump versus the linear spike you would get by hard-setting the velocity for X frames.

The solution of hard-setting the velocity or turning off gravity will give the jump an unnatural feel, because there is no acceleration/deceleration model. To get a more natural feel with that sort of system, you would have to implement your own acceleration/deceleration model when the button is pressed/released. Using an upward force + gravity handles this automatically.

Technically I guess you are right, you could turn off gravity and make the additional upward force numbers smaller, but I would consider dealing with another flag and the special cases that may come up to be an unnecessary headache when just leaving it on will give the same effect with less room for error.
T1Oracle
T1Oracle
The accelleration of a falling object is easy to model.

X = Vi*T+4.9*T*T

currentHeight = jumpVelocity*timeElapsed-4.9*timeElapsed*timeElapsed;

which can be simplified to

currentHeight = (jumpVelocity-4.9*timeElapsed)*timeElapsed;

That should give you the height of the player after they have jumped off the ground at the speed of"jumpVelocity." The actuall physics of the situation is that simple although you do need to convert the scale to fit your game. The scale in that equation is given by the constant 4.9 (9.8 meters/sec/sec divided by 2 = 4.9) which puts these numbers in reference to meters as long as the speed is in meters per second. Anyway for the conversion just multiply by a constant like so.

currentHeight = (jumpVelocity-4.9*timeElapsed)*timeElapsed*CONVERSION_CONSTANT;

If you want the accelleration of gravity to be faster or slower then just adjust the 4.9 value.
Programming since 1995.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.