TGC Codebase Backup



Mouse by Gilfalas

6th Nov 2003 17:49
Summary

This code allows you to click anywhere on a matrix and reports the coordinates of the target area.



Description

The original version of this code was written by a user called JamesBlond. Although his method is really great it seems to have one disadvantage. It didn't allow you to use matrixes with position y<>0. With my version you can position your matrix(es) anywhere in space (x,y,z) and still accurately find the exact coordinates on where you clicked. This can come in handy for RTS games and such.

Here the original description by JamesBlond:

Here's how it works:
I basically projected the mouse coordinates onto an imaginary plane which faces the camera and is r# units away from it. hmax# and wmax# are the height and width of that plane. Then I imagined theres a 2d coordinate system on that plane and used linear interpolation to get the mousecoordinates on that plane (mw#,mh#). Then I converted these coordinates into length and height from the camera to the center of the plane (cl#,ch#) and from the center of the plane to the mouse coordinates on that plane (sl#,sh#). Then I threw these lengths and heights into 3d space taking into acount the camera rotation to get a point in actual 3d space ox#,oy#,oz#. Finaly I travelled alond the line between the camera and that point using linear interpolation again until I hit the matrix.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
` To get the mouse coordinates call the function like this:
` eg. mmcoord(mousex(),mousey(),1,200,200,4000,60)
` - scrx# and scry# are the screen coordinates
` - matnum is the matrix number
` - msizex and msizez are the x and z size of the current matrix
` - r# is the length of the vector and should ideally be equal to the camera range
` - FOV# is the field of view of the camera (make sure you get this value right!)

function mmcoord(object,scrx#,scry#,matnum,msizex,msizez,r#,FOV#)

  ax#=camera angle x()
  ay#=camera angle y()
  scrw#=screen width()
  scrh#=screen height()

  hmax# = r#*tan(FOV#/2)
  mh#   = (((scrh#/2-scry#)*2)/scrh#)*hmax#
  wmax# = r#*scrw#/scrh#*tan(FOV#/2)
  mw#   = (((scrx#-scrw#/2)*2)/scrw#)*wmax#

  cl#   = r#*cos(ax#)
  ch#   = -r#*sin(ax#)

  sl#   = mh#*cos(90-ax#)
  sh#   = mh#*sin(90-ax#)

  ox#    = camera position x() + (cl#+sl#)*sin(ay#) + mw#*cos(ay#)
  oy#    = camera position y() + (ch#+sh#)
  oz#    = camera position z() + (cl#+sl#)*cos(ay#) - mw#*sin(ay#)

  for i#=0 to 1 step (1.0/r#)
    x# = camera position x() + (ox#-camera position x())*i#
    y# = camera position y() + (oy#-camera position y())*i#
    z# = camera position z() + (oz#-camera position z())*i#
    if x#>=matrix position x(matnum) and x#<=matrix position x(matnum)+msizex and z#>=matrix position z(matnum) and z#<=matrix position z(matnum)+msizez
      if y#-matrix position y(matnum)+get ground height(matnum,x#,z#)<=0
         position object object,x#,matrix position y(matnum)+1,z#
         exitfunction
      endif
    ENDIF
  next i#

endfunction