HOME
First Person Camera
Room Walk
Explore a simple room from a first person view.  Detect collisions with the walls and slide along them.
The room x files and textures are available as a
zip file.
` Move around in a room using a first person camera.
` Apply collision to slide along the walls.
` Author: Hop A Long
` December 1, 2004

`*****SET UP*****
   autocam off
   sync rate 30
   sync on
   hide mouse

` Add wall 1 to scene
   Load Object "wall.x",     1
   Make Object Collision Box 1, -20.0,-10.0,-5.0,20.0,10.0,5.0,0
   Position Object           1,0,0,19
   Rotate Object             1,0,180,0

` Add wall 2 to scene
   Load Object "wall.x",     2
   Make Object Collision Box 2, -20.0,-10.0,-5.0,20.0,10.0,5.0,0
   Position Object           2,0,0,-19

` Add wall 3 to scene
   Load Object "wall.x",     3
   Make Object Collision Box 3, -5.0,-10.0,-20.0,5.0,10.0,20.0,0
   Position Object           3,19,0,0
   Rotate Object             3,0,270,0

` Add wall 4 to scene
   Load Object "wall.x",     4
   Make Object Collision Box 4, -5.0,-10.0,-20.0,5.0,10.0,20.0,0
   Position Object           4,-19,0,0
   Rotate Object             4,0,90,0

` Add floor to scene
   Load Object "floor.x",    5
   Position Object           5,0,0,0

` Add ceiling to scene
   Load Object "ceiling.x",  6
   Position Object           6,0,19,0

` Make the Player's/Camera's Collision Box
   Make Object Box           7,5,5,5
   Position Object           7,0,10,0
   Make Object Collision Box 7, -2.0,-2.0,-2.0,2.0,2.0,2.0,0

   Position Camera 0,10,0
`*****BEGIN THE LOOP*****

      Do

      `********************************
      `  Use the Mouse to turn
      `********************************
      `  Use MOUSEMOVE to alter camera angles
      `  The view follows no object in particular and is free to roam.
         ang_x#=wrapvalue(ang_x#+mousemovey()*0.2)
         ang_y#=wrapvalue(ang_y#+mousemovex()*0.2)

         Rotate camera ang_x#,ang_y#,0

      `********************************
      `  Simple movement
      `********************************
         If Upkey()=1 then move camera 1
         If Downkey()=1 then move camera -1

         ` Get the Camera values
         x# = camera position x()
         z# = camera position z()
         y# = 10.0
         ` Assign the camera values to the Player's Collision Box
         player_x# = x#
         player_y# = y#
         player_z# = z#

         ` Position the Player's Collision Box at the Camera location
         Position Object 7, player_x#, player_y#, player_z#

         `*****CHECK THE PLAYER'S BOX FOR COLLISION*****
         obj_id = 0

         If Object Collision(7,0)>0
            ` A collision was detected.
            obj_id = Object Collision(7,0)
            ` Adjust for the collision.
            Dec player_x#,Get Object Collision X()
            Dec player_y#,Get Object Collision Y()
            Dec player_z#,Get Object Collision Z()
           ` Reposition the player's box with the adjusted values.
            Position Object 7, player_x#, player_y#, player_z#
           ` Make sure to correct the camera's position too.
            x# = player_x#
            y# = player_y#
            z# = player_z#
         EndIf

         Position camera x#,y#,z#

         `*****FORMAT OUTPUT*************************************
         Center Text 320,20, "FIRST PERSON CAMERA ROOM WALK"
         Center Text 320,40, "Mouse : Turn and Look   Up Arrow : Forward   Down Arrow : Back"
         Text 10,380,"collision with object : "+str$(obj_id)
         Text 10,400,"camera x value: "+str$(x#)
         Text 10,420,"camera y value: "+str$(y#)
         Text 10,440,"camera z value: "+str$(z#)

         `*****UPDATE THE SCREEN*****
         Sync

      Loop
`*****END OF MAIN LOOP*************