Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Velocity, gravity and bouncing

After this lesson: I can move a thing with velocity and gravity, one step per frame.

60 min lesson2 commandsGame development lab: a game loop, gravity and collisions in the browser

Gravity changes speed, and speed changes position

Velocity is how far, and in which direction, a thing moves on each frame. Gravity is a small number added to the velocity on every frame. Together they make a ball speed up as it falls, and bounce when it lands.

The same two lines of arithmetic move almost everything in a simple game.

By the end of this lesson

Note

I can move a thing with velocity and gravity, one step per frame.

Key words

WordWhat it means
Velocityhow far a thing moves on each frame, and which way
Gravitya number added to the velocity on every frame
Stepone frame of movement worked out with arithmetic
Floorthe y value where a falling thing lands
Bounceturning the velocity round and making it smaller

The cells students fill in are left blank in this preview.

Two lines move a falling thing

On every frame, a falling thing does two steps in this order. First the gravity is added to the speed. Then the speed is added to the position.

speed = speed + gravity, then y = y + speed. In a web page, y grows downwards, so a positive speed means falling.

Worked example: gravity 1, from rest

FrameSpeed (old + 1)Position y (old + speed)
Start00
111
223
336
4410
5515

The cells students fill in are left blank in this preview.

Try it: print a ball's fall

Code: web
<pre id="out"></pre>

Follow one falling star for ten frames

Command
docker exec jupyter python3 steps/fall.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The script follows a falling star

The script reads GRAVITY from game/game.js, which is 0.15. It then does the two lines of arithmetic for ten frames and prints each one.

On frame 1 the speed is 0.15 and y is 0.15. On frame 10 the speed is 1.50 and y is 8.25. Each frame moves further than the one before, which is why a falling star seems to speed up.

Find where the game uses GRAVITY

Command
docker exec jupyter grep -n "GRAVITY" game/game.js

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The constant is set once and used once

The search prints two lines. Line 3 is var GRAVITY = 0.15;, the named number. Line 74 adds it to a star's speed, scaled by the level.

One named number means one place to change. Lowering it makes the game gentler, and raising it makes the stars hard to catch.

A bounce flips and shrinks the speed

When a ball reaches the floor, two things happen. Its y is put back on the floor, so it does not sink in. Its speed is multiplied by a negative number, such as -0.5, so it turns round and loses half its strength.

The lab's game has no bounce, because a star that is missed is simply lost. The rule is the same one that many games use for balls.

Worked example: gravity 1, bounce 0.5

FrameSpeed after the stepPosition y after the stepWhat happened
131391Still falling
1414, then -7105, put back to 100Landed and bounced
15-694Moving up
16-589Slowing down
20-179Almost at the top
21079At the top of the bounce

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think gravity moves a thing directly. It does not. Gravity changes the speed, and the speed changes the position. If gravity moved the position, the ball would fall at a steady pace and never speed up.

About this lab and its source: Game development lab: a game loop, gravity and collisions in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.