The Physics of Flight: Coding a Gravity Engine in Scratch
Lesson Overview
Target Age: 13 (7th Grade)
Subject: Computer Science / Physics
Duration: 60–90 minutes
Learning Objectives: By the end of this lesson, the student will be able to:
- Define "Acceleration due to Gravity" and explain how it differs from constant movement.
- Create and implement Variables to track vertical velocity ($y$-velocity).
- Code a "collision detection" system to stop a character from falling through a floor.
- Modify code to simulate different planetary gravities (e.g., Earth vs. Moon).
Materials Needed
- Computer or Laptop with internet access
- A free Scratch account
- A notebook or digital document for brainstorming "Game Rules"
- Optional: A small ball (like a tennis ball) for a real-world physics demonstration
1. Introduction: The "Invisible Hand" (The Hook)
The Scenario: Think about your favorite platformer games (like Mario, Celeste, or Hollow Knight). When the character jumps, they don't just move up at a constant speed and then stop. They slow down as they reach the peak of their jump, and then they speed up as they fall back to earth. This is called acceleration.
The Question: If you were building a world from scratch, how would you tell a computer that "down" exists? How do you make a character feel heavy instead of like they are floating in space?
Real-World Connection: On Earth, gravity pulls everything down at the same rate. In coding, we have to build that "pull" ourselves using math.
2. Content & Practice: The Gradual Release Model
Phase 1: "I Do" - Understanding $y$-Velocity (15 mins)
In Scratch, the "Change y by [number]" block moves a sprite a set distance. But gravity isn't a set distance; it's a changing speed. We need a Variable called y_velocity.
- Concept: Think of
y_velocityas the "speedometer" for vertical movement. - If
y_velocityis 10, the sprite goes up fast. - If
y_velocityis -10, the sprite falls fast. - The Logic: Gravity is a constant "subtraction" from that speed. We will tell Scratch: "Every millisecond, make the y_velocity slightly smaller."
Phase 2: "We Do" - Building the Engine (20 mins)
Follow these steps to build the core script together:
- Create a Variable: Click the Variables category and make one called
y_velocity(for this sprite only). - Apply Gravity: Create a script:
When Green Flag Clicked -> Forever -> [Change y by (y_velocity)] AND [Change (y_velocity) by (-1)]. - The Result: Observe how the sprite zooms off the bottom of the screen. Why? Because the velocity keeps getting more negative forever!
- Collision Detection: Add a second sprite (a simple flat rectangle) and name it "Floor."
- The Stop Rule: Modify the script:
If touching "Floor", then [Set y_velocity to 0].
Phase 3: "You Do" - The Jump & Polish Challenge (30 mins)
Now it's your turn to make it a playable game. Your mission is to implement the following features independently:
- The Jump: Create a script that says: "If the Space Key is pressed AND touching Floor, set
y_velocityto 15." (Why do we need the "touching floor" part? What happens if you remove it?) - The Buffer: Sometimes the sprite gets "stuck" in the floor. Add a block that moves the sprite up by 1 pixel until it is no longer touching the floor when it hits the ground.
- Level Design: Add three more platforms. Does your gravity engine work on all of them?
3. Conclusion: Summary & Reflection
Recap: We learned that gravity in coding isn't just moving down; it's the constant adjustment of a velocity variable. We used Conditionals (If-Then statements) to make sure our character didn't fall through the earth.
The "Tell Me" Moment:
- How would you make the character feel like they are on the Moon? (Hint: Look at the "Change y_velocity by" block).
- What happens if you set the gravity to a positive number like +1?
Success Criteria
Will has successfully completed the lesson if:
- The sprite falls and speeds up as it goes.
- The sprite stops perfectly on the "Floor" without sinking.
- The sprite can only jump when it is actually touching the floor (no infinite air-jumping!).
- Will can explain what a "Variable" is in his own words.
Differentiation & Extensions
- Scaffolding (For help): If the sprite gets stuck in the floor, use the "Go to x: [ ] y: [ ]" block to reset the character at the top of the screen to test again.
- Extension (Advanced):
- Terminal Velocity: Add a script that prevents the
y_velocityfrom ever going lower than -20 (simulating air resistance). - Variable Gravity: Create a "Planet" variable. If Planet = Mars, set gravity to -0.4. If Planet = Earth, set it to -1.
- Terminal Velocity: Add a script that prevents the
Assessment
Formative: Observe if the student identifies why the sprite is "floating" or "sinking" during the "We Do" phase and can suggest a fix.
Summative: The final Scratch project. If the character can jump and land realistically on multiple platforms, the student has mastered the logic of a basic physics engine.