# SlingShot

``````function setup()
ball = true
ballIsMoving = false
Xpos = -50
Ypos = -50
Xspeed = 0
Yspeed = 0
parameter.number("Elasticity", .5, 2, 1)
parameter.watch("CurrentTouch")
parameter.watch("Xspeed")
parameter.watch("Yspeed")
end
function findDistance(x1, y1, x2, y2)
return math.sqrt((x2-x1)^2+(y2-y1)^2)
end

function draw()
background(40, 40, 50)
E = -1*Elasticity
if CurrentTouch.y <= HEIGHT/2 and CurrentTouch.y >= HEIGHT/8 and (CurrentTouch.state == 0 or CurrentTouch.state == 1) and ball == true and CurrentTouch.x >= 50  and CurrentTouch.x <= WIDTH - 50 then
lineX = math.abs(CurrentTouch.x-WIDTH)
strokeWidth(math.abs(math.abs(findDistance(lineX, HEIGHT/2, CurrentTouch.x, CurrentTouch.y)/50)-20))
line(CurrentTouch.x, CurrentTouch.y, lineX, HEIGHT/2) -- Show where the ball will go
Xpos = CurrentTouch.x
Ypos = CurrentTouch.y
end
if (CurrentTouch.state == 2 or ballIsMoving) and ready == false then
if ball == true then
ball = false
ballIsMoving = true
Xspeed = (lineX-CurrentTouch.x)/60 -- find the "run" of the trajectory line
Yspeed = (HEIGHT/2 - CurrentTouch.y)/50 -- find the "rise" of the trajectory line
end
if ballIsMoving then
Xpos = Xpos + Xspeed
Ypos = Ypos + Yspeed
end
end
ellipse(Xpos, Ypos, 100)
if Xpos <= 50 or Xpos >= WIDTH-50 then -- Bounce if ball hits the side
Xspeed = Xspeed * E
end
if Ypos >= HEIGHT-50 or (Ypos <= 50 and (CurrentTouch.state == 0 or CurrentTouch.state == 1 or CurrentTouch.tapCount >= 2)) then -- Bounce if ball hits the top
Yspeed = Yspeed * E
elseif Ypos <= -50 and CurrentTouch.state == 2 then -- If ball is below the screen, then stop
Xspeed = 0
Yspeed = 0
Xpos = -50
Ypos = -50
ballIsMoving = false
ball = true