# bounce2

from Processing import *
window(500, 500)
w = width()
h = height()

# Init all variables
sx = random(0.0, w)         # x position
sy = random(0.0, 10.0)      # y position
vx = random(-3.0, 3.0)      # x velocity
vy = random(0.0, 5.0)       # y velocity

sx2 = random(0.0, w)        # x position
sy2 = random(0.0, 10.0)     # y position
vx2 = random(-3.0, 3.0)     # x velocity
vy2 = random(0.0, 5.0)      # y velocity

ay = 0.2                    # y acceleration (gravity)
fr = 0.9                    # Losses due to friction

fill(255, 0, 0)

def draw(o, e):
    global sx, sy, vx, vy, sx2, sy2, vx2, vy2
    background(255)

    # Equations of Motion
    sx = sx + vx
    sy = sy + vy + 0.5*ay
    vy = vy + ay

    sx2 = sx2 + vx2
    sy2 = sy2 + vy2 + 0.5*ay
    vy2 = vy2 + ay

    # Bounce off walls and floor
    if sx <= 0.0 or sx >= w:
        vx = -fr*vx
    if sy >= (h-10.0) and vy > 0.0:
        sy = (h-10.0)
        vy = -fr*vy

    if sx2 <= 0.0 or sx2 >= w:
        vx2 = -fr*vx2
    if sy2 >= (h-10.0) and vy2 > 0.0:
        sy2 = (h-10.0)
        vy2 = -fr*vy2

    # Draw ball
    ellipse( sx, sy, 20, 20)
    ellipse( sx2, sy2, 20, 20)

# Set up looping
onLoop += draw
frameRate(50)
loop()
