-- MOVE CAMERA BEHAVIOR -- -- -- © July 2001, James Newton -- -- This behavior allows the user to rotate the camera around the -- center of the world, and to zoom in and out. It also provides 9 -- preset views: up and down each of the axes, a cavalier view and a -- frogman's view, and the original camera setting. -- -- It is intended primarily for examining a 3D world during authoring. -- -- Rotation is carried out by the arrow keys, zooming by keys "I" and -- "O". Using the shift key simultaneously accelerates the movement. -- -- The preset views are obtained by pressing the keys "X", "Y", "Z", -- "C" and "R". Pressing the Shift key gives a view in the opposite -- direction (except for "R" for reset). -- -- If you alter the value of pvAnchor, via the mSetAnchor() method, -- zooming and rotation can be performed relative to any point in the -- world. -- PROPERTY DECLARATIONS -- property pCamera -- Camera used by this sprite property pvAnchor -- Anchor point for rotations and zooms (origin) property ptRevert -- Transform of original camera position -- SPRITE EVENT -- on beginSprite(me) pCamera = sprite(me.spriteNum).camera -- Prepare transform for Reset pvAnchor = vector(0, 0, 0) ptRevert = pCamera.transform.duplicate() end mUseNewFile -- EVENT HANDLERS -- on exitFrame(me) ----------------------------------------------------- -- Allows the user to manipulate the camera with the arrow and other -- keys -------------------------------------------------------------------- me.mCheckKeys() end exitFrame -- PUBLIC METHOD -- on mSetAnchor(me, aPointVector) -------------------------------------- -- PARAMETER: -- must be a vector -- -- ACTION: Modifies the anchor point around which rotation and -- zooming is performed -------------------------------------------------------------------- if ilk(aPointVector, #vector) then pvAnchor = aPointVector end if end mSetAnchor -- PRIVATE METHODS -- on mCheckKeys(me) ---------------------------------------------------- -- Sent by exitFrame() -- -- ACTION: checks if the user is pressing: -- * the keys "i" or "o" to zoom in or out -- * the keys "x", "y" or "z" to display a preset view along an axis -- * the key "c" for a cavalier view and "r" to reset -- * the arrow keys to rotate the camera -------------------------------------------------------------------- -- Use Shift as an accelerator key if the shiftDown then tDelta = 10 else tDelta = 1 end if -- Zoom if keyPressed("i") then me.mZoom(-tDelta) exit else if keyPressed("o") then me.mZoom(tDelta) exit -- Preset views along axes else if keyPressed("r") then pCamera.transform = ptRevert exit else if keyPressed("c") then me.mPresetView(#cavalier) exit else if keyPressed("x") then me.mPresetView(#x) exit else if keyPressed("y") then me.mPresetView(#y) exit else if keyPressed("z") then me.mPresetView(#z) exit end if -- Panning the camera if keyPressed(123) then me.mRotate(#latitude, -tDelta) else if keyPressed(124) then me.mRotate(#latitude, tDelta) end if if keyPressed(125) then me.mRotate(#longitude, tDelta) else if keyPressed(126) then me.mRotate(#longitude, -tDelta) end if end mCheckKeys -- camera movement on mZoom(me, aDistance) ---------------------------------------------- -- Sent by mCheckKeys() if the user is pressing "i" or "o" -- -- PARAMETERS: -- is a positive or negative floating point number -- -- ACTION: Dollies the camera forward or back along a line between -- its current position and the center of the world -------------------------------------------------------------------- tVector = pCamera.worldPosition / 50 * aDistance pCamera.translate(tVector, #world) end mZoom on mRotate(me, aDirection, anAngle) ----------------------------------- -- Sent by mCheckKeys() if the user is pressing one of the arrow -- keys -- -- PARAMETERS: -- is #longitude | #latitude -- is a positive or negative floating point number -- -- ACTION: Rotates the camera -------------------------------------------------------------------- case aDirection of #latitude: -- left or right arrow pCamera.rotate(pvAnchor, vector(0, 1, 0), anAngle, #world) #longitude: -- up or down arrow tAxis = pCamera.transform.xAxis pCamera.rotate(pvAnchor, tAxis, anAngle, #world) end case end mRotate on mPresetView(me, anAxis) ------------------------------------------- -- Sent by mCheckKeys() if the user presses x, y, z or c -- -- PARAMETERS: -- is #x | #y | #z | #cavalier -- -- ACTION: Moves the camera so that the view appears to be along the -- scene's x, y or z axis, or shows a cavalier view. -------------------------------------------------------------------- tDistance = ptRevert.position.magnitude if the shiftDown then -- Allow the user to look the "wrong" way along the axis if anAxis <> #cavalier then tDistance = -tDistance end if end if case anAxis of #x: pCamera.transform.position = vector(tDistance, 1, 0) #y: pCamera.transform.position = vector(0, tDistance, 1) #z: pCamera.transform.position = vector(0, 0, tDistance) #cavalier: tDistance = tDistance / sqrt(3.0) if the shiftDown then -- look up: frogman's view tVector = vector(tDistance, -tDistance, tDistance) else -- look down: cavalier view tVector = vector(tDistance, tDistance, tDistance) end if me.pCamera.transform.position = tVector end case pCamera.pointAt(pvAnchor) end mPresetView