Basics: Gravity
Watch the movie
Step 1. Make sure you know the basics
Step 2. Create the ball
Step 3. Create the platform
Step 4. Create the variable
Step 5. The master command
Step 6. When the ball isn’t touching the platform…
Step 7. When the ball is touching the platform…
Watch the movie
In the movie below, the ball falls to the ground as if gravity were pulling it. Can you figure out how to simulate gravity in this way?
Step 1. Make sure you know the basics
If you don’t know how to make variables, see Games: Basics: Scrolling Platforms (Step 2)
Step 2. Create the ball and platform
Delete the cat sprite. (Do you know how?) Then create a ball that looks like this.
To get the highlight, use the gradient fill tool.
Step 3. Create the platform
Now create another sprite that’s a platform. Name the sprite platform, and place it at the bottom of the screen. You’ll find a picture of the commands you’ll need below. Be sure the change the numbers so that that platform is at the bottom.
Create a variable named y velocity.
Step 5. The master command
We want the ball to fall until it reaches the platform, and then we want it to bounce upwards. To say this another way, we want the ball’s y position to get smaller and smaller until finally the ball reaches the platform, and then we want its y position to get larger and larger, making the ball bounce upwards. We can use a single, master command to make the ball move in either direction:
We can make the variable y velocity get smaller and smaller when the ball is falling, or larger and larger when the ball is bouncing. Either way, this single command makes the ball behave properly. We’ll use this master command to control the ball’s motion at all times throughout the script.
Before we add more commands, let’s go on for a moment and think about how y velocity will get smaller and smaller when the ball is falling.
Step 6. When the ball isn’t touching the platform…
When the ball isn’t touching the platform, y velocity should get smaller and smaller, like this:
And when the ball bounces upwards, each bounce becomes smaller. The command below creates this behavior. How does it work?
Here are all the commands you need to make the ball fall. Can you figure out how to assemble them? If you assemble them correctly, then when the green flag is clicked your ball will fall to the platform and stay there. (We’ll add the bounce in a moment.)
Step 7. When the ball is touching the platform…
When the ball is touching the platform, we want it to bounce upwards. We can perform this neat trick by simply changing the value of y velocity from a negative to a positive number, like this:
Here are all the commands you’ll need to make the ball bounce when it touches the platform. Can you assemble them?
. . .
That’s it! Your project is complete!
See http://wiki.scratch.mit.edu/wiki/Simulating_Gravity for a more thorough discussion of gravity.









