#CONSTANT EPSILON = 0.001
 
GLOBAL GRAVITY# = 0.60
GLOBAL DRAG# = 0.99
 
#CONSTANT K = 0.70
 
type Vector2D
   x as float
   y as float
endtype
 
 
type BallObject
   velocity as Vector2D
   position as Vector2D
   radius as float
endtype
 
 
ball as BallObject
ball.radius = 16
ball.position.x = 320
ball.position.y = 240
 
 
 
sync on
sync rate 60
 
repeat
   cls
 
   rem update position
   ball.position.x = ball.position.x + ball.velocity.x
   ball.position.y = ball.position.y + ball.velocity.y
 
   rem apply gravity
   inc ball.velocity.y, GRAVITY#
 
   rem apply forces
   ball.velocity.x = ball.velocity.x*DRAG#
   ball.velocity.y = ball.velocity.y*DRAG#
 
 
 
   REM ===== Boundaries =====
   rem left wall
   if ball.position.x <= ball.radius
      ball.position.x = ball.radius
      ball.velocity.x = ball.velocity.x*-1
   endif
   rem right wall
   if ball.position.x >= screen width()-ball.radius
      ball.position.x = screen width()-ball.radius
      ball.velocity.x = ball.velocity.x*-1
   endif
   rem top wall
   if ball.position.y <= ball.radius
      ball.position.y = ball.radius
      ball.velocity.y = ball.velocity.y*-1
   endif
   rem bottom wall (make it have flipper force)
   if ball.position.y >= screen height()-ball.radius
      ball.position.y = screen height()-ball.radius
      ball.velocity.y = -20
   endif
 
 
 
 
   if spacekey() = 1 and flag = 0
      flag = 1
      ball.velocity.x = ball.velocity.x + sin(pa#)*20
      ball.velocity.y = ball.velocity.y + cos(pa#)*20
   endif
   if spacekey() = 0 then flag = 0
 
 
   set cursor 0,0
   print ball.velocity.x
   print ball.velocity.y
 
 
 
   ink rgb(255,0,0),0
   circle ball.position.x, ball.position.y, ball.radius
   x# = ball.position.x + sin(pa#)*ball.radius
   y# = ball.position.y + cos(pa#)*ball.radius
   line ball.position.x,ball.position.y,x#,y#
   if mouseclick() = 1 then pa# = wrapvalue(pa#+3)
   if mouseclick() = 2 then pa# = wrapvalue(pa#-3)
 
   sync
until returnkey()