## World Gen Mods Ramp Up Minecraft Play

dark mode light mode Search Menu
Search

# Scratch and Motion

my camera and me on Flickr

Hi Scratchers! In this issue, we’re going to talk about movement. It’s pretty easy to get started with movement in Scratch. The movement blocks can be connected together to change your x and y position, to move in the direction your sprite is facing, or even move smoothly between two points. What’s a little harder is being able to move more realistically. That requires understanding a bit of physics, which is what we’re going to do in the rest of this article!

We’re going to start by defining a few terms: position, velocity, and acceleration. The short version is that:

• “Position” is where you are
• “Velocity” is how your position is changing
• “Acceleration” is how your velocity is changing

You might read that and think “what’s the difference between velocity and speed?” Well, speed just tells how fast you’re going but not what direction you’re going. Velocity is your speed and the direction you’re moving.

A fun bit of weirdness is that you can’t actually feel velocity. You can be going super fast and not be able to tell that your velocity isn’t changing without a tell like the wind hitting your face or scenery whipping by, like if you’re in a train that’s on smooth tracks. You only can feel acceleration!

So in both games programming and real physics, we describe all of these things with coordinates. In Scratch we can move up & down and left & right, which are our y and x coordinates respectively.

In our day to day life, we use three coordinates to describe where we are in space: x, y, and z. Sometimes problems in physics with multiple objects get modeled like a single thing moving through 4-dimensional (or higher) space! And sometimes we even calculate motion with coordinates that don’t correspond to anything like x, y, or z. We’ll see a little example of that later!

How do we actually use these concepts, though? Scratch already has the x and y coordinates defined for a sprite. You’ll need to define x and y velocity variables yourself, though, like in the following screenshot:

This is too simple, though. We’re not having realistic acceleration. To do that we need to change all our code that sets velocity to blocks that change velocity:

If you play with this you can see that holding down the arrow keys moves you faster but you start very slow.

What else can we do? A natural thing to tackle next is gravity. We’ll look at both the simplified gravity for when you’re close to the ground as well as the full version of gravity that governs how planets orbit the sun.

The simple version like you’d use in a platformer is where you assume gravity is a constant acceleration down. Here’s a simple “flappy bird” physics example:

Our gravitational acceleration in this case is the line that changes velocity to be more negative every loop. It follows the same pattern though!

Now let’s finally hit our first example where the acceleration isn’t constant either: planetary orbits!

Gravity, once you’re far away from the surface of a planet where you can simplify it, actually gets weaker the further you are from the object. The acceleration due to gravity if you’re orbiting a planet, in the x and y direction respectively, looks like:

```a_x = - g*xD/r^3 and a_y = - g*yD/r^3
```

where xD is the distance to the planet in the x direction, yD is the distance to the planet in the y direction, r is the distance to the planet, and finally g is a gravitational constant that—while an experimentally determined constant in real physics—you can set as you wish in a game.

I can’t stress enough that this is just a few lines of code that is a realistic simulation of planetary orbits and we did it on a site for making video games. That’s kinda awesome! Try out this game and watch all the possible orbits you can make and their shapes!

Our final example, that I think is pretty cool, is that you can even make pendulums—which physicists like to call “harmonic oscillators”—super easily.

I’m slipping something past you, though! Can you read through this code to figure out what it is?

So here our acceleration and velocity aren’t for the x and y coordinate! No, our coordinate is the angle our pendulum is swinging through. 0 degrees is pointing straight down at rest and pointing right is 90 degrees, pointing left is -90 degrees. It’s a cute trick that makes the math for a pendulum exactly the same as the math of a weight bouncing up and down on the end of a spring. You need just a hint of trigonometry to understand this code.

So that’s our whirlwind introduction to making realistic movement in Scratch. If you keep in mind the difference between your position, velocity, and acceleration you can code up all sorts of wild or complex things!

Check out the Scratch studio below for the full versions of all the code snippets we included.

#### Doing Physics with Scratch

https://video.search.yahoo.com/yhs/search?fr=yhs-Lkry-SF01&hsimp=yhs-SF01&hspart=Lkry&p=Doing+physics+with+Scratch#action=view&id=9&vid=155e1b75500e8445e237992d66cae12f

#### Scratch – Velocity

https://en.scratch-wiki.info/wiki/Velocity

#### Simulating Gravity

https://en.scratch-wiki.info/wiki/Simulating_Gravity

#### Racing Game in Scratch

https://www.wikihow.com/Create-a-Racing-Game-in-Scratch

#### Programming velocity

http://www.raspberry-pi-geek.com/Archive/2014/03/Programming-velocity-and-speed-in-Scratch