from visual import *

#----- Set These Values in Meters, Seconds, Kilograms-----
mass = 0.0143 #of ball
v0x = 4.3 # launch velocity x-component in meters/second
v0y = 3.8 #  launch velocity x-component in meters/second
air_friction_const=0.5
#----------------------------
grav = 9.81
dist=8.0
ball_radius=0.012
pi=3.14159
scene.background=(1,1,1)
floor = box(pos=(0,0,0), length=dist, height=0.05, width=0.5, color=color.blue)
ballx=-dist/2.0
bally=0.0
air_density=1.29
kvalue=0.5*air_friction_const*air_density*pi*ball_radius**2/mass
ball = sphere(pos=(ballx,bally,0), radius=5*ball_radius, color=color.red)
ball.velocity = vector(v0x,v0y,0.0)
dt = 0.001
time=0.0
scene.autoscale=0
scene.uniform=1
quit=0
done=0
trail=curve()
while (quit == 0):
    rate(100)
    vmag2=(ball.velocity.x**2+ball.velocity.y**2)
    vmag=vmag2**0.5
    ax=-kvalue*vmag*ball.velocity.x
    ay=-grav-kvalue*vmag*ball.velocity.y
    ball.acceleration = vector(ax,ay,0)
    if (done==0):
        ball.velocity=ball.velocity+ball.acceleration*0.5*dt
        done=1
    else:
        ball.velocity=ball.velocity+ball.acceleration*dt
    ball.pos = ball.pos + ball.velocity*dt
    trail.append(pos=ball.pos, color=color.green)
    time=time+dt
    if (ball.pos.y<0):
      xx=ball.pos.x+(dist/2.0)
      if (xx > dist):
          xx=dist
      yy=ball.pos.y
      if (yy < 0.0):
        yy=0.0
      print "Time ",time," Position (",xx,", ",yy,") meters"
      quit=1