REM ***********************************************
REM Title: Terrain Click
REM Author: Unknown
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
sync on : sync rate 0 : set display mode 1024,768,32
 
matx=64 : matz=64 : tilesize=128
creatematrix(1,matx,matz,tilesize)
ax#=45 : ay#=0 : ox#=matx/2*tilesize : oy#=0 : oz#=matz/2*tilesize : r#=1000 : FOV#=60 : set camera fov FOV#
make object sphere 1,50 : make object sphere 2,75
 
DO
  gosub camera
  if mouseclick()=1 and mouseclick()<>3 then gosub mmcoord
  gosub stats
  sync
LOOP
 
 
function creatematrix(n,x,z,ts):
  make matrix n,x*ts,z*ts,x,z
endfunction
 
 
camera:
 
  if mouseclick()=2
    ax#=ax#+(my-mousey())
    ay#=wrapvalue(ay#+(mx-mousex()))
    if ax#>85 then ax#=85
    if ax#<5 then ax#=5
  endif
 
  mx=mousex()
  my=mousey()
 
  if upkey()=1 or (mouseclick()=0 and mousey()=0)
    ox#=ox#-5*sin(ay#)
    oz#=oz#-5*cos(ay#)
  endif
  if downkey()=1 or (mouseclick()=0 and mousey()=screen height()-1)
    ox#=ox#+5*sin(ay#)
    oz#=oz#+5*cos(ay#)
  endif
  if leftkey()=1 or (mouseclick()=0 and mousex()=0)
    ox#=ox#+5*cos(ay#)
    oz#=oz#-5*sin(ay#)
  endif
  if rightkey()=1 or (mouseclick()=0 and mousex()=screen width()-1)
    ox#=ox#-5*cos(ay#)
    oz#=oz#+5*sin(ay#)
  endif
 
  if mouseclick()=3
    r#=r#+mousemovey()
    if r#<100 then r#=100
    if r#>2500 then r#=2500
  endif
 
  if inkey$()="+" and FOV#<360
    FOV#=FOV#+0.5
    set camera fov FOV#
  endif
  if inkey$()="-" and FOV#>0
    FOV#=FOV#-0.5
    set camera fov FOV#
  endif
 
  x#=ox#+r#*sin(ay#)*cos(ax#)
  y#=oy#+r#*sin(ax#)
  z#=oz#+r#*cos(ay#)*cos(ax#)
 
  position camera x#,y#,z#
  point camera ox#,oy#,oz#
  position object 2,ox#,oy#,oz#
 
return
 
 
mmcoord:
 
  scrw#  = screen width()
  scrh#  = screen height()
  smax#  = r#*tan(FOV#/2)
  sh#    = (((scrh#/2-mousey())*2)/scrh#)*smax#
  l#     = (sh#*sin(90+atan(sh#/r#)))/sin(ax#-atan(sh#/r#))
 
  smax2# = r#*scrw#/scrh#*tan(FOV#/2)
  sw#    = (((mousex()-scrw#/2)*2)/scrw#)*smax2#
  rsh#   = sqrt(r#^2+sh#^2)
  rl#    = sqrt((camera position x()-(ox#-l#*sin(ay#)))^2+(camera position y()-oy#)^2+(camera position z()-(oz#-l#*cos(ay#)))^2)
  w#     = (sw#*rl#)/rsh#
 
  x#     = ox#-l#*sin(ay#)-w#*cos(ay#)
  y#     = oy#
  z#     = oz#-l#*cos(ay#)+w#*sin(ay#)
 
  position object 1,x#,y#,z#
 
return
 
 
stats:
 
  text 0,0,"Rotation coordinates: "+str$(ox#)+","+str$(oy#)+","+str$(oz#)
  text 0,20,"Camera angle y = "+str$(ay#)
  text 0,40,"Camera angle x = "+str$(ax#)
  text 0,60,"Camera radius = "+str$(r#)
  text 0,80,"Camera fov = "+str$(FOV#)
 
return