9. Special Effect: Simulating Gravity

In these notes you will learn:

  • How to simulate gravity.
  • How to simulate the elasticity of an object.

9.1. Introduction

So far our bouncing ball simulations have ignored gravity completely. In this note we will see how we apply a couple of simple tricks to get effects that look as if gravity is in action.

We won’t worry about the exact physics of gravity here: all that concerns us here is that the ball looks like it is under the influence of gravity.

Note

Of course, sometimes it is important to simulate gravity, or other physical processes accurately, even in video games. This is tricky enough that many games use so-called physics engines that provide premade functions designed to efficiently simulate realistic physics. As you can imagine, creating a physics engine requires quite a bit of knowledge of physics, more than we will cover in the introductory programming course.

If you are curious about this topic, then you might want to look at a book such as Game Physics Engine Development By Ian Millington.

9.2. A Bouncing Ball in Gravity

We’re going to explore gravity using our trusty friend, the bouncing ball. Now what do we mean by ‘simulating gravity’? What do want to accomplish? In the real world, when an object (say a cannonball) is dropped from a height, it will start off having 0 speed, and will accelerate at some constant speed (9.8 meters per second to be more precise). This is what we’d like to achieve: our object will start with dy as 0.0, and with each call to draw we will add a constant amount to it.

This will have the effect of making the ball fall downwards slowly at first, but then increasing in speed as it nears the bottom. When it hits the bottom edge we reverse dy to get a simple bouncing effect.

The second we’d like to model is that of elasticity, which is one of the factors that determines how high the ball bounces after it hits the ground. The idea of elasticity is that every time the ball hits the ground it loses some energy, and so it bounces lower and lower. Exactly how much energy the ball loses depends on what it is made. e.g. a rubber ball loses less energy than a glass marble. Instead of directly modelling the material of the ball, we’ll use elasticity to dampen the bounce every time the ball hits the ground.

We are still ignoring some details, such as the ball’s mass and resistance from the air. But as you will see when you run the program, the results look good enough to be used in many games.

Here is the program:

float x, y;
float dx, dy;
float gravity_y;
float elasticity;

void setup() {
    size(500, 500);
    smooth();

    // set the initial position and speed of the ball
    x = 25;
    y = 100;
    dx = 1.8;
    dy = 0;

    // gravity and elasticity are constant values: try playing with them.
    gravity_y = 0.1;
    elasticity = 0.5;
}


void draw() {
    background(255);
    noStroke();

    fill(200, 0, 0);
    ellipse(x, y, 50, 50);

    x += dx;
    y += dy;

    dy += gravity_y;

    if (y + 25 >= 499) {
        y = 499 - 25;
        dy = -(dy * elasticity);
    }
}

There are several points to notice about this program:

  • You may have seen this in earlier programs: we’ve declared two variables on the same line. e.g.

    float x, y;
    

    This is just to save a bit of typing. It is equivalent to writing:

    float x;
    float y;
    
  • As discussed, dy starts at 0, but increases with subsequent calls to the draw() function.

  • When the ball hits the bottom edge, we don’t just flip dy as we’ve done until now. Instead, we multiply dy (which is positive) by elasticity (in our case set to 0.5). Thus we are sending the ball in the opposite direction, at a fraction (0.5) of its speed at the time of impact. This means, since we are always adding gravity_y to dy, will eventually become positive again, and the ball will begin its decent. In other words, we achieve a bouncing effect.

9.3. Adding More Gravity

In real life there is only one major source of gravity on Earth that pulls things downwards. However, it is easy to simulate other kinds of gravity to our demo program.

For example, just for fun let’s add a second force of gravity that causes things to move toward the right edge of the screen:

float x, y;
float dx, dy;
float gravity_x, gravity_y;
float elasticity;

void setup() {
    size(500, 500);
    smooth();

    // set the initial position and speed of the ball
    x = 25;
    y = 100;
    dx = 1.8;
    dy = 0;

    // gravity and elasticity are constant.
    gravity_x = 0.025;
    gravity_y = 0.05;
    elasticity = 0.5;
}

void draw() {
    background(255);

    fill(200, 0, 0);
    ellipse(x, y, 50, 50);

    x += dx;
    y += dy;

    dx += gravity_x;
    dy += gravity_y;

    if (y + 25 >= 500) {
        y = 500 - 25;
        dy = -(dy * elasticity);
    }

    if (x + 25 >= 500) {
        x = 500 - 25;
        dx = -(dx * elasticity);
    }
}

The movement of the ball is now much more complex. Let’s try to get a closer look at it. We can modify the program above to see the path that the ball takes, by first commenting out the call background in the first row of draw(), and then making the ball smaller, showing the path more clearly

void draw() {
    // background(255);

    fill(200, 0, 0);
    ellipse(x, y, 5, 5);

    // .. statements as before ...
}

9.4. Questions

  1. Besides certain video games, what is another application that might require more accurate modelling of physics?
  2. What is the purpose of the variable elasticity?

9.5. Programming Questions

  1. Modify the gravity demo program so the ball’s bottom edge bounces on the bottom of the screen, and not its center.
  2. Modify the gravity demo program so that the ball stops when it hits the right edge of the screen instead of rolling off.
  3. Modify the gravity demo program to use an image (of your choice) instead of a circle. Make sure no part of the image goes off the screen when it bounces.
  4. Modify the gravity demo program so that gravity pulls the ball up instead of down, causing the ball to bounce on the top edge.
  5. Modify the gravity demo program so that gravity pulls the ball to the left instead of down, causing the ball to bounce on the left edge.