Home · Academy · Robotics & Coding · Programming with Scratch · Simple Physics: Speed, Gravity and Collision

Simple Physics: Speed, Gravity and Collision

Model jumping and collision in Scratch using speed and gravity variables.

LESSON COMPASS

What will you use this page for?

Core idea

We learn how to use a speed variable, gravity and collision detection so that a character in a game falls, jumps and lands on the ground in a realistic way.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Forgetting to change the position If you only change y speed and leave out the change y by ... block, the number changes but the character stays still. Speed is just a number; you have to apply it to the position on every frame. Resetting the speed inside the loop If you accidentally put set y speed to 0 directly…

Next connection

Score and Levels in Game Design: We turn your jumping character into a real game by adding score, lives and levels that get harder.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteClones
ContentStandard lesson · 1,648 words
Last updated

One-sentence summary

We learn how to use a speed variable, gravity and collision detection so that a character in a game falls, jumps and lands on the ground in a realistic way.

Why it matters

Moving a cat in Scratch is easy: you say "move 10 steps" and the cat glides across the stage. But in real games, objects do not drift at a fixed speed. When a ball is thrown up, it slows down, stops and then speeds up as it falls. When a character jumps, it first rises and then comes back down.

To get this natural look, we keep the object's speed as a separate value and change that speed a little on every frame. The same idea is the foundation of jumping games, platform games and throwing games.

The first time I tried this, I moved the character down at a fixed speed, and the motion looked robotic. When I made the speed grow a little on each frame, the fall suddenly looked real. I share those steps below.

Speed: the engine of motion

What is a speed variable?

Position is where a character is. Speed is how much the character changes its position on each frame. Scratch redraws the stage many times per second; let us call each redraw a "frame." If we add a fixed number to the position on every frame, the object moves smoothly.

To do this, we create a new variable in the Variables category. Let us call it x speed. Then, inside a loop, we change the x position by this variable.

Example: a ball that speeds up to the right

The program below moves the ball to the right, slowly at first and then faster and faster, because x speed grows a little on every frame.

when green flag clicked
set x speed to 0
forever
  change x by (x speed)
  change x speed by 0.5

Two different things are happening here:

If speed stayed the same, the ball would slide at a fixed rate. Because we grow the speed on each frame, the ball accelerates. This is where the physics feeling comes from: we change the speed itself, not just the motion.

Gravity and jumping

Gravity lowers the speed on every frame

Gravity is a force that constantly pulls objects down. In Scratch, the y axis points up: if the y position grows, the object rises; if it shrinks, the object drops. To imitate gravity, we keep a y speed variable and make that speed a little lower on every frame. Then we change the y position by this speed.

The rule is two lines:

  1. change y by (y speed) — move the object at its current speed.
  2. change y speed by -0.5 — nudge the speed downward.

Changing it by a negative number makes the speed smaller over time; after a while the speed becomes negative and the object starts to fall.

Example: a character falling under gravity

when green flag clicked
set y speed to 0
forever
  change y by (y speed)
  change y speed by -0.5

If you place the character at the top of the stage and run this program, it falls down, slowly at first and then faster, just like a ball you drop. The magic is in one line: making the speed a little more negative on every frame.

How does a jump work?

A jump is really giving the speed a sudden large upward value. When the space key is pressed, we set y speed to a positive number. Gravity then lowers this speed step by step; the character rises, slows, stops and falls back down.

when space key pressed
set y speed to 8

This block is from the Events category. While the falling program keeps running in the background, this single tap throws the character into the air. Make the 8 bigger and the jump goes higher; make it smaller and the jump is lower.

Collision detection

Touching the ground

The character cannot fall forever; it has to stop somewhere. For this we use the touching? block from the Sensing category. When the character touches the ground (for example a green line sprite), we stop the fall by setting y speed to 0.

when green flag clicked
set y speed to 0
forever
  change y by (y speed)
  if <touching color (ground)?> then
    set y speed to 0
  else
    change y speed by -0.5

Here the if ... then ... else block (Control) makes the decision: if we are touching the ground, reset the speed and skip gravity; if not, keep applying gravity. This way the character settles on the ground and stays there.

Example: stop when hitting a sprite

The same idea works for obstacles. In a platform game we want the character to stop moving when it hits a wall or a box. Suppose the character has an x speed and is moving right:

forever
  change x by (x speed)
  if <touching (Wall) sprite?> then
    change x by ((x speed) * -1)
    set x speed to 0

If the character enters the wall, we first undo the last step (move back by the opposite of x speed) and then set the speed to zero, so the character stops in front of the wall instead of passing through it. Collision detection means asking "are these two objects sharing the same place?" and reacting to the answer.

Mini practice

Build your own jumping character. Try these steps:

  1. Create a variable called y speed.
  2. Draw a horizontal line (a ground sprite) at the bottom of the stage.
  3. Set up the character's fall + land program (like the "stop when hitting a sprite" example above).
  4. Add the block when space key pressed, set y speed to 8.
  5. Click the green flag and press space to make the character jump.

Experiments:

Common mistakes

Forgetting to change the position

If you only change y speed and leave out the change y by ... block, the number changes but the character stays still. Speed is just a number; you have to apply it to the position on every frame.

Resetting the speed inside the loop

If you accidentally put set y speed to 0 directly inside the forever loop with no condition, the character never falls, because the speed is reset every frame. The reset should happen only when touching the ground.

Making gravity positive

If you change y speed by a positive number (for example +0.5), the character flies up instead of down. Gravity pulls down, so the value must be negative.

Checking for collisions too late

If you move the character by a very large step and then check for a collision, the character can sink into the wall. Keep the steps small and check after each one.

Safety note

This lesson runs entirely on the screen, inside Scratch, and involves no physical hazard. Even so, try not to sit at the screen for too long: take a short break every 30–40 minutes, rest your eyes and move around a little. Before sharing your Scratch project, review it with an adult; do not write personal details (address, school, phone) in the comments.

Lesson summary

Check questions

  1. What does a "frame" mean in Scratch, and why is speed applied on every frame?
  2. To imitate gravity, should you increase or decrease y speed each frame? Why?
  3. To make a jump, what do we do to which main variable?
  4. Which block stops the character from falling forever, and which category is it in?
  5. If a character passes through a wall, what are two possible reasons?

Answers

  1. A frame is one redraw of the stage by Scratch; the stage refreshes many times per second. Because we apply speed on every frame, motion looks smooth and we can create speeding up or slowing down over time.
  2. Decrease it. Because the y axis points up, changing the speed in the negative direction pulls the object down, which is the natural effect of gravity.
  3. We set the y speed variable to a positive number (for example set y speed to 8). Gravity then lowers this speed slowly, and the character rises and comes back down.
  4. The touching? block in the Sensing category. When touching the ground, we set y speed to 0 to stop the fall.
  5. (a) The step is too large, so the character enters the wall in a single frame; (b) collision is not being checked, or after a hit the speed is not reset and reversed.

Source and verification note

For “Simple Physics: Speed, Gravity and Collision”, verification focuses on whether the relationship between Speed: the engine of motion and Example: a ball that speeds up to the right remains consistent across examples. Block names are kept consistent with the current core Scratch categories. Project behaviour should be tested separately for start-up, normal play, errors and restarting.

Next lesson

Score and Levels in Game Design: We turn your jumping character into a real game by adding score, lives and levels that get harder.

Start QuizBack to Programming with Scratch
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.