How to make a character jump?

I am currently making a game in C# using Direct X 9.0. The game remake is Donkey Kong NES. I have nearly everything completed, but I am having problems with the physics of Mario's jump. I have variables declared for the Y and X co-ordinations.

I was wondering if there was a simple method of doing this. I have searched high and low for an answer but the answers I have found are either irrelevant/ or using a different programming language such as XNA.

I currently have a bool variable set to check to see if W has been pressed then that will trigger any code to make him jump. I have been messing around such as.

  if (jump == true)
{

Playerypos -=  vel;
Playerxpos +=  vel;
}


Which didn't work that well. I have searched high and low for an answer and now I am desperate, if someone could point me in the right direction to a simple solution. That would be great. Much appreciated for any responses.

• Answers using a different programming language shouldn't pose a problem. The algorithm is what's of interest, not the specific implementation... unless you're just hoping to copy & paste without understanding anything, which I certainly hope you are not! Being able to read and adapt algorithms from other programming languages is a very good skill to have. – ccxvii Feb 19 '13 at 15:04

First of all, XNA also uses C# too so it's the same programming language. And although the underlying API might have some differences from DirectX, that has nothing to do with jumping, so the same tutorials or answers should apply here. Also, there are numerous ways to implement this, depending on a lot of factors. What I'll describe below is just one of the possibilities.

Basic Physics Requirements

First you need some basic physics variables and calculations on your update loop. In particular, you need position, velocity, and gravity defined, and your update loop should be doing something roughly similar to this (collision detection and response omitted for simplicity):

float positionX, positionY;     // Position of the character
float velocityX, velocityY;     // Velocity of the character
float gravity = 0.5f;           // How strong is gravity

void Update(float time)
{
positionX += velocityX * time;      // Apply horizontal velocity to X position
positionY += velocityY * time;      // Apply vertical velocity to X position
velocityY += gravity * time;        // Apply gravity to vertical velocity
}


Fixed Height Jump

Now the easiest way to implement a jump that always has the same height, no matter how long you press the key, is simply to change the vertical velocity once. This will make the character start rising, while gravity will automatically take care of bringing him down:

void OnJumpKeyPressed()
{
velocityY = -12.0f;   // Give a vertical boost to the players velocity to start jump
}


Important You don't want to be able to start a jump if your character is not on the ground, so you'll need to add a check for that.

Variable Height Jump

But games like Mario and Sonic have a variable jump where the height of the jump depends on how long you press the button down. In this case you'll need to handle both pressing and releasing the jump key. You could add something like:

void OnJumpKeyReleased()
{
if(velocityY < -6.0f)       // If character is still ascending in the jump
velocityY = -6.0f;      // Limit the speed of ascent
}


Tweaking all of these values (i.e. the numbers like 0.5, -12.0f or -6.0f) in the code above you can change the feel of your jump, how high he jumps, how much momentum does he keep even after releasing the jump button, how fast he falls, etc.

Important Don't call these functions every frame. Call them only once when the key is pressed or released. Otherwise your character will fly instead of jump.

Working Example

I have just created a quick example of the techniques described above, which you can try on your browser here (press mouse button to jump, release it midway to control the height of the jump). It's in JS but like I mentioned earlier, the language is irrelevant in this case.

http://jsfiddle.net/LyM87/

• Might as well take the chance to share this amazing article written a few days ago with information related to this topic: The guide to implementing 2D platformers. And also this: Sonic Jumping. – David Gouveia May 25 '12 at 16:51
• The article describes a different way of doing variable jumping than you did in your answer. It calls for suppressing gravity or continuing to add the impulse as the button is held. I usually do what David says in his answer, but is there any thoughts one the article's methods? – Jeff May 25 '12 at 19:32
• @Jeff That piked my attention too, and is exactly why I mentioned that there are multiple ways to implement it. The first article describes two additional ways of handling the same problem, while the way I described is based on the second article. I don't know how they compare to each other, although any of them is pretty easy to implement. In the end, I chose this method because it was easier to explain, and didn't require adding a timer or dealing with multiple impulses of diminishing magnitudes. – David Gouveia May 25 '12 at 19:57
• The answer can vary based on the specific game and how many animations you want to do. Metroid uses a constant speed jump:/descent, but masks it with high quality animation work to make it look smooth and "curved" at the zenith. Many other platformers use variable speed jumps, which require more work to make it work well. – Sean Middleditch May 26 '12 at 19:26
• this was very helpful, 5 years later :) and this fiddle thing is very cool--I should learn JS! – Fluidity May 21 '17 at 1:23

Like in physics. Only at the moment You press jump key and character is on the ground add upwards velocity (it is called impulse), but make character fall by gravity all the time.

If You do not want character to be jumping every time it hits the ground - because You are holding jump key - add jump key is being pressed variable which You clear after jump key is released and set after. Pseudocode:

if jump being key pressed == false && jump key just pressed
jump being key pressed = true

if jump key released
jump being key pressed = false

decrease velocity by gravity
check for collisions // to not penetrate ground ...


if jump key not pressed
jump height = 0            // imaginary height donkey would travel without gravity

if jump key pressed
// going up
if jump height < maximal height donkey might jump
jump height += 5
donkey.y    += 5        //
// going down
else if jump height < 2 * maximal height donkey might jump
jump height += 5
donkey.y    -= 5


How donkey.y will change is up to Your imagination. Velocity should get smaller heigher donkey is in the air to make it look good.