In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Simulating Gravity

Simulating Gravity, or at least the effect of gravity in Scratch can be difficult, but due to trigonometric blocks in Scratch, it is possible.

Warning Tip: You should always adjust the scripts shown in this tutorial as necessary to fit your project.

Creating Gravity Using Trigonometry

This section is designed to teach you how to simulate gravity's pull on an object around a center-point. This is similar to how the gravity of a star affects the motion of a planet.

The script below is based on a simple trigonometric identity which states that sin2x + cos2y = 1.

Below are the scripts you will need to put into the object that you want to rotate:

when flag clicked
set [Distance v] to [100]
set [Rotation v] to [0]
forever
  repeat (360)
    go to x: ((([sin v] of (Rotation)) * (Distance)) + ([x position v] of [Sprite2 v])) y: ((([cos v] of (Rotation)) * (distance)) + ([y position v] of [Sprite2 v]))
  end

when flag clicked
forever
  if <[359] < (Rotation)> then
    set [Rotation v] to [0]
  end
  if <[-359] > (Rotation)> then
    set [Rotation v] to [0]
  end

when flag clicked
forever
  point in direction (([direction v] of [Sprite2 v]) + (90))

when flag clicked
forever
  if <not <touching [Sprite2 v]?>> then
    change [Distance v] by (-1.5)
  end

If you want the person playing the game to control the rotation you can use this script:

when flag clicked
forever 
  if <key [right arrow v] pressed?> then
    change [Rotation v] by (2)
    switch to costume [costume3 v]
  end

when flag clicked
forever
  if <key [up arrow v] pressed?> then
    repeat (10)
      change [Distance v] by (5)
    end
    wait until <touching [Sprite2 v]?>
  end

when flag clicked
forever
  if <key [left arrow v] pressed?> then
    change [Rotation v] by (-2)
    switch to costume [costume4 v]
  end

For the "Planet" or other object that is being rotated about you will want to use this script:

when flag clicked
forever
  point towards [Sprite1 v]

Make sure you set the "Planet" or other object's rotation to "don't rotate".

Using trigonometry is a smooth and effective way for more experienced Scratchers to simulate gravity. With some more advanced scripts you can even rotate about non-circular objects.

An example of trigonometric gravity can be seen here.

Creating Gravity Using Rotation

Another method of simulating gravity is to change the center of a sprite so when you rotate it, it appears to be pulled by gravity. This method is much more simple, but far more tedious and inaccurate.

  1. Change the sprite's center to the center of the planet. Paint Editor With Sprite Center in Center.png
  2. Repeat Step 1 for every single sprite of that character.
  3. Write scripts for each sprite for every different situation:
when flag clicked
show
go to x: (0) y: (0)
point in direction (0 v)
forever
if <not <<key [space v] pressed?> or <<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>> then
if <touching [Sprite1 v]?> then
switch to costume [costume1 v]
end
end
end

when flag clicked
forever
wait until <key [up arrow v] pressed?>
play sound [jump v]
repeat (20)
switch to costume [costume4 v]
move (1.5) steps
end
repeat (10)
switch to costume [costume4 v]
move (1) steps
end
repeat (10)
switch to costume [costume4 v]
move (-1) steps
end
repeat (10)
switch to costume [costume4 v]
move (-1.5) steps
end
switch to costume [costume1 v]
go to x: (0) y: (0)
end

when flag clicked
forever
if <<key [right arrow v] pressed?> and <not <key [up arrow v] pressed?>>> then
turn cw (5) degrees
switch to costume [costume2 v]
wait (0.05) secs
turn cw (5) degrees
switch to costume [costume3 v]
wait (0.05) secs
end
if <<key [right arrow v] pressed?> and <key [up arrow v] pressed?>> then
turn cw (5) degrees
switch to costume [costume4 v]
wait (0.05) secs
turn cw (5) degrees
switch to costume [costume4 v]
wait (0.05) secs
end
end

when flag clicked
forever
if <<key [left arrow v] pressed?> and <not <key [up arrow v] pressed?>>> then
turn cw (-5) degrees
switch to costume [costume5 v]
wait (0.05) secs
turn cw (-5) degrees
switch to costume [costume 6 v]
wait (0.05) secs
end
if <<key [left arrow v] pressed?> and <key [up arrow v] pressed?>> then
turn cw (-5) degrees
switch to costume [costume4 v]
wait (0.05) secs
turn cw (-5) degrees
switch to costume [costume4 v]
wait (0.05) secs
end
end

One example of rotational gravity can be seen here.

Creating Gravity Using Physics

One advanced technique for simulating gravity involves Newton's law of universal Gravity:

Newton's Law of Universal Gravity.png

Variables and Lists

From this equation and Newton's 2nd Law of Motion (which states that F = ma) we can solve for the change in velocity of an object as: a = (Gm2)/(r2)

This describes the acceleration of one object due to the gravity of another (with mass = m2)

In the equation above, three variables can be seen to be needed:

  • G (Newton's universal law of gravity)
  • m2(mass of the other object)
  • r (the distance the objects are from each other)

G is a universal constant and can often lead to masses and distances that seem very awkward or unintuitive. To simplify the equation and allow you to use easier/friendlier numbers, we can actually ignore G. When you choose your relative masses you will be factoring it in.


Along the mass of object two and its distance, the speed of the moving object (object one) will need to be stored:

  • X Velocity
  • Y Velocity

Also, to turn the acceleration into its x and y components, a ratio will be used (this is based on the idea that the x and y forces form a right triangle similar to that formed by the objects themselves)

  • Ratio

Finally, a variable will be used to iterate through every mass and every object's x and y position:

  • I

Those masses will be stored in a list:

  • Masses

As well as the x and y positions:

  • X Positions
  • Y Positions

Scripting

To begin, a custom block is needed that will iterate through every object in the project:

define Check Objects//make sure this runs without screen refresh!
set [I v] to (1)//the beginning of the list
repeat (length of [Masses v])//each object takes up two items in the list
    . . .//this is where the calculations will go
    change [I v] by (1)//moving onto the next object
end

To note, the custom block needs to run without a screen refresh. After the custom block is created, the distance between the sprite and an object needs to be computed:

define Check Objects
set [I v] to (1)//the beginning of the list
repeat (length of [Masses v])
    set [Dist. v] to ([sqrt v] of ((((item (I) of [X Positions v]) - (X Position))*((item (I) of [X Positions v]) - (X Position))) + (((item (i) of [Y Positions v]) - (Y Position))*((item (i) of [Y Positions v]) - (Y Position))))
    change [I v] by (1)
end

Next, the overall acceleration is needed:

define Check Objects
set [I v] to (1)//the beginning of the list
repeat (length of [Masses v])
    set [Dist. v] to ([sqrt v] of ((((item (I) of [X Positions v]) - (X Position))*((item (I) of [X Positions v]) - (X Position))) + (((item (i) of [Y Positions v]) - (Y Position))*((item (i) of [Y Positions v]) - (Y Position))))
set [Acceleration v] to ((item (i) of [Masses v]) / ((Dist.) * (Dist.)))// equation we found above
change [I v] by (1)
end

Next, the force is needed to turn into its x and y components. To achieve this, the force will be compared to the distance, and that ratio, when compared to the horizontal/vertical distance between the sprite and an object, will achieve just that:

define Check Objects
set [I v] to (1)//the beginning of the list
repeat (length of [Masses v])
    set [Dist. v] to ([sqrt v] of ((((item (I) of [X Positions v]) - (X Position))*((item (I) of [X Positions v]) - (X Position))) + (((item (i) of [Y Positions v]) - (Y Position))*((item (i) of [Y Positions v]) - (Y Position))))
set [Acceleration v] to ((item (i) of [Masses v]) / ((Dist.) * (Dist.)))//equation we found above
    set [Ratio v] to ((Acceleration) / (Dist.))
    change [X Velocity v] by ((Ratio) * ((item (I) of [X Positions v]) - (X Position)))//x component of the force vector
    change [Y Velocity v] by ((Ratio) * ((item (i) of [Y Positions v]) - (Y Position)))//y component of force vector
    change [I v] by (1)
end

The script is now done, though the variables X Velocity and Y Velocity need to have some use:

when gf clicked
set [X Velocity v] to (0)
set [Y Velocity v] to (0)
forever
  Check Objects
  change x by (X Velocity)//applying the velocities
  change y by (Y Velocity)

define Check Objects
. . .//refer above for the coding

Final Product

Once the steps above have been followed, this should be the final coding:

define Check Objects
set [I v] to (1)//the beginning of the list
repeat (length of [Masses v])
    set [Dist. v] to ([sqrt v] of ((((item (I) of [X Positions v]) - (X Position))*((item (I) of [X Positions v]) - (X Position))) + (((item (i) of [Y Positions v]) - (Y Position))*((item (i) of [Y Positions v]) - (Y Position))))
set [Acceleration v] to ((item (i) of [Masses v]) / ((Dist.) * (Dist.)))//equation we found above
    set [Ratio v] to ((Acceleration) / (Dist.))
    change [X Velocity v] by ((Ratio) * ((item (I) of [X Positions v]) - (X Position)))//x component of the force vector
    change [Y Velocity v] by ((Ratio) * ((item (i) of [Y Positions v]) - (Y Position)))//y component of force vector
    change [I v] by (1)
end

when gf clicked
set [X Velocity v] to (0)
set [Y Velocity v] to (0)
forever
  Check Objects
  change x by (X Velocity)//applying the velocities
  change y by (Y Velocity)

Simulating Gravity in Scrollers

Gravity can be replicated on Scratch to be used in Scrollers and other project where an object is forced downward. Listed below are several methods that you can use.

Velocity Method

The Velocity Method is a great method for creating gravity and is highly effective and adaptable for multiple situations. Here is an example script—please note, this script will have the sprite inside the ground, rather than on top of it. This script will be placed into the Sprite that is being affected by gravity:

when flag clicked
go to x: (0) y: (0)
forever
change y by (Y Velocity)
set [Y Velocity v] to ((Y Velocity) * (0.98))
when flag clicked
forever
if <not<touching [Ground v]?>> then
change [Y Velocity v] by (-0.1)
end
when flag clicked
forever
if <touching [Ground v]?> then
set [Y Velocity v] to [0]
end
wait until <not<touching [Ground v]?>>

Click here to see an example.

Direct Movement Method

Direct movement is more simple than the Velocity Method, but far less efficient at what it does. Not only does the jump look unrealistic, but it is far less practical in almost all situations. Nonetheless, the script works and is a good starting point for beginners:

when flag clicked
forever
if <not <touching [Ground v]?>> then
change y by (-1)
end
end

when flag clicked
forever
if <key [up arrow v] pressed?> then
repeat (10)
change y by (10)
end
end
wait until <touching [Ground v]?>

Examples

See Also

  • This page was last modified on 25 June 2014, at 03:53.