remstart   ==============================================================
   =  Title  : 2d Matrix Oscillation
   =  Author : latch grapple
   =  Date   : 09/16/2007
   =  Update : 09/17/2007
   =  Version: .02
   ==============================================================
   Comments    The goal is to make a 2d matrix that is 3d.  Then
               oscillate the matrix and put a "boat" on it that
               bobs with the waves.  Main elements will be the
               translation of 3d space to 2d screen coordinates,
               the waves of the matrix (grid).  Still have to
               figure out the boat.
   ==============================================================
remend
 
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
   `autocam off
   set display mode 640,480,32
   set window on
   set window size 640,480
   sync on
   sync rate 60
 
rem =============================================================
rem = MAIN
rem =============================================================
   gosub _init
   do
      cls
      paste sprite 1,300,svert(pos,1)-50
      ink dgreen,0
      thick_line(300,520+(-1*svert(pos,1)),401,svert(pos,1),22)
      ink cyan,0
      gosub _draw_world
 
      text 0,0,str$(screen fps())
      text 0,20,"Camera Z :"+str$(camz#)
 
      sync
   loop
 
   end
 
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
   _init:
      rem arrays for functions and mat size
      `dim vect#(2)
      matx=498000
      matz=4400
      tilex=41
      tilez=41
      matsize=tilex*tilez
 
      rem calculate camera distance
      hwidth=screen width()/2
      hheight=screen height()/2
      fov#=3.14/2.905
      degrees#=(fov#*(180.0/3.14))/2.0
      zdist#=(hheight)/(tan(degrees#))
 
      rem inital camera position
      camx#=(matx/2)-9000
      camy#=-90000
      camz#=-1.0*zdist#
      cangy#=0
      cangx#=0
      cangz#=0
 
      rem create 2d matrix
      dim matvert(matsize,2)
      dim svert(matsize,1)
      matrix_2d(matx,matz,tilex,tilez,camx#,camy#,camz#,hwidth,hheight)
 
      rem colors
      cyan=rgb(0,255,255)
      dgreen=rgb(0,100,0)
      green=rgb(0,255,0)
 
      rem boat
      gosub _boat
      ink cyan,0
   return
`----------------------------------------------------------------
   _boat:
      ink green,0
      box 0,0,100,60
      ink dgreen,0
      box 0,61,100,100
      get image 1,0,0,101,101
      sprite 1,0,0,1
      hide sprite 1
      set sprite 1,0,0
      cls 0
   return
`----------------------------------------------------------------
   _draw_world:
      rem go through top and bottom values and connect them with lines
      `ink RGB(0,255,255),0
      for z=0 to tilez-1
         for x=1 to tilex
            pos=x+(z*tilex)
            `pos2=pos+1
            `pos3=pos+tilex
            `if x >= tilex then pos2=pos
            `if z >= tilez-1 then pos3=pos
            degree=wrapvalue(degree+9)
            matvert(pos,1)=30000*sin(degree)
            svert(pos,0)=screen_x(matvert(pos,0)*1.0,camx#,matvert(pos,2)*1.0,camz#,hwidth)
            svert(pos,1)=screen_y(matvert(pos,1)*1.0,camy#,matvert(pos,2)*1.0,camz#,hheight)
            `box svert(pos,0),svert(pos,1),svert(pos2,0),svert(pos2,1)
            `line svert(pos3,0),svert(pos3,1),svert(pos,0),svert(pos,1)
 
            dot svert(pos,0),svert(pos,1)
 
         next x
      next z
   return
 
rem =============================================================
rem = FUNCTIONS
rem =============================================================
   function screen_x(x#,viewerx#,z#,viewerz#,swhalf)
      adjz#=z#-viewerz#
      if adjz#=0
         sx#=-1
      else
         sx#=((x#-viewerx#)/adjz#*2.0)+swhalf
      endif
   endfunction sx#
`----------------------------------------------------------------
   function screen_y(y#,viewery#,z#,viewerz#,shhalf)
      adjz#=z#-viewerz#
      if adjz#=0
         sy#=-1
      else
         sy#=(((y#-viewery#)/adjz#))+shhalf
      endif
   endfunction sy#
`----------------------------------------------------------------
   function matrix_2d(matx,matz,tilex,tilez,camx#,camy#,camz#,hwidth,hheight)
      sizex=matx/tilex
      sizez=matz/tilez
      for z=0 to tilez-1
         for x=1 to tilex
            pos=x+(z*tilex)
            matvert(pos,0)=(x-1)*sizex
            matvert(pos,1)=0
            matvert(pos,2)=z*sizez
            svert(pos,0)=screen_x(matvert(pos,0)*1.0,camx#,matvert(pos,2)*1.0,camz#,hwidth)
            svert(pos,1)=screen_y(matvert(pos,1)*1.0,camy#,matvert(pos,2)*1.0,camz#,hheight)
         next x
      next z
   endfunction
`----------------------------------------------------------------
   function thick_line(x1,y1,x2,y2,thickness)
      for n=0 to thickness-1
         if x1<>x2 then line x1,y1+n,x2,y2+n
         if x1=x2 then line x1+n,y1,x2+n,y2
      next n
   endfunction