TGC Codebase Backup



Mouse Look and Strafing by Ronaldaveo

7th Oct 2003 17:55
Summary

Basic FPS mouse look, WASD movement and strafing for beginners.



Description

Simple Explanation:

if keystate(17)=1
x# = newxvalue(x#,wrapvalue(camera angle y()),1)
z# = newzvalue(z#,wrapvalue(camera angle y()),1)
endif

The number 17 represents the "scancode" of the keyboard key "w".
The keystate command will return a "1" if the key is currently being pressed.
x# and z# represent the camera's position in the 3D world.
The newx and newz value commands will find new co-ordinates in a 3D space based on the infomation passed into the command.
so the above basically means:
newcamera postion = new value(current position, direction, distance to travel)
Similar to mathematical vectors but with none of the working out :)



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem setup
sync on : sync rate 60

rem create test object
make object cube 1,100
set object 1,1,1,0

x# = camera position x()
y# = camera position y()
z# = camera position z()

hide mouse

do

   rem centre mouse
   position mouse 320,240

   x# = camera position x()
   y# = camera position y()
   z# = camera position z()

   `forwards (w)
   if keystate(17)=1
      x# = newxvalue(x#,wrapvalue(camera angle y()),1)
      z# = newzvalue(z#,wrapvalue(camera angle y()),1)
   endif
   `backwards (s)
   if keystate(31)=1
      x# = newxvalue(x#,wrapvalue(camera angle y()-180),1)
      z# = newzvalue(z#,wrapvalue(camera angle y()-180),1)
   endif
   `left (a)
   if keystate(30)=1
      x# = newxvalue(x#,wrapvalue(camera angle y()-90),1)
      z# = newzvalue(z#,wrapvalue(camera angle y()-90),1)
   endif
   `right (d)
   if keystate(32)=1
      x# = newxvalue(x#,wrapvalue(camera angle y()+90),1)
      z# = newzvalue(z#,wrapvalue(camera angle y()+90),1)
   endif

   rem camera rotation
   yrotate camera camera angle y() + mousemovex()*0.3
   xrotate camera camera angle x() + mousemovey()*0.3
   rem stops mouse from going upside down
   if wrapvalue(camera angle x(0))>40 and wrapvalue(camera angle x(0))<180 then xrotate camera 0,40
   if wrapvalue(camera angle x(0))>180 and wrapvalue(camera angle x(0))<280 then xrotate camera 0,280
   rem camera position
   position camera x#,y#,z#

   sync
loop