Gravity and Bouncing Physics in Scratch
A step-by-step middle-school lesson on simple physics in Scratch: make a ball fall with gravity using a velocity variable, bounce off the floor, lose energy each bounce, and roll with friction. With a worked project and quiz.
Key takeaways
- Gravity is made by steadily subtracting from a 'y velocity' variable each frame
- The ball moves by changing its y position by the velocity every frame
- A bounce flips the velocity from negative to positive when the ball hits the floor
- Multiplying the velocity by less than 1 on each bounce makes the ball lose energy and settle
What is physics in a game?
When a ball drops, speeds up, hits the ground, and bounces a little lower each time before rolling to a stop, that is physics — the rules that govern how things move in the real world. Games feel alive when objects obey these rules. In this lesson you will build a small physics simulation in Scratch: a ball that falls under gravity, bounces off the floor, loses energy with each bounce, and finally settles. It is one of the most satisfying things you can program because it looks so real.
This is a simulation, not a game you control, so it is the perfect place to focus purely on the maths of movement. If Scratch is new to you, start with getting started with Scratch. If you want to turn these ideas into a controllable character afterwards, the lesson on making a platformer in Scratch uses the same gravity trick.
The big idea: position and velocity
Beginners move a sprite by saying "change y by -5" directly, which falls at a constant speed — but real falling speeds up. The secret is to separate two ideas:
- Velocity — how fast the ball is moving vertically right now. We store this in a variable called
y velocity. - Position — where the ball is. Each frame we do
change y by (y velocity).
Gravity is what changes the velocity. Every frame we do change [y velocity] by -1. Because the velocity keeps getting more negative, the ball moves down faster and faster — exactly how falling feels. This split between velocity and position is the foundation of nearly all game physics.
You will need a variable. If those are new to you, see variables and score in Scratch.
Step 1: Set up the ball and the floor
- Delete the cat and add the Ball sprite.
- Paint a floor: click the Stage, open the Backdrops tab, and draw a solid strip of one colour (say brown) along the bottom.
- Make one variable:
y velocity(Variables → Make a Variable).
Step 2: Drop the ball with gravity
Give the ball this script:
when green flag clicked
go to x: 0 y: 150
set [y velocity] to 0
forever
change [y velocity] by -1
change y by (y velocity)
Click the green flag. The ball starts slow and falls faster and faster — but it sails straight through the floor and off the bottom! That is because we have gravity but no bounce yet. Still, notice how natural the acceleration looks: that is the velocity trick at work.
Step 3: Bounce off the floor
Now make the ball bounce when it touches the brown floor. Add a check inside the loop:
forever
change [y velocity] by -1
change y by (y velocity)
if <touching color [brown]?> then
set [y velocity] to (y velocity) * -1
Read the bounce line carefully: set [y velocity] to (y velocity) * -1 flips the velocity's sign. Just before a bounce the velocity is a large negative number (falling fast). Multiplying by -1 makes it a large positive number, so the ball shoots straight back up. Click the green flag — the ball bounces! But it bounces forever to the same height, which is not how real balls behave.
Step 4: Lose energy on each bounce
Real balls do not bounce back to where they started — they lose energy. We copy this by keeping only part of the speed on each bounce:
if <touching color [brown]?> then
set [y velocity] to (y velocity) * -0.8
Now the bounce flips the velocity and shrinks it to 80% (-0.8 instead of -1). Each bounce is a little lower than the last, exactly like a real ball, until it finally settles on the floor. Try -0.6 for a heavy, dead ball or -0.95 for a bouncy super-ball.
Step 5: Stop the ball sinking into the floor
You may notice the ball dips slightly into the floor before bouncing back. To make it bounce cleanly from the surface, push it back up to floor level whenever it bounces. Say the top of your floor is at y = -140:
if <touching color [brown]?> then
set y to -140
set [y velocity] to (y velocity) * -0.8
The set y to -140 plants the ball on the floor's surface before flipping the velocity, so it always bounces from the same line. Adjust -140 to match where your floor actually sits.
Step 6: Add sideways motion and friction
Let's make the ball roll as well as bounce. Add a second variable, x velocity, give it a starting push, and apply friction so it gradually slows:
when green flag clicked
go to x: -180 y: 150
set [y velocity] to 0
set [x velocity] to 6
forever
change [y velocity] by -1
change y by (y velocity)
change x by (x velocity)
set [x velocity] to (x velocity) * 0.99
if on edge, bounce
if <touching color [brown]?> then
set y to -140
set [y velocity] to (y velocity) * -0.8
Now the ball is launched to the right, arcs through the air, bounces off the floor and the side walls, and slowly loses horizontal speed because set [x velocity] to (x velocity) * 0.99 trims a tiny bit of speed each frame — that is friction. The ball ends up rolling gently and coming to rest. You have built a believable little world!
Common mistakes and how to fix them
- The ball falls through the floor. Your floor colour and the
touching colorblock do not match. Use the dropper to grab the exact colour. - The ball never settles. Your bounce multiplier is -1 (no energy loss). Use something like -0.8 so bounces shrink.
- The ball buries itself in the floor. Add
set y to (floor level)in the bounce so it always restarts from the surface.
Reading your script in order to find these problems is called debugging, and every game maker does it.
Try it: make it your own
You have a working physics simulation. Now extend it:
- Drop several balls: use clones, each with its own random starting push, and watch them all bounce.
- Add walls and obstacles: draw shapes in the floor colour and watch the ball bounce off them.
- Make a peg board: scatter round pegs and drop the ball through them, like a pinball.
- Tune the world: change gravity (
-1), bounciness (-0.8) and friction (0.99) to make a moon, a trampoline, or thick mud.
Velocity, gravity and friction are the building blocks of every action game. Once they click, try making a platformer in Scratch to control a character with the same physics. Have fun experimenting! âš½
Quick quiz
Test yourself and earn XP
What does the 'y velocity' variable store?
'y velocity' is the ball's vertical speed. Gravity lowers it, and the ball's y position changes by it each frame.
How is gravity created each frame?
Reducing the velocity a little each frame makes the fall speed up over time, then changing y by that velocity actually moves the ball.
How does the ball bounce off the floor?
A bounce reverses direction: a downward (negative) velocity becomes an upward (positive) one when the ball touches the floor.
Why multiply the velocity by something like 0.8 on each bounce?
Real balls do not bounce back to the same height. Multiplying by 0.8 keeps only 80% of the speed, so bounces shrink and the ball comes to rest.
What is friction in this kind of simulation?
Friction trims the horizontal speed a little each frame, so a moving ball rolls to a stop instead of sliding forever.
FAQ
A platformer focuses on a player you control jumping between platforms. This lesson is about the physics itself, the way a free ball falls, bounces lower each time, and rolls to a stop. The gravity idea is the same velocity trick, but here you build a little simulation rather than a controllable character.
Usually the floor check happens too late, after the ball has already moved deep into the floor. Nudge the ball back up to floor level when it bounces (set its y to the floor's y), then flip the velocity, so it always bounces from the surface rather than from inside it.
Keep exploring
More in Coding