CameraToonMovement by The Sab22nd Aug 2008 1:38
|
---|
Summary Pass an object and/or camera number to the functions to facilitate basic mouse and keyboard movement. Description I made this program so I could pass it along to other projects. Users should call function InitializeMovement() before the main loop, and call either function MoveCamera(CamNo) for just a free floating camera or function MoveToon(ToonNo, CamNo) to control an object with a follow camera. With Toon Movement, hold the Left Mouse Button to move the camera around the toon without turning the toon. Hold the Right Mouse Button to turn the toon itself. Hold both buttons to move the object forward. The arrow keys will also move the object/camera. I used scancodes to map the arrowkeys, so that the defaults can be changed. There is a full set of Get and Set functions for all the variables. Note that this program does not do anything with collisions. Probably a lot of this kind of code out there, but let me know if it is useful. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com `Toon and Camera movement functions `*** Test parameter declarations *** `camno = 0 `toon = 1 `*** Direction constants *** #constant _STOP = 0 #constant _FORWARD = 1 #constant _BACK = 2 #constant _RIGHT = 3 #constant _LEFT = 4 `*** Global variables used by CamToonMovement *** global movespeed as float global upperlimit as integer global lowerlimit as integer global zoomdistance as integer global zoominterval as float global maxzoom as integer global minzoom as integer global currentzoom as integer global toonmoving as byte global FKey as byte global BKey as byte global LKey as byte global RKey as byte `*** Test objects *** `make object cube toon,100 `make object plain 100,1000,1000 `xrotate object 100, 90 `do ` MoveToonWithMouse(toon, camno, movespeed, upperlimit, lowerlimit, zoominterval, maxzoom, minzoom) ` MoveToonWithKeyboard(toon, movespeed) ` Diagnose(toon, camno, movespeed, upperlimit, lowerlimit, zoominterval, maxzoom, minzoom) `loop `end `*** Call this function to initialize all variables *** function InitializeMovement() movespeed = 0.1 upperlimit = 270 lowerlimit = 90 zoomdistance = 500 zoominterval = 1.0 maxzoom = 1000 minzoom = 0 currentzoom = zoomdistance toonmoving = _STOP FKey = 200 BKey = 208 LKey = 203 RKey = 205 endfunction `*** Call this function in main loop to control camera only *** function MoveCamera(camno) `*** Reset movement *** toonmoving = _STOP MoveCameraWithMouse(camno) MoveWithKeyboard() if toonmoving = _FORWARD move camera camno, movespeed endif if toonmoving = _BACK move camera camno, -movespeed endif if toonmoving = _LEFT move camera left camno, movespeed endif if toonmoving = _RIGHT move camera right camno, movespeed endif endfunction `*** Sub-function controlling the camera with mouse *** function MoveCameraWithMouse(camno) `*** If no Mouse Buttons are pressed, soak mousemove(), (prevents jerkiness) if mouseclick() = 0 y = mousemovey() x = mousemovex() endif `*** Turn Camera if Right Mouse Button is pressed *** if mouseclick() = 2 PointCameraUsingMouse(camno) else hide mouse endif `*** Move and Turn Camera if both mouse buttons are pressed *** if mouseclick() = 3 PointCameraUsingMouse(camno) toonmoving = _FORWARD else hide mouse endif endfunction `*** Sub-function adjusting the angle of the camera *** function PointCameraUsingMouse(camno) `*** Get current camera angles *** XcurrentAngle# = camera angle x(camno) YcurrentAngle# = camera angle y(camno) `*** Calculate the new angle based on mouse movement *** XnewAngle# = wrapvalue(XcurrentAngle# + mousemovey()) YnewAngle# = wrapvalue(YcurrentAngle# + mousemovex()) `*** Ensure XnewAngle is within upper and lower limit *** TestX#=WrapValue(XnewAngle#-180) if TestX# > (lowerlimit+180) then XnewAngle#=lowerlimit if TestX# < (upperlimit-180) then XnewAngle#=upperlimit `*** Rotate camera *** xrotate camera camno, XnewAngle# yrotate camera camno, YnewAngle# endfunction `*** Call this function in Main Loop to control selected toon and camera with both mouse and keyboard *** function MoveToon(toon, camno) `*** Reset movement *** toonmoving = _STOP MoveToonWithMouse(toon, camno) MoveWithKeyboard() if toonmoving = _FORWARD move object toon, movespeed endif if toonmoving = _BACK move object toon, -movespeed endif if toonmoving = _LEFT move object left toon, movespeed endif if toonmoving = _RIGHT move object right toon, movespeed endif endfunction `*** Sub-function for controlling toon with mouse *** function MoveToonWithMouse(toon, camno) `*** If no Mouse Buttons are pressed, soak mousemove(), (prevents jerkiness) if mouseclick() = 0 camlock = 0 y = mousemovey() x = mousemovex() endif `*** Free spin camera around Toon when left mouse button pressed *** if mouseclick() = 1 camlock = 0 hide mouse PointCameraUsingMouse(camno) else show mouse endif `*** Turn Toon if Right Mouse Button is pressed *** if mouseclick() = 2 camlock = 1 hide mouse PointToonWithMouse(toon, camno) else show mouse endif `*** Move and Turn Toon if both mouse buttons are pressed *** if mouseclick() = 3 camlock = 1 hide mouse PointToonWithMouse(toon, camno) toonmoving = _FORWARD else show mouse endif FollowCam(toon, camno, camlock) FollowCamZoom(camno) endfunction `*** Sub-function adjusting look direction for toon *** function PointToonWithMouse(toon, camno) `*** Turn Toon in same direction as camera yrotate object toon, camera angle y(camno) `*** Get current toon angles *** XcurrentAngle# = camera angle x(camno) YcurrentAngle# = object angle y(toon) XnewAngle# = wrapvalue(XcurrentAngle# + mousemovey()) YnewAngle# = wrapvalue(YcurrentAngle# + mousemovex()) `*** Ensure XnewAngle is within upper and lower limit *** TestX#=WrapValue(XnewAngle#-180) if TestX# > (lowerlimit+180) then XnewAngle#=lowerlimit if TestX# < (upperlimit-180) then XnewAngle#=upperlimit yrotate object toon, YnewAngle# xrotate camera camno, XnewAngle# endfunction `*** Sub-function keeping the camera following the toon *** function FollowCam(toon,camno,camlock) `*** Set the camera to follow the toon and face the same direction *** tX#= object position x(toon) tY#= object position y(toon) tZ#= object position z(toon) tA#= object angle y(toon) position camera camno,tX#,tY#,tZ# `*** If camera is not free spinning, set camera angle same as toon angle if camlock = 1 yrotate camera tA# endif endfunction `*** Sub-function controlling camera zoon *** function FollowCamZoom(camno) `*** Camera zoom function on toon *** mz = mousemovez() if mz <> 0 zoomdistance = zoomdistance-(mz/zoominterval) if zoomdistance > maxzoom then zoomdistance = maxzoom if zoomdistance < minzoom then zoomdistance = minzoom endif If currentzoom < zoomdistance currentzoom = currentzoom + 1 endif If currentzoom > zoomdistance currentzoom = currentzoom - 1 endif move camera camno, -currentzoom endfunction `*** Sub-function controlling movement with keyboard *** function MoveWithKeyboard() KeyCode = scancode() if KeyCode = FKey toonmoving = _FORWARD endif if KeyCode = BKey toonmoving = _BACK endif if KeyCode = LKey toonmoving = _LEFT endif if KeyCode = RKey toonmoving = _RIGHT endif endfunction `*** Get functions *** function GetMoveSpeed() ms = movespeed endfunction ms function GetUpperLimit() ul = upperlimit endfunction ul function GetLowerLimit() ll = lowerlimit endfunction ll function GetZoom() zoom = zoomdistance endfunction zoom function GetZoomInterval() zi = zoominterval endfunction zi function GetMaxZoom() mz = maxzoom endfunction mz function GetMinZoom() mz = minzoom endfunction mz function GetCurrentZoom() cz = currentzoom endfunction cz `*** Set functions *** function SetMoveSpeed(ms as float) movespeed = ms endfunction function SetUpperLimit(ul as integer) upperlimit = ul endfunction function SetLowerLimit(ll as integer) lowerlimit = ll endfunction function SetZoom(zoom) zoomdistance = zoom endfunction function SetZoomInterval(zi as float) zoominterval = zi endfunction function SetMaxZoom(mz as integer) maxzoom = mz endfunction function SetMinZoom(mz as integer) minzoom = mz endfunction function SetCurrentZoom(cz as integer) zoomdistance = cz currentzoom = cz endfunction function SetFKey(key as byte) FKey = key endfunction function SetBKey(key as byte) BKey = key endfunction function SetLKey(key as byte) LKey = key endfunction function SetRKey(key as byte) RKey = key endfunction |