Simple physics: gravity, bouncing and friction

Lesson 8 of 20 · Python and Processing

In games and simulations objects fall, bounce and slow down. We can do all of that with two variables: position and velocity. This lesson connects programming with the physics from the 6th and 7th grade (speed, acceleration, the force of gravity).

Works in both environments:
the code in this lesson uses only functions that have the same names in Processing Python Mode and in py5 (size, background, fill, ellipse, width, height), so it works without changes both in Processing and in Thonny.

1. Velocity changes position, acceleration changes velocity

In every frame: the velocity increases by the acceleration (gravity), and the position moves by the velocity.

y = 50        # the position of the ball (pixels from the top)
vy = 0        # the downward velocity (pixels per frame)
g = 0.4       # gravity: how much the velocity grows every frame


def setup():
    size(400, 400)


def draw():
    global y, vy
    background(230)

    vy = vy + g      # acceleration changes velocity
    y = y + vy       # velocity changes position

    fill(220, 60, 60)
    ellipse(200, y, 40, 40)

The ball falls faster and faster – just like a real object. But it falls through the floor. We will fix that.

2. Bouncing off the floor

y = 50
vy = 0
g = 0.4
bounce = 0.8   # how much velocity is left after the hit (80%)


def setup():
    size(400, 400)


def draw():
    global y, vy
    background(230)
    vy = vy + g
    y = y + vy

    # the floor is at height - 20 (the radius of the ball is 20)
    if y > height - 20:
        y = height - 20          # put the ball back on the floor
        vy = -vy * bounce        # reverse the direction and lose some energy

    fill(220, 60, 60)
    ellipse(200, y, 40, 40)
Why do we put y back on the floor?
so that the ball does not "sink" into the floor. If you leave it out, after a few hits the ball gets stuck under the floor and shakes.

3. Sideways movement and friction

We add a velocity along the x axis. Every frame friction reduces the velocity a little, so the ball slowly stops.

x, y = 50, 50
vx, vy = 4, 0
g = 0.4
bounce = 0.8
friction = 0.99   # 1% of the velocity is lost every frame


def setup():
    size(600, 400)


def draw():
    global x, y, vx, vy
    background(230)

    vy = vy + g
    vx = vx * friction
    x = x + vx
    y = y + vy

    if y > height - 20:
        y = height - 20
        vy = -vy * bounce
    if x < 20 or x > width - 20:     # the left and right walls
        vx = -vx
        x = constrain(x, 20, width - 20)

    fill(40, 120, 220)
    ellipse(x, y, 40, 40)

4. Many balls – a list

Every ball is a list [x, y, vx, vy]. One loop moves all the balls. This is the basis for rain, fireworks and particle systems.

balls = []
g = 0.3


def setup():
    size(600, 400)
    for i in range(15):
        balls.append([random(20, width - 20), random(20, 150), random(-3, 3), 0])


def draw():
    background(230)
    fill(40, 160, 90)
    for b in balls:
        b[3] = b[3] + g          # vy
        b[0] = b[0] + b[2]       # x
        b[1] = b[1] + b[3]       # y
        if b[1] > height - 10:
            b[1] = height - 10
            b[3] = -b[3] * 0.85
        if b[0] < 10 or b[0] > width - 10:
            b[2] = -b[2]
        ellipse(b[0], b[1], 20, 20)

Practice tasks

  • Change g to 0.1 – what would it look like on the Moon?
  • Add a mouse click that gives the balls an upward velocity (in py5: mouse_pressed()).
  • Make the ball change colour after every hit on the floor.
  • Challenge: let the micro:bit tilt sensor change the direction of gravity (see the final project).

Check your knowledge

Open the quiz

Mini quiz: physics in animation

1. What changes every frame when we add gravity?




2. What does vy = -vy * 0.8 mean?




3. What does vx = vx * 0.99 do?




Useful external resources