Sunday, November 6, 2011

Programming falling objects in Scratch

How can we write Scratch scripts to simulate falls? In a recent project we programmed a falling ball. Here, I am going to explain it from a different angle (see and download this project at scratch.mit.edu),

Let's do a little thought experiment. What is the simplest way to make an object fall? We could start by constantly changing its y position, like so:

Change y position of the object at each loop iteration
We could put the value of this change in a variable (we will just call this variable v) and our code would be like this:

Using a variable to contain the downward displacement
If we run this script, the first thing we'd notice is that the fall is very unnatural. This is because the object is falling at a constant rate. It moves downwards by the same distance at every loop iteration. A fall in the real world is not like that - a falling object in the physical world moves faster and faster downwards all the time. To simulate this we simply add a statement block to increase v by a small amount at every loop:

Increase downward displacement at every loop iteration
At this point, you may be confused as to why we are changing v by negative 0.5 when previously we had mentioned we wanted to increase v. The negative sign denotes that the direction of the change is downwards. We increase the magnitude of v by 0.5 downwards, and that is equivalent to changing v by -0.5.

That was actually all the code we need to simulate a falling object.

Making the Scratch sprite land

Most of the time, we would want the object to be able to land on the ground (or something else, anything). How can we program landing with Scratch? Let's think about this again.. When we jump off a chair in the physical world, why are we able to land on the ground and not sink further in? After all, we are always acted upon by gravity, meaning that we are always pulled towards the centre of the Earth. So why not sink further in?

The scientific explanation for this is that the ground exerts a force equal to the gravitational force on us. The two forces cancel leaving us stationary, since there can be no further movement without a force.

We can use the same concept in our script. We insert statement blocks to cancel out the falling distance (i.e. the v variable) when the object is on the ground.

I will suppose that when our object touches the ground, its y position is -80.  So I will use the condition if y position < -80 to determine whether the object is on the ground. We could of course use other means, like the sensor blocks for example.

Here is the complete script:

Final script
What simply happens now is that when the object lands, we set v to a positive value of the falling displacement i.e. set v to 0.5. In the very next statement block, v is decreased by the same amount (i.e. change v by -0.5) leaving it to result to zero. When the program later executes change y by v it is effectively changing the object's y position by zero, and so the object stays stationary.

3 comments:

  1. lo i am gonna get a goooooooooood level kl yeah

    ReplyDelete
  2. Thank you so much! Before I knew how to do this my meteors were all over the place and impossible to avoid. Once again, thanks!

    ReplyDelete