from visual import *
autoscale=0

# Set Model Constants
# set origin at middle of launch tube exit
dt = 0.1 # seconds
x_wall=12.7 # meters
y_floor=-0.88 # meters y=0 at height of middle of launch tube
ball_diameter=0.03 # meters
ball_mass=0.010 # kilograms
launch_velocity=vector(10.0,0.0,0.0) # meters/second

# Fluid Drag Force Magnitude= (1/2)*drag_coef*fluid_density*xsection_area*v**2
xi=-x_wall*0.5
scene.range=(x_wall*0.6,x_wall*0.6,x_wall*0.6)
air_viscocity=1.5e-5
air_density=1.2
drag_coefficient=0.5
ball_radius=ball_diameter/2.0
ball_area=pi*ball_radius**2
ball_diameter=0.03
drag_constant=0.5*drag_coefficient*air_density*ball_area/ball_mass
a_gravity=vector(0.0,-9.8,0.0)

floor = box(pos=(0.0,y_floor,0.0), length=x_wall, height=0.1, width=4, color=color.blue)
wall = box(pos=(x_wall*0.5,1.5,0.0), length=0.01, height=5.0, width=4, color=color.blue)
ball = sphere(pos=(xi,0,0), radius=0.1, color=color.red)
ball.velocity = launch_velocity

time=0.0
floor_impact=0
while time<3.0:
    rate(100)
    v_mag=mag(ball.velocity)
    a_drag=-drag_constant*v_mag*ball.velocity
    ball_acceleration=a_drag+a_gravity
    ball.velocity = ball.velocity + ball_acceleration*dt
    ball.pos = ball.pos + ball.velocity*dt
    reynolds_number=air_density*mag(ball.velocity)*ball_diameter/air_viscocity
    if ball.y < y_floor:
        floor_impact=floor_impact+1
        ball.y=y_floor
        ball.velocity.y = -ball.velocity.y
        print "Floor impact x[",floor_impact,"] = ",ball.pos.x-xi
    if ball.x > x_wall*0.5:
        ball.velocity.x = -ball.velocity.x
        ball.x=x_wall*0.5
        print "Wall impact x = ",ball.pos.x-xi," y = ",ball.pos.y
    time=time+dt