Category Archives: Teaching kids to code

Modelling Gravity in Scratch

With school holidays upon us, and one week until the next CoderDojo, I thought I’d set a Scratch challenge!

I thought we could create a clone of a game I played and loved when I was 12 – Thrust!

Here’s a video of this awesome game from 1986 – although by modern standards, it’s a bit basic.¬†

(By the way, here’s a JavaScript clone of the game ūüôā¬†http://joncom.be/experiments/thrust/play/)

Now the physics going on in this game are a little complex to start with, so maybe we should start with something a bit simpler – maybe… [delves into computer game history…. and picks out…] Tanks!¬†

This might not look like much, but it will get us some of the basics of physics.¬†And show it to your parents, and they might get all nostalgic ūüôā

Let’s get started!

So how do we start modelling this kind of physics in Scratch? There are lots of ways, but let’s start with a simple approach – basically when something falls, it accelerates toward the ground, so it will start at 0 kph, and then speed up, 1 kph, 2kph, 3kph, etc. In real life, it will be travelling around 32kph after one second of free-fall. We won’t bother about stuff like terminal velocity yet (that’s what stops a parachutist from carrying on accelerating much past 200kph, due to air resistance).

We can model this by using a variable, let’s call it vy¬†(for velocity y – you can call it what you like),¬†¬†and set it to zero. Fire up Scratch and start a new project at http://scratch.mit.edu/projects/editor/, select the¬†data Scripts palette, and click¬†Make a variable. Give the variable the name vy, choose¬†For this sprite only, and click OK.

Creating new variable

Creating new variable

Insert a When green flag clicked block from the events palette, and under it we’ll set y = 0¬†(from the Motion palette,¬†and set vy =0 from the¬†data¬†palette. This means when the green flag is clicked, we’ll set the y position (or height) of our cat sprite to zero (this is in the middle of the screen – more on this later), and we set our y velocity variable to zero – e.g. we’re staying still.

Then we’ll create a forever block from the control palette, and inside that block, add a¬†change¬†vy¬†by -0.1 from the data¬†palette. Then we’ll grab a change y by¬†block from the¬†motion¬†palette, and add the¬†vy variable from the¬†data palette. This means we’re decreasing our velocity (increasing our speed downwards), and moving the sprite based on our velocity.

First draft of code

First draft of code

Press the Green flag to give it a go. Try changing the value in the change vy by block Рwhat happens?

Here’s a copy of the code:¬†http://scratch.mit.edu/projects/11417057/

But that’s not really what happens when things fall, is it? Things bounce when they hit the ground. So¬†let’s detect when we hit the ground… we need a few new blocks here:

  • an if block from the control palette
  • two less than¬†(<) blocks from the operators palette
  • an and block from the operators palette
  • the y position block from the motion palette
  • and a vy variable from the data palette

Drag the if block onto your canvas, and insert the and block. In the left part of the and block, insert one of the less than operators, and inside this, insert the y positionin the left hand side, and type -130 in the right hand side.

In the right hand side of the and block, insert the second less than operator Рin this one, insert the vy in the left hand side, and type 0 in the right hand side.

if block, and, < operators

if block, and, < operators

So what is this doing? It’s saying, if the y position (or height) of the sprite is less than -130,¬†and vy is less than zero (e.g. we’re moving downwards), then do something. The y value will vary from the bottom to the top of the stage, with the middle being zero. The top and bottom are -180 at the bottom to 180 at the top. Take a look at Scratch’s coordinate system¬†¬†for more info. So if the bottom of the stage is -180, why are we using -130? That’s because we’re checking the centre of the sprite, not the bottom – so if the centre of the cat sprite is at -130, then the cat’s feet are about -180.

Scratch’s coordinate system

So we’ve touched the ground, now what? We need to bounce – and we can do that by changing our speed so the cat moves in the opposite direction. I use the term speed here, but as we’re measuring the magnitude of a speed in one direction (up), we’re actually measuring¬†velocity using a vector.

So if we multiply our velocity by -1, this will change our direction from down to up. It’s also a good idea to set the position of the sprite to -130 – this is in case the sprite has moved slightly below -130 before our check and puts it back at ground level.

Take the whole if block, including the set vy and set y blocks as below, and add it under the change y by block. Here’s our code:

Code

Bouncing of the ground code

Try this out, and see what happens. There’s something wrong with the bouncing though… when did you last see something bounce like that? And how do you think we could fix it? Create a remix and see how you can tweak it.

Here’s a copy of the project: http://scratch.mit.edu/projects/11418986/

Next steps

Next step in a couple of days will be to change from a bouncing cat to something more spacey… like a rocket we can steer ūüôā

Further Reading

Here’s a really good, but quite in depth, introduction to Gravity at Khan Academy:¬†https://www.khanacademy.org/science/physics/newton-gravitation/gravity-newtonian/v/introduction-to-gravity?v=Xcel427Ezl0

Leave a comment

Filed under Teaching kids to code