from visual import *

# vpython model of a ball rolling on an
# inclined surface without slipping

# SET PARAMETERS=================
a = 2.0 # surface inclination angle (degrees)
x = 2.0 # distance the ball rolls (meters)
#=================================

autoscale = 1
a = a*(3.14159/180.0) # convert to radians
t = 0.05 # surface thickness (meters)
t2 = t/2.0
r = 0.0075 # radius of the ball (meters)
incline = vector(x*cos(a),x*sin(a),0.0)
floor = box(axis=incline, length=x, height=t, width=0.5, color=color.blue)
# starting position
x0 = (x/2.0)*cos(a)-(r+t2)*sin(a)
y0 = (x/2.0)*sin(a)+(t2+r)*cos(a)
z0 = 0.0
pos0 = vector(x0,y0,z0)
ball = sphere(pos=pos0, radius=r, color=color.red)
ball.velocity = vector(0,0,0)
# ball acceleration
acc = vector(-(5.0/7.0)*9.8*sin(a)*cos(a),-(5.0/7.0)*9.8*sin(a)*sin(a),0.0)
print "Linear Acceleration = ",mag(acc)," m/s^2"
dt = 0.01
time = 0.0
autoscale = 0
while 1:
    rate(100)
    time = time + dt
    ball.pos = ball.pos + ball.velocity*dt
    if ball.x < -((x/2.0)*cos(a)+(r+t2)*sin(a)):
        ball.velocity = -ball.velocity
        print "Time of impact = ",time, " seconds"
    else:
        ball.velocity = ball.velocity + acc*dt