`**************************************
`* Title  : bouncing_clock
`* Author : latch
`* Date   : 1/21/2007
`* Version:
`**************************************
 
rem ------ Set Up Display -------------
autocam off
set display mode 800,600,32
sync on
sync rate 60
hide mouse
 
rem =============================================================
rem = SETUP
rem =============================================================
   rem object attribute arrays
   dim mass#(6)
   dim force#(6)
   dim accel#(6)
   dim ivel#(6)
   dim fvel#(6)
      rem initialize final velocity
      for obj = 1 to 6
         fvel#(obj)=-1
      next obj
 
   dim sideways(6)
   dim rot#(6)
   dim time#(6)
   dim distance#(6)
   dim x#(12)
   dim y#(12)
   dim z#(12)
 
   randomize timer()
 
   rem make a ground for reference
   box 2,2,9,9
   get image 1000,0,0,11,11
   make matrix 1,1000,1000,10,10
   prepare matrix texture 1,1000,1,1
   update matrix 1
 
   rem info
   set camera view 0,0,1,1
   center text screen width()/2,screen height()/2,"Initializing Display..."
 
   gosub _digital_numbers
   gosub _blocks
   position camera 500,50,350
 
   `sync
 
rem =============================================================
rem = MAIN
rem =============================================================
 
   set camera view 0,0,screen width(),screen height()
 
   do
      gosub _time_pos
      for obj = 1 to 6
         gosub _bounce
         `sync
      next obj
      sync
   loop
 
   end
 
rem =============================================================
rem = SUB-ROUTINES
rem =============================================================
   _time_pos:
 
      a$=get time$()
      sizet=len(a$)
 
      rem get seconds
      sec1=val(mid$(a$,sizet))
      sec2=val(mid$(a$,sizet-1))
      rem get minutes
      min1=val(mid$(a$,sizet-3))
      min2=val(mid$(a$,sizet-4))
      rem get hours
      hour1=val(mid$(a$,sizet-6))
      hour2=val(mid$(a$,sizet-7))
 
      rem put numbers on cubes
      texture object 6,100+sec1
      texture object 5,100+sec2
      texture object 4,100+min1
      texture object 3,100+min2
      texture object 2,100+hour1
      texture object 1,100+hour2
 
   return
`-------------------------------------------------------------------
   _digital_numbers:
 
      rem draw everything in background
      create bitmap 1,screen width(),screen height()
 
      rem array to store number graphics
      dim digital(9,7)
      for num = 0 to 9
         for value = 1 to 7
            read digital(num,value)
         next value
      next num
 
      rem make across bar
      f_triangle(0,10*2,10,5*2,10,15*2,rgb(255,255,255))
      box 10,5*2,40*2,15*2
      f_triangle(81,10,91,20,81,30,rgb(255,255,255))
      get image 1,0,5*2,92,31
      cls
 
      rem make up-down bar
      f_triangle(10,10,0,20,20,20,rgb(255,255,255))
      box 0,21,20,80
      f_triangle(0,81,10,91,20,81,rgb(255,255,255))
      get image 2,0,0,21,92
      cls
 
      sprite 2,82,102,2
      hide sprite 2
      sprite 1,0,100,1
      hide sprite 1
 
      rem create all numbers
      for num = 0 to 9
         for value = 1 to 7
            select value
               case 1
                  if digital(num,value) = 2 then paste sprite 2,0,2
               endcase
               case 2
                  if digital(num,value) = 1 then paste sprite 1,12,0
               endcase
               case 3
                  if digital(num,value) = 2 then paste sprite 2,95,2
               endcase
               case 4
                  if digital(num,value) = 2 then paste sprite 2,0,87
               endcase
               case 5
                  if digital(num,value) = 1 then paste sprite 1,12,85
               endcase
               case 6
                  if digital(num,value) = 2 then paste sprite 2,95,87
               endcase
               case 7
                  if digital(num,value) = 1 then paste sprite 1,12,170
               endcase
            endselect
         next value
         sync
         get image 100+num,0,0,116,191
         cls
      next num
 
      rem finished with array, sprites, bitmap
      undim digital(9,7)
      delete sprite 1
      delete sprite 2
      delete bitmap 1
 
   return
`----------------------------------------------------------------
   _blocks:
 
      rem make cubes to hold digital numbers
      start=-1
      for obj = 1 to 6
         inc start
         make object cube obj,25
         position object obj,(start*32)+425,get ground height(1,500,500)+25,500
      next obj
 
   return
`----------------------------------------------------------------
   _bounce:
 
      gravity#=-9.8
 
      if fvel#(obj) <= 0
         time#(obj)=0
         mass#(obj)=10
         sideways(obj)=rnd(4)-2
 
         force#(obj)=rnd(600)
         accel#(obj)=accel_2(force#(obj),mass#(obj))
         ivel#(obj)=vel_2(0.0,accel#(obj),1.0)
 
         x#(obj)=object position x(obj)
         y#(obj)=object position y(obj)
         z#(obj)=object position z(obj)
 
         fvel#(obj)=1
 
      else
 
         time#(obj)=time#(obj)+.05
         fvel#(obj)=vel_2(ivel#(obj),gravity#,time#(obj))
         distance#(obj)=distance_2(fvel#(obj),time#(obj))
         if distance#(obj) < 0 then distance#(obj) = 0
         rot#(obj)=wrapvalue(rot#(obj)+sideways(obj))
         position object obj,x#(obj),y#(obj)+distance#(obj),z#(obj)
         rotate object obj,rot#(obj),rot#(obj),wrapvalue(rot#(obj)*-2)
      endif
 
   return
`----------------------------------------------------------------
 
 
rem =============================================================
rem = FUNCTIONS
rem =============================================================
   function accel_2(force#,mass#)
      a#=force#/mass#
   endfunction a#
`-----------------------------------------------------------------
   function distance_2(velocity#,time#)
      s#=velocity#*time#
   endfunction s#
`-----------------------------------------------------------------
   function vel_2(ivelocity#,acceleration#,time#)
      v#=ivelocity#+(acceleration#*time#)
   endfunction v#
`-----------------------------------------------------------------
   function f_triangle(x1,y1,x2,y2,x3,y3,color)
 
      sync rate 0
 
      ink color,0
      line x1,y1,x2,y2
      line x2,y2,x3,y3
      line x3,y3,x1,y1
 
      midx=(x1+x2+x3)/3
      midy=(y1+y2+y3)/3
 
      fill(midx,midy,color)
 
      sync rate 60
 
   endfunction
`-----------------------------------------------------------------------
   function fill(x,y,color)
      if point(x,y) <> color
         box x,y,x,y
         fill(x+1,y,color)
         fill(x,y+1,color)
         fill(x-1,y,color)
         fill(x,y-1,color)
      endif
   endfunction
`------------------------------------------------------------------------
rem =============================================================
rem = DATA STATMENTS
rem =============================================================
 
rem 7 positions make up the graphic for a number 3 top 3 mid 1 bottom
rem 1=horizontal section
rem 2=vertical section
rem 0=empty space (ex number 2 would be 0,1,2,2,1,0,1)
 
   data 2,1,2,2,0,2,1
   data 0,0,2,0,0,2,0
   data 0,1,2,2,1,0,1
   data 0,1,2,0,1,2,1
   data 2,0,2,0,1,2,0
   data 2,1,0,0,1,2,1
   data 2,1,0,2,1,2,1
   data 0,1,2,0,0,2,0
   data 2,1,2,2,1,2,1
   data 2,1,2,0,1,2,1