Hi all,
following my
last post regarding step-by-step movement in a 3D matrix, I would like to ask for your advice once more.
I'm trying to get to grips with controlling the camera: the code in my example creates a matrix in which a user can move forwards, backwards and sideways by pressing the WASD or arrow-keys. However, the user can also rotate the camera by pressing Q, E or Home and PgUp.
This last part is causing me trouble: I'd like the camera to move fore- & backwards etc. in the direction
it is facing when a user has rotated it. In the example provided, the camera is merely re-positioned along the z and x axis. Clearly I'm missing something here... but what? <ponders>
`------------------------------------------------------
` Set screen and variables for timer-based movement
`------------------------------------------------------
autocam on
color backdrop 0
sync rate 60
make matrix 1,5000,5000,50,50
set matrix height 1,25,25,500
update matrix 1
#constant StepSpeed = 10
global oldTime
global difTime
oldTime = timer()
global MoveAllowed
global CamAngT as Float
global CamAngX as Float
global CamAngY as Float
global CamAngZ as Float
CamAngX=0
CamAngY=0
CamAngZ=0
global CamPosX as Float
global CamPosY as Float
global CamPosZ as Float
CamPosX=2500
CamPosY=500
CamPosZ=-500
`------------------------------------------------------
` Loop for displaying matrix and checking for key input
`------------------------------------------------------
do
difTime = timer() - oldTime
oldTime = timer()
CamPosT=camera position z(0)
if keystate(17) or keystate(72) or keystate(200) then MoveForward()
if keystate(31) or keystate(45) or keystate(80) or keystate (208) then MoveBackward()
if keystate(30) or keystate(75) or keystate(203) then MoveLeft()
if keystate(32) or keystate(77) or keystate(205) then MoveRight()
if keystate(16) or keystate(71) then TurnLeft(CamAngT)
if keystate(18) or keystate(73) then TurnRight(CamAngT)
rotate camera 0, CamAngX, CamAngY, CamAngZ
position camera 0, CamPosX, CamPosY, CamPosZ
ink rgb(255,255,255),0
text 0, 0, "FPS: " + str$(screen fps())
text 0, 20, "Cam.PosX: " + str$(camera position x(0))
text 0, 30, "Cam.PosY: " + str$(camera position y(0))
text 0, 40, "Cam.PosZ: " + str$(camera position z(0))
text 0, 60, "Cam.AngX: " + str$(camera angle x(0))
text 0, 70, "Cam.AngY: " + str$(camera angle y(0))
text 0, 80, "Cam.AngZ: " + str$(camera angle z(0))
text 0,100, "Use WASD or arrow-keys to move camera"
text 0,110, "Use Q and E or Home and PgUp to turn camera"
sync
loop
delete matrix 1
autocam off
end
`------------------------------------------------------
` Functions for movement
`------------------------------------------------------
Function MoveForward()
CamPosZ=CamPosZ+StepSpeed
MoveAllowed = Timer()+50
endfunction
Function MoveBackward()
CamPosZ=CamPosZ-StepSpeed
MoveAllowed = Timer()+50
endfunction
Function MoveLeft()
CamPosX=CamPosX-StepSpeed
MoveAllowed = Timer()+50
endfunction
Function MoveRight()
CamPosX=CamPosX+StepSpeed
MoveAllowed = Timer()+50
endfunction
Function TurnLeft(CamAngT)
CamAngT=camera angle y(0)
CamAngY=wrapvalue(CamAngT)
CamAngY=CamAngY-StepSpeed/2
MoveAllowed = Timer()+50
endfunction
Function TurnRight(CamAngT)
CamAngT=camera angle y(0)
CamAngY=wrapvalue(CamAngT)
CamAngY=CamAngY+StepSpeed/2
MoveAllowed = Timer()+50
endfunction