Hi all,
It's been quite a hectic week, so I haven't been able to experiment a lot. This evening I've been examining Quisco's code. The example provided moves very smoothly indeed but (on my machine at least) keeps moving until a key is unpressed.
I've been trying to combine the lines above with the earlier code-examples to create a 'fusion': smooth movement in a direction once a key is pressed, but for a specific distance. After reaching the distance, key is reset to 0. For this reason I used the FOR-NEXT loop, i.e.:
When key=0 and keyboard key is pressed
for move=1 to distance
take a small step
draw object & camera
next move
Reset key when no keys are being pressed, allowing movement once more
The DifTime-variable is there because I want to make a program in which I can switch between step-by-step movement and FPS-like movement. I'm not quite there yet, but wanted to share my stuff:
`------------------------------------------------------
` Set screen and variables for timer-based movement
`------------------------------------------------------
autocam off
color backdrop 0
sync rate 60 : sync on
make matrix 1,5000,5000,50,50
set matrix height 1,25,25,500
update matrix 1
#constant StepSpeed = 100
global OldTime
global DifTime
OldTime = timer()
global MoveAllowed
global OldSkoolMoves as Boolean
OldSkoolMoves = 1
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
global ObjPosX as Float
global ObjPosY as Float
global ObjPosZ as Float
ObjPosX=2500
ObjPosY=500
ObjPosZ=-500
`------------------------------------------------------
` Create an object and set the camera orientation to it
`------------------------------------------------------
make object cube 1, 1
hide object 1
global key as integer
`------------------------------------------------------
` Loop for displaying matrix and checking for key input
`------------------------------------------------------
do
difTime = timer() - oldTime
oldTime = timer()
CheckKey()
DrawCamera()
DisplayInfo()
sync
loop
delete matrix 1
autocam off
end
`------------------------------------------------------
` Functions that draws the main object & camera view
`------------------------------------------------------
Function DrawCamera()
rotate object 1, CamAngX, CamAngY, CamAngZ
position object 1, ObjPosX, ObjPosY, ObjPosZ
set camera to follow object position x(1), object position y(1), object position z(1), 0, 0, 50, 0, 0
set camera to object orientation 0, 1
position camera 0, object position x(1), object position y(1), object position z(1)
endfunction
`------------------------------------------------------
` Functions that checks key
`------------------------------------------------------
function CheckKey()
k17 = keystate(17) : k72 = keystate(72) : k200 = keystate(200) ` forward
k31 = keystate(31) : k45 = keystate(45) : k80 = keystate(80) : k208 = keystate(208) ` backward
k30 = keystate(30) : k75 = keystate(75) : k203 = keystate(203) ` left
k32 = keystate(32) : k77 = keystate(77) : k205 = keystate(205) ` right
k16 = keystate(16) : k71 = keystate(71) ` turn left
k18 = keystate(18) : k73 = keystate(73) ` turn right
if key = 0
if k17 = 1 or k72 = 1 or k200 = 1 then MoveForward(difTime) : key = 8
if k31 = 1 or k45 = 1 or k80 = 1 or k208 = 1 then MoveBackward(difTime) : key = 2
if k30 or k75 or k203 then MoveLeft(difTime) : key = 4
if k32 or k77 or k205 then MoveRight(difTime) : key = 6
if k16 or k71 then TurnLeft(difTime) : key = 7
if k18 or k73 then TurnRight(difTime) : key = 9
else
if key = 8 and k17 = 0 and k72 = 0 and k200 = 0 then key = 0
if key = 2 and k31 = 0 and k45 = 0 and k80 = 0 and k208 = 0 then key = 0
if key = 4 and k30 = 0 and k75 = 0 and k203 = 0 then key = 0
if key = 6 and k32 = 0 and k77 = 0 and k205 = 0 then key = 0
if key = 7 and k16 = 0 and k71 = 0 then key = 0
if key = 9 and k18 = 0 and k73 = 0 then key = 0
endif
if keystate(57)=0 then Space_Pressed=0
if keystate(57)=1 and Space_Pressed=0
Space_pressed=1
if timer()>MoveAllowed then OldSkoolMoves=1-OldSkoolMoves
MoveAllowed = Timer()+50
endif
endfunction
`------------------------------------------------------
` Functions for movement
`------------------------------------------------------
Function MoveForward(DifTime)
if OldSkoolMoves=1
y# = camera angle y()
ObjPosX=NewXValue(ObjPosX, y#, StepSpeed)
ObjPosZ=NewZValue(ObjPosZ, y#, StepSpeed)
position object 1, ObjPosX, 30.0, ObjPosZ - 150.0
point camera ObjPosX,30.0,ObjPosZ
else
if OldSkoolMoves=0 then move object 1,StepSpeed*difTime
endif
endfunction ObjPosZ
Function MoveBackward(DifTime)
if OldSkoolMoves=1
y# = camera angle y()
ObjPosX=NewXValue(ObjPosX, y#, -StepSpeed)
ObjPosZ=NewZValue(ObjPosZ, y#, -StepSpeed)
position camera ObjPosX, 30.0, ObjPosZ - 150.0
point camera ObjPosX,30.0,ObjPosZ
else
if OldSkoolMoves=0 then move object 1,StepSpeed*-difTime
endif
endfunction
Function MoveLeft(DifTime)
if OldSkoolMoves=1
OldY# = camera angle y()
y# = wrapvalue(OldY# + 270.0) : yrotate camera y#
ObjPosX=NewXValue(ObjPosX, y#, StepSpeed)
ObjPosZ=NewZValue(ObjPosZ, y#, StepSpeed)
yrotate camera OldY#
position camera ObjPosX, 30.0, ObjPosZ - 150.0
point camera ObjPosX,30.0,ObjPosZ
else
if OldSkoolMoves=0 then move object left 1,StepSpeed*difTime
endif
endfunction
Function MoveRight(DifTime)
if OldSkoolMoves=1
OldY# = camera angle y()
y# = wrapvalue(OldY# + 90.0) : yrotate camera y#
ObjPosX=NewXValue(ObjPosX, y#, StepSpeed)
ObjPosZ=NewZValue(ObjPosZ, y#, StepSpeed)
yrotate camera OldY#
position camera ObjPosX, 30.0, ObjPosZ - 150.0
point camera ObjPosX,30.0,ObjPosZ
else
if OldSkoolMoves=0 then move object right 1,StepSpeed*difTime
endif
endfunction
Function TurnLeft(DifTime)
CamAngY=CamAngY-StepSpeed*DifTime*1
endfunction
Function TurnRight(DifTime)
CamAngY=CamAngY+StepSpeed*DifTime*1
endfunction
`------------------------------------------------------
` Function for displaying text
`------------------------------------------------------
Function DisplayInfo()
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, "Scancode: "+str$(scancode())
text 0,110, "key : "+str$(key)
text 0,130, "Use WASD or arrow-keys to move camera"
text 0,140, "Use Q and E or Home and PgUp to turn camera"
endfunction