Lesson 2 of 4
Gravity and Jumping
Add one number for vertical speed. The rectangle gets weight, an arc, and a landing.
What you will learn
- Velocity
- Gravity
- Ground collision
- Stopping the double jump
Before you start
The Game Loop. Same two files. `update()` now does more work.
What this stage does
Click the game and press the up arrow or W to jump. The player rises, slows down, falls, and lands on the ground. You cannot jump again until you land.
How gravity works
Jumping is not "move the player up". It is one number that changes on every frame. velocityY is how fast the player is moving up or down right now.
player.velocityY -= gravity; // gravity pulls down, so it takes speed away
player.y += player.velocityY; // move by the current speedA jump sets velocityY to a large positive number, once. Gravity does everything after that. The player rises, slows, stops, and falls. You get the curve for free.
Landing, and not jumping twice
isJumping is a true-or-false value. It stops you jumping again while you are in the air. It is set when you leave the ground and cleared when you land.
// Jump only when we are on the ground
if ((keys["ArrowUp"] || keys["KeyW"]) && !player.isJumping) {
player.velocityY = player.jumpPower;
player.isJumping = true;
}
// ...after gravity has moved the player:
if (player.y <= groundTop) {
player.y = groundTop; // put the feet exactly on the ground
player.velocityY = 0; // stop falling
player.isJumping = false; // you may jump again
}Build it step by step
Add the new numbers
Add
jumpPower,velocityY, andisJumpingto theplayerobject. Addgravityas a constant near the top of the file.groundTopis already there from the last stage.Add gravity only
Add the two gravity lines to
update(). Nothing else. Reload. The player falls straight through the floor and disappears. That is correct for now.Add the floor
Add the ground check. The player now falls and lands. There is still no jump.
Add the jump
Add the jump key check, but leave out the
!player.isJumpingpart at first. Hold the up arrow and you fly. Now add the check and try again. You get one jump, and you must land before the next one.Tune the feel
Change
gravityto 0.2, then to 1.5. ChangejumpPowerto 8, then to 25. Two numbers control how the whole game feels. Spend real time here.
Tune the feel
Two numbers decide how the whole game feels. Try low gravity with a small jump, then high gravity with a big one.
How much speed gravity adds each frame. Low feels like the moon.
The speed the player leaves the ground at.
Try these changes
Make these in the files beside this guide. None of them need anything the lesson has not covered.
- Add a double jump. Count the jumps instead of using true or false.
- Make the jump higher when the key is held for longer.
- Make the player move more slowly in the air than on the ground.
- Squash the player when it lands, by changing `height` for a few frames.
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.
How to teach it
- Run step 2 on its own and let the player fall out of the world. The missing floor is obvious, and the student names it themselves.
- Let the student fly in step 4 before adding
isJumping. A flying player is funny and it makes the flag feel necessary rather than arbitrary. - Draw the arc on the board as a list of
velocityYvalues: 15, 14.4, 13.8, and so on through 0 and into negative numbers. Gravity is one subtraction, repeated. - Step 5 is not filler. Budget ten minutes for it. Tuning two numbers is the first time most students feel like designers rather than typists.