from visual.controls import *
from random import *
from math import *

gun=arrow(pos=(-10,0,0),axis=(1.0,1.0,0))
ball=sphere(pos=(-10,0,0),radius=0.5,color=color.red)
ball.v=vector(0,0,0)
floor=box(pos=(0,-0.2,0),length=20,height=0.3,width=10)
target=box(pos=(-5+10*random(),0.5,0),width=1.0,length=1.0,height=1.0,color=color.blue)
trail=curve()

g=9.8
mu=0
v=5
theta=45

dt=0.01


scene.center=vector(0,10,0)
scene.autoscale=0

hit=0

c=controls(title="Firing parameters")
glab=label(pos=(-10,20,0),text="Grav= %5.2f m/s^2" % g)
flab=label(pos=(-10,18,0),text="Friction= %5.2f"%mu)
vlab=label(pos=(-10,16,0),text="Velocity= %5.2f m/s"%v)
alab=label(pos=(-10,14,0),text="Angle= %5.2f degrees"%theta)

# Now, put in the controls
def fire():
    global hit
    global trail
    if (fire_button.text=="Fire"):
        ball.v=5*gun.axis
        gun.visible=0
        fire_button.text="Reset"
        t=0
        ###############################################################
        # Here is the entire assignment.
        # Put in the necessary physics to launch the "ball" (including
        # using a path), and include the gravity (constant grav downward), and
        # air resistance.  You should stop as soon as the "ball" drops below
        # the floor.
        ###############################################################

    else:
        fire_button.text="Fire"
        reset()
        

def change_grav():
    g=grav_slide.value
    glab.text="Grav= %5.2f m/s^2" % g

def change_mu():
    mu=mu_slide.value
    flab.text="Friction= %5.2f"%mu

def change_vel():
    v=vel_slide.value
    vlab.text="Velocity= %5.2f m/s"%v
    gun.axis=vector(v*cos(theta),v*sin(theta),0)/5

def change_angle():
    theta=angle_slide.value
    alab.text="Angle= %5.2f degrees"%theta
    gun.axis=vector(v*cos(pi*theta/180),v*sin(pi*theta/180),0)/5
    
def reset():
    global hit
    global trail
    if (hit==1):
        target.pos=(-5+10*random(),0.5,0)
    ball.pos=(-10,0,0)
    gun.visible=1
    trail.visible=0
    trail=curve(pos=[ball.pos])
    trail.visible=1

    
# Put in the control buttons -- fire,
grav_slide = slider(pos=(-45,-40), width=7, length=70, axis=(0,1.0,0),min=0,max=20,action=lambda: change_grav())
grav_slide.value=g
mu_slide = slider(pos=(-15,-40),width=7,length=70,axis=(0,1.0,0),min=0,max=1.0,action=lambda: change_mu())
mu_slide.value=mu
vel_slide=slider(pos=(15,-40),width=7,length=70,axis=(0,1.0,0),min=0,max=20,action=lambda: change_vel())
vel_slide.value=v
angle_slide=slider(pos=(45,-40),width=7,length=70,axis=(0,1.0,0),min=0,max=90,action=lambda: change_angle())
angle_slide.value=theta
fire_button=button(pos=(0,-80),width=60,height=20,text='Fire',action=lambda: fire())


while (1==1):
    c.interact()
    




