TGC Codebase Backup



3d camera and cursor function by hottap

9th Apr 2010 12:26
Summary

A simple function which controls the camera with the mouse and incorporates a 3d cursor. Easy to drop into your existing code - follow the REM instructions.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Simple 3d Camera & Cursor Function
REM By Matt Stockham (www.user33.com).
REM Please use as you wish.

REM User-defined types to handle vector coordinates of cursors and camera.
REM Place these declarations at the top of your code.
type TVector   x as FLOAT y as FLOAT z as FLOAT   endtype
type TVectorList   position as TVector angle as TVector distance as TVector   endtype

set display mode 800,600,32 : autocam off : sync on : sync rate 60 : hide mouse

REM declare_camera_variables() declares vector sets for cursors and camera.
declare_camera_variables()

REM The camera_cursor_object and cursor_object objects are necessary, but do not have to be spheres.
REM Feel free to substitute for any other objects. Object numbers are declared in declare_camera_variables() function.
make object sphere camera_cursor_object,10 : color object camera_cursor_object,rgb(200,0,0)
make object sphere cursor_object,10 : color object cursor_object,rgb(0,200,0)

REM This matrix is a non-essential object.
make matrix 1,5000,5000,50,50

REM Main program loop.
do
 text 10,5,"Green Sphere: Mouse Cursor; Red Sphere: Camera Focus Cursor"
 text 10,20,"[RMB+Drag]: Move Camera; [MMB+Drag]: Rotate Camera; [LMB+RMB+Drag] / [Scroll Wheel]: Zoom Camera"

 REM camera_control() function calculates and plots camera and cursor movements.
 camera_control()

 sync
loop
end




function camera_control()
 REM newzoom, mmx & mmy store the mouse movement values.
 local newzoom as FLOAT : newzoom = mousez()
 local mmx as FLOAT : mmx = mousemovex()
 local mmy as FLOAT : mmy = mousemovey()

 REM Compare mousez values and adjust camera zoom (camera.distance.z) based on mouse z axis movement.
 if newzoom > oldzoom then if camera.distance.z > camera_min_zoom then dec camera.distance.z,(newzoom - oldzoom)
 if newzoom < oldzoom then if camera.distance.z < camera_max_zoom then inc camera.distance.z,(oldzoom - newzoom)

 REM If right mouse button is pressed:
 if mouseclick() = 2
  REM Calculate new position of the camera cursor.
  camera_cursor.position.x = camera_cursor.position.x + (cos(camera.angle.y+90) * (0-mmx)) + (sin(camera.angle.y+90) * (0-mmy))
  camera_cursor.position.z = camera_cursor.position.z + (cos(camera.angle.y) * (0-mmx)) + (sin(camera.angle.y) * (0-mmy))
 endif

 REM If both right and left mouse buttons are pressed:
 if mouseclick() = 3
  REM Adjust camera zoom based on mouse x & y axis movement.
  if camera.distance.z >= camera_min_zoom and camera.distance.z <= camera_max_zoom
   camera.distance.z = camera.distance.z + ((mmx + mmy) / 2)
  else
   REM Limit camera zoom to maximum and minimum values declared in declare_camera_variables() function.
   if camera.distance.z < camera_min_zoom then camera.distance.z = camera_min_zoom
   if camera.distance.z > camera_max_zoom then camera.distance.z = camera_max_zoom
  endif
 endif

 REM If middle mouse button is pressed:
 if mouseclick() = 4
  REM Adjust camera y axis angle based on mouse x & y axis movement.
  camera.angle.y = wrapvalue(camera.angle.y + (mmy / 2) - (mmx / 2))
 endif

 REM Make sure we do not lose our mouse cursor!
 if object in screen(cursor_object)
  REM Make sure that the mouse cursor stays where it is when the camera is moving.
  if mouseclick() = 0
   REM Adjust mouse cursor position based on mouse x & y axis movement.
   cursor.position.x = cursor.position.x + (cos(camera.angle.y+90) * mmx) + (sin(camera.angle.y+90) * mmy)
   cursor.position.z = cursor.position.z + (cos(camera.angle.y) * mmx) + (sin(camera.angle.y) * mmy)
  endif
 else
  REM If the mouse cursor object does leave the screen point it at the camera cursor and move it back onto the screen.
  point object cursor_object,camera_cursor.position.x,camera_cursor.position.y,camera_cursor.position.z
  move object cursor_object,10
  REM Then record its new position values.
  cursor.position.x = object position x(cursor_object)
  cursor.position.z = object position z(cursor_object)
 endif

 REM Calculate new camera position based on camera cursor position.
 camera.position.x = camera_cursor.position.x + (cos(camera.angle.y) * camera.distance.z)
 camera.position.y = camera.distance.z
 camera.position.z = camera_cursor.position.z + (sin(camera.angle.y) * camera.distance.z)

 REM Actually position camera and cursor objects.
 position camera camera.position.x,camera.position.y,camera.position.z
 point camera camera_cursor.position.x,camera_cursor.position.y,camera_cursor.position.z
 position object camera_cursor_object,camera_cursor.position.x,camera_cursor.position.y,camera_cursor.position.z
 position object cursor_object,cursor.position.x,cursor.position.y,cursor.position.z

 REM Catch previous mouse z axis value in order to track movement.
 global oldzoom as FLOAT : oldzoom = mousez()
endfunction




function declare_camera_variables()
 REM camera_cursor is focus point for the camera.
 global camera_cursor as TVectorList
 REM cursor is position in 3d of the mouse cursor.
 global cursor as TVectorList
 REM camera is position of the camera.
 global camera as TVectorList
 REM Initial values for the camera. Change these to any values.
 camera.position.y = 200
 camera.angle.y = 270
 camera.distance.z = 200
 REM Object reference numbers for the two cursors used.
 global camera_cursor_object as DWORD : camera_cursor_object = 1
 global cursor_object as DWORD : cursor_object = 2
 REM Minimum and maximum camera zoom distances.
 global camera_min_zoom as FLOAT : camera_min_zoom = 100
 global camera_max_zoom as FLOAT : camera_max_zoom = 1000
endfunction