#=====================#
# PHYS113-002         #
# Assignment 2        #
#                     #
# Aleksandr Karagodov #
# 10/08/2007          #
#=====================#

from pylab import *
from visual import *
scene.autoscale = 0;
scene.center = vector(0.0, 5.0, 2.0);

boxx = box(length = 10.0, height = 0.1, width = 15.0, color = color.yellow);

ball = sphere(pos = vector(0.0, 5.0, 0.0), m = 5.0,
              radius = 0.4, color = color.red);
ball.p = vector(2.0, 0.0, 1.0); # initial momentum
ball.dp = vector(0.0, 0.01, 0.0); # change in momentum

trail = curve(color = color.green);

eps = 1E-9; # precision constant
k = 0.9 # "bouncing coefficient". Set to 1 for infinite bouncing.
g = 9.8;
dt = 0.01;

a = vector(0.0, -g, 0.0); # gravity acceleration

while(abs(ball.dp.mag) > eps):  # while there's a movement
    rate(100);
 
    if(ball.pos.y - ball.radius + eps < boxx.height / 2.0 + boxx.pos.y):
          ball.dp = vector(0.0, - 2.0 * ball.p.y * k, 0.0);
    else: ball.dp = ball.m * a * dt;
 
    ball.p += ball.dp;
    ball.pos += ball.p / ball.m * dt;
    trail.append(ball.pos);

print "program finished";
