REM ***********************************************
REM Title: Matrix Mouse Click
REM Author: Unknown
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
set display mode 1024,768,32
autocam off
sync on
sync rate 60
 
REM VARIABLES
screenwidth# = screen width()
screenheight# = screen height()
screenwidth2# = screenwidth#/2.0
screenheight2# = screenheight#/2.0
scalefactor# = screenheight#/1.2
rem starting camera height position
cy# as float = 100.0
 
Global targetX# as float
Global targetY# as float
Global targetZ# as float
 
 
make matrix 1, 2000,2000,50,50
 
make object sphere 1, 50
 
 
DO
   _M_C = mouseclick()
 
   if _M_C > 0
      clickMatrix(screenwidth2#, screenheight2#, scalefactor#, offsetx#, offsety#, offsetz#)
   endif
 
   position object 1,targetX#,targetY#,targetZ#
 
 
   gosub _Control_Camera
 
   text 1, 1, "FPS: "+str$(screen fps())
 
   sync
LOOP
 
 
 
_Control_Camera:
   speed# = 5
   if upkey()=1 then inc cz#,speed#
   if downkey()=1 then dec cz#,speed#
 
   if leftkey()=1 then dec cx#,speed#
 
   if rightkey()=1 then inc cx#,speed#
 
   if shiftkey() then inc cy#, 2
   if controlkey() then dec cy#, 2
 
   a#=wrapvalue(a#+(mousemovex()/3.0))
   cxa#=cxa#+(mousemovey()/3.0)
   if cxa#<-90.0 then cxa#=-90.0
   if cxa#>90.0 then cxa#=90.0
   position camera cx#,cy#,cz#
   rotate camera wrapvalue(cxa#),a#,0
 
 
   offsetx# = cx#
   offsety# = 0
   offsetz# = cz#+150
   point camera offsetx#,offsety#,offsetz#
 
RETURN
 
function clickMatrix(screenwidth2# as float, screenheight2# as float, scalefactor# as float, offsetx# as float, offsety# as float, offsetz# as float)
   mouseposx#=mousex()-screenwidth2#
   mouseposy#=screenheight2#-mousey()
 
   dist#=sqrt((camera position x()-offsetx#)^2+(camera position y()-offsety#)^2+(camera position z()-offsetz#)^2)
 
   if mouseposy#<>0
      vectorang#=atanfull(scalefactor#,mouseposy#)
   else
      vectorang#=90.0
   endif
 
   if vectorang#+camera angle x()>90.965
      ratio#=mouseposy#/(sin((vectorang#+camera angle x() )-90.0))
      cursorposy#=ratio#*sin(180.0-vectorang#)
   else
      cursorposy#=1000000.0
   endif
 
   hyplength#=scalefactor#/sin(vectorang#)
   cursorposx#=(((ratio#*sin(90.0-camera angle x() ))/hyplength#)+1.0)*mouseposx#
 
   if mouseposx#<>0
      angtotal#=wrapvalue(camera angle y()-atanfull(mouseposy#,mouseposx#))
   else
      if mouseposy#<0
         angtotal#=wrapvalue(camera angle y()+90.0)
      else
         angtotal#=wrapvalue(camera angle y()-90.0)
      endif
   endif
 
   cameraang#=wrapvalue(360.0-camera angle y() )
 
   cosang#=cos(cameraang#)
   sinang#=sin(cameraang#)
 
   movex#=(dist#/scalefactor#)*((cosang#*cursorposx#)+((-1*sinang#)*cursorposy#))
   movez#=(dist#/scalefactor#)*((sinang#*cursorposx#)+(cosang#*cursorposy#))
 
  `\\\ 3D translated coords from 2D mouse coords \\\
   targetX#=movex#+offsetx#
   targetZ#=movez#+offsetz#
   targetY#=0
endfunction