from visual import *

#===========================================
# Bounce Power Law Hypothesis: V(after)= c*V(before)^d
# set these parameters for each ball and drop
c = 1.0  # coefficient in proposed power law
d = 0.95  # exponent in proposed power law
ball_h=4.0 # ball drop height
ball_r=0.2 # ball radius
#===========================================

thick=0.3
scene.autoscale=1
scene.background=(1,1,1)
floor = box(length=4, height=thick, width=4, color=color.blue)

ball = sphere(pos=(0,ball_h,0), radius=ball_r, color=color.red)
ball.velocity = vector(0,0,0)
vysave=0.0
scene.autoscale=0
dt = 0.01
while 1:
    rate(100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y-(thick/2.0) < ball_r:
        bvy = ball.velocity.y
        ball.velocity.y = -(bvy/fabs(bvy))*c*(fabs(bvy))**d
    else:
        ball.velocity.y = ball.velocity.y - 9.8*dt
        if ((ball.velocity.y<0) & (vysave>=0.0)):
            print "Ball max height= ",ball.y-(thick/2.0)-ball_r
        vysave=ball.velocity.y