Posted: 1st Jul 2015 20:53
This editor camera movement template can be used in any new in DarkBASIC professional project; no need to write a camera movement program every time you start a new project.

The snippet provides your program with basic editor based WASD movement support with fly cam and pan cam modes. In fly mode you can move around a large area, in pan mode you track a certain coordinate while panning and moving the camera.

You invoke pan movement by supplying a vector to track.

[W] Move Forward
[A] Move Left
[S] Move Backward
[D] Move Right

(Duh!!)

[CTRL] = Move fast
[SHIFT] = Move slow
[E] = Pitch up
[W] = Pitch down
[Mouse movement] Pan and rotate

The code is self explanitory, use at will. Many thanks to IanM for his Matrix1 plugin and keyboard constants. I have also added some utility functions and shortcuts from my own project to enhance the user experience and your further development of your programs.


+ Code Snippet
Chris Tate - chris.tate@binarymodular.com
`======================
Global v3Track : v3Track = New Vector3() // Get rid of this vector if you want to fly around;
Sync Rate 128
Sync On
Global FrameTimespan
Global FrameStart 
Make Matrix 1, 100, 100, 10, 10
Position Matrix 1, -50, 0, -50
Move Camera Up 2
Make Object Cube 1, 10

Do
  
  FrameStart = HiTimer()
  
  Set Cursor 0, 0
  Print "FPS: "; Screen FPS()
  Print "Frame Timespan:"; FrameTimespan
  
  

  cameraID = 0
  
  // Pan cam
  //BasicCameraMovement( cameraID, UnitsPerSec#(20.0), UnitsPerSec#(90.0), 1, v3Track )
  
  // Fly cam
  BasicCameraMovement( cameraID, UnitsPerSec#(20.0), UnitsPerSec#(45.0), 1, 0 )
  
  Sync
  
  FrameTimespan = HiTimer() - FrameStart 
Loop


//============================================================
Function UnitsPerSec#( fUnits# )
  If fUnits# = 0 Then ExitFunction 0.0
  Local fSpeed# : fSpeed# = fUnits# * 0.001
  fSpeed# = fSpeed# * FrameTimespan
Endfunction fSpeed#
//============================================================
Function StepsPerSec( iSteps )
  If iSteps = 0 Then ExitFunction 0
  Local fSpeed# : fSpeed# = iSteps * 0.001
  fSpeed# = fSpeed# * FrameTimespan
  iSteps = fSpeed#
Endfunction iSteps
//============================================================
Function PerSec#( fUnits#, rate )
  If fUnits# = 0 Then ExitFunction 0.0
  Local fSpeed# : fSpeed# = fUnits# * 0.001
  fSpeed# = fSpeed# * rate
Endfunction fSpeed#
//============================================================
Function PerSec( iSteps, rate )
  If iSteps = 0 Then ExitFunction 0
  Local fSpeed# : fSpeed# = iSteps * 0.001
  fSpeed# = fSpeed# * rate
  iSteps = fSpeed#
Endfunction iSteps
//==================================================
Function BasicCameraMovement( c, fSpeed#, fRotateSpeed#, fMouseInfluence#, v3Track )
  If c < 0 Then ExitFunction
  If Camera Exist(c) = 0 Then ExitFunction
  If fMouseInfluence# <> 0

    If v3Track > 0 And Vector Exist(v3Track)
      //If BypassMouseZoom) = 0 Then Move Camera c, fMouseInfluence# * Clamp(MouseMoveZ(),-10,10) * fSpeed#
      Move Camera Left c, Clamp(MouseMoveX(),-10,10) * fSpeed# * fMouseInfluence#
      Move Camera Up c, Clamp(MouseMoveY(),-10,10) * fSpeed# * fMouseInfluence#
    Else
      If ControlKey() + ShiftKey() = 0
        YRotate Camera c, ( CAY + Clamp(MouseMoveX(),-10,10) *  fRotateSpeed# * fMouseInfluence#)
        XRotate Camera c, (CAX +  Clamp(MouseMoveY(),-10,10) * fRotateSpeed# * fMouseInfluence#)
      EndIf
      Move Camera c, Clamp(MouseMoveY(),-10,10) * fSpeed# * fMouseInfluence#
    Endif
  
  Endif 
    
  Move Camera c, ( KeyState(KEY_W) - KeyState(KEY_S) ) * (fSpeed# + ((ControlKey() * fSpeed# * 9)) - ((ShiftKey() * fSpeed# * 0.75)) )
  If Vector Exist(v3Track)
    Move Camera Left c, KeyState(KEY_D) - KeyState(KEY_A) * fSpeed#
    Move Camera Up c, KeyState(KEY_E) - KeyState(KEY_Q) * fSpeed#  
    Point Camera At Vector3 c, v3Track
  Else
    If fMouseInfluence# <> 0 And MouseClicked && 2
      YRotate Camera c, CAY -((KeyState(KEY_A) * fRotateSpeed# ) +  -KeyState(KEY_D) * fRotateSpeed#)
      XRotate Camera c, CAX -( (KeyState(KEY_E) * fRotateSpeed#) +  -KeyState(KEY_Q) * fRotateSpeed#  )
      Print "Rotate mode"
    Else
      Move Camera Right c, (KeyState(KEY_D) * fSpeed# )  - (KeyState(KEY_A) * fSpeed#)
      XRotate Camera c, CAX -( (KeyState(KEY_E) * fRotateSpeed#) +  -KeyState(KEY_Q) * fRotateSpeed#  )
      Print "Strafe mode"
    Endif
      
  Endif
Endfunction


#CONSTANT CPOS Camera Position X(), Camera Position Y(), Camera Position Z()
#CONSTANT CANG Camera Angle X(), Camera Angle Y(), Camera Angle Z()

#CONSTANT RCAM Rotate Camera
#CONSTANT PCAM Position Camera
#CONSTANT XRCAM XRotate Camera
#CONSTANT YRCAM YRotate Camera
#CONSTANT ZRCAM ZRotate Camera



#CONSTANT TCAML Turn Camera Left
#CONSTANT TCAMR Turn Camera Right

#CONSTANT MCAM Move Camera
#CONSTANT MCAML Move Camera Left
#CONSTANT MCAMR Move Camera Right
#CONSTANT MCAMU Move Camera Up
#CONSTANT MCAMD Move Camera Down
#CONSTANT CPX Camera Position X(c)
#CONSTANT CPY Camera Position Y(c)
#CONSTANT CPZ Camera Position Z(c)
#CONSTANT CAX Camera Angle X()
#CONSTANT CAY Camera Angle Y()
#CONSTANT CAZ Camera Angle Z()
#CONSTANT CANGX Camera Angle X(c)
#CONSTANT CANGY Camera Angle Y(c)
#CONSTANT CANGZ Camera Angle Z(c)

#CONSTANT CAM_POS_STRING Str$(Camera Position X()) + ", " + Str$(Camera Position Y()) + ", " +  Str$(Camera Position Z())
#CONSTANT CAM_INT_POS_STRING Str$(Camera Position X(),0) + ", " + Str$(Camera Position Y(),0) + ", " +  Str$(Camera Position Z(),0)

#CONSTANT SHIFT_KEY_FLAG 1
#CONSTANT CTRL_KEY_FLAG 2
#CONSTANT ALT_KEY_FLAG 4
//======================================================================
` Key codes by IanM
#constant    Key_0              11
#constant    Key_1               2
#constant    Key_2               3
#constant    Key_3               4
#constant    Key_4               5
#constant    Key_5               6
#constant    Key_6               7
#constant    Key_7               8
#constant    Key_8               9
#constant    Key_9              10
#constant    Key_A              30
#constant    Key_ABNT_C1       115
#constant    Key_ABNT_C2       126
#constant    Key_ADD            78
#constant    Key_APOSTROPHE     40
#constant    Key_APPS          221
#constant    Key_AT            145
#constant    Key_AX            150
#constant    Key_B              48
#constant    Key_BACK           14
#constant    Key_BACKSLASH      43
#constant    Key_BACKSPACE      14
#constant    Key_C              46
#constant    Key_CALCULATOR    161
#constant    Key_CAPITAL        58
#constant    Key_CAPSLOCK       58
#constant    Key_CIRCUMFLEX    144
#constant    Key_COLON         146
#constant    Key_COMMA          51
#constant    Key_CONVERT       121
#constant    Key_D              32
#constant    Key_DECIMAL        83
#constant    Key_DELETE        211
#constant    Key_DIVIDE        181
#constant    Key_DOWN          208     
#constant    Key_DOWNARROW     208
#constant    Key_E              18
#constant    Key_END           207
#constant    Key_ENTER         28
#constant    Key_EQUALS         13
#constant    Key_ESCAPE          1
#constant    Key_F              33
#constant    Key_F1             59
#constant    Key_F2             60
#constant    Key_F3             61
#constant    Key_F4             62
#constant    Key_F5             63
#constant    Key_F6             64
#constant    Key_F7             65
#constant    Key_F8             66
#constant    Key_F9             67
#constant    Key_F10            68
#constant    Key_F11            87
#constant    Key_F12            88
#constant    Key_F13           100
#constant    Key_F14           101
#constant    Key_F15           102
#constant    Key_G              34
#constant    Key_GRAVE          41
#constant    Key_H              35
#constant    Key_HOME          199
#constant    Key_I              23
#constant    Key_INSERT        210
#constant    Key_J              36
#constant    Key_K              37
#constant    Key_KANA          112
#constant    Key_KANJI         148
#constant    Key_L              38
#constant    Key_LALT           56
#constant    Key_LBRACKET       26
#constant    Key_LCONTROL       29
#constant    Key_LEFT          203      
#constant    Key_LEFTARROW     203
#constant    Key_LMENU          56
#constant    Key_LSHIFT         42
#constant    Key_LWIN          219
#constant    Key_M              50
#constant    Key_MAIL          236
#constant    Key_MEDIASELECT   237
#constant    Key_MEDIASTOP     164
#constant    Key_MINUS          12
#constant    Key_MULTIPLY       55
#constant    Key_MUTE          160
#constant    Key_MYCOMPUTER    235
#constant    Key_N              49
#constant    Key_NEXT          209
#constant    Key_NEXTTRACK     153
#constant    Key_NOCONVERT     123
#constant    Key_NUMLOCK        69
#constant    Key_NUMPAD0        82
#constant    Key_NUMPAD1        79
#constant    Key_NUMPAD2        80
#constant    Key_NUMPAD3        81
#constant    Key_NUMPAD4        75
#constant    Key_NUMPAD5        76
#constant    Key_NUMPAD6        77
#constant    Key_NUMPAD7        71
#constant    Key_NUMPAD8        72
#constant    Key_NUMPAD9        73
#constant    Key_NUMPADCOMMA   179
#constant    Key_NUMPADENTER   156
#constant    Key_NUMPADEQUALS  141
#constant    Key_NUMPADMINUS    74
#constant    Key_NUMPADPERIOD   83
#constant    Key_NUMPADPLUS     78
#constant    Key_NUMPADSLASH   181
#constant    Key_NUMPADSTAR     55
#constant    Key_O              24
#constant    Key_OEM_102        86
#constant    Key_P              25
#constant    Key_PAUSE         197
#constant    Key_PERIOD         52
#constant    Key_PGDN          209
#constant    Key_PGUP          201
#constant    Key_PLAYPAUSE     162
#constant    Key_POWER         222
#constant    Key_PREVTRACK     144
#constant    Key_PRIOR         201
#constant    Key_Q              16
#constant    Key_R              19
#constant    Key_RALT          184
#constant    Key_RBRACKET       27
#constant    Key_RCONTROL      157
#constant    Key_RETURN         28
#constant    Key_RIGHT         205 
#constant    Key_RIGHTARROW    205
#constant    Key_RMENU         184
#constant    Key_RSHIFT         54
#constant    Key_RWIN          220
#constant    Key_S              31
#constant    Key_SCROLL         70
#constant    Key_SEMICOLON      39
#constant    Key_SLASH          53
#constant    Key_SLEEP         223
#constant    Key_SPACE          57
#constant    Key_SPACEBAR       57
#constant    Key_STOP          149
#constant    Key_SUBTRACT       74
#constant    Key_SYSRQ         183
#constant    Key_T              20
#constant    Key_TAB            15
#constant    Key_U              22
#constant    Key_UNDERLINE     147
#constant    Key_UNLABELED     151
#constant    Key_UP            200     
#constant    Key_UPARROW       200
#constant    Key_V              47
#constant    Key_VOLUMEDOWN    174
#constant    Key_VOLUMEUP      176
#constant    Key_W              17
#constant    Key_WAKE          227
#constant    Key_WEBBACK       234
#constant    Key_WEBFAVORITES  230
#constant    Key_WEBFORWARD    233
#constant    Key_WEBHOME       178
#constant    Key_WEBREFRESH    231
#constant    Key_WEBSEARCH     229
#constant    Key_WEBSTOP       232
#constant    Key_X              45
#constant    Key_Y              21
#constant    Key_YEN           125
#constant    Key_Z              44