HOME
ACROSS THE BRIDGE
Use a routine from Emperor Baal's 3D math collision tutorial to move up and down an inclined surface. The model and it's textures are made with the easy to use Deled Editor.   The Demo is based on an idea suggested by xXMASONXx in the
DB Forum
`ACROSS THE BRIDGE
`   (1)Uses Emperor Baal's 3d Math Collision Routine for inclines.
`   (2)Model is made with the DeleD Editor
`   (3)Demo idea proposed by xXMasonXx on the DB Fourm
`   (4)Certain values used in the code depend on the Model's construction
`      and it's location in 3D space.  Read the Emperor's tutorial.
`   (5)DB Collision and 3d Math Collision are used together.
`   (6)No Guarantees...All code and related materials are used at your own risk.
`Author: Hop A Long
`December 14, 2004


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

`*****BEGIN LOADING THE DELED MODELS*****
   Load Object "leftbank.x",     8
   Load Object "rightbank.x",    9
   Load Object "leftspan.x",     10
   Load Object "rightspan.x",    11

`*****Make the Player*****
   Make Object Box           7,40,60,40
   ` A little high to land on the ground.
   Position Object           7,0,35,-900

`*****VARIABLES*****
   speed# = 10.0
   jg# = 0.0
   slope# = 192.0/512.0
   On_Slope$ = "FALSE"
   Game_Is$ = "ACTIVE"
`*****Camera Placement*****
   ` At the top of player, add 30.
   Position Camera 0,60,-900

`*****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.
         If Game_Is$ = "ACTIVE"
            ang_x#=wrapvalue(ang_x#+mousemovey()*0.1)
         EndIf
         ang_y#=wrapvalue(ang_y#+mousemovex()*0.1)

         Rotate camera ang_x#,ang_y#,0

      `*****STORE PLAYER POSITION*****
         If On_Slope$ = "FALSE"
            x_saved# = Object Position X(7)
            y_saved# = Object Position Y(7)
            z_saved# = Object Position Z(7)
         EndIf
         ` Reset and be ready to leave the slope.
         On_Slope$ = "FALSE"
      `********************************
      `  Simple movement
      `********************************
         If Upkey()  =1 then move camera speed#
         If Downkey()=1 then move camera speed#*(-1)
         ` Get the Camera values
         x# = camera position x()
         y# = camera position Y()
         z# = camera position z()

         `Slide Left
         If LeftKey()=1
            x# = NewXValue(x#,WrapValue(ang_y#-90),speed#)
            z# = NewZValue(z#,WrapValue(ang_y#-90),speed#)
         EndIf
         `Slide Right
         If RightKey()=1
            x# = NewXValue(x#,WrapValue(ang_y#+90),speed#)
            z# = NewZValue(z#,WrapValue(ang_y#+90),speed#)
         EndIf

         `*****Gravity Math*****
         If Game_Is$ = "ACTIVE"
            jg#=jg#-0.1
            y#=y#+jg#
         EndIf

         ` Assign the camera values to the Player's Collision Box
         player_x# = x#
         ` Remember the 'head' is higher.
         player_y# = y#-30
         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
         new_obj_id = 0

         If Object Collision(7,0)>0
            ` A collision was detected.
            obj_id = Object Collision(7,0)

            ` Test if player is on the span
            If obj_id = 10 Or obj_id = 11
               ` Turn off gravity
               On_Slope$ = "TRUE"
               ` An absolute value will work for both coming and going.
               relative_z# = ABS(player_z#)
               ` Just for screen output.
               factor# = relative_z# * slope#
               ` The Emperor's Incline Routine
               y_calc# = y_saved# + ( 192.0 - ( relative_z# * slope# ) )
               ` Use the calculated height
               Position Object 7,player_x#,y_calc#,player_z#
               ` Transfer the Player values to the Camera.
               x# = player_x#
               y# = y_calc#+30
               z# = player_z#

               ` On the slope so skip the code that tests the banks
               GOTO CONTINUE_HERE

            EndIf

           ` On the banks, stay on top and adjust for collisions if any.
            If Object Collision(7,obj_id)>0 Then Position Object 7, x_saved#,player_y#,player_z#
            If Object Collision(7,obj_id)>0 Then Position Object 7, player_x#,y_saved#,player_z#
            If Object Collision(7,obj_id)>0 Then Position Object 7, player_x#,player_y#,z_saved#

            player_x# = Object Position X(7)
            player_y# = Object Position Y(7)
            player_z# = Object Position Z(7)

            x# = player_x#
            y# = player_y#+30
            z# = player_z#

         EndIf

         CONTINUE_HERE:

         ` Place the camera with the player.
         Position camera x#,y#,z#

         ` That's All Folks.
         If y# < -700
              Game_Is$ = "OVER"
              Position Object 7,0.0,-160.0,-36.0
              Position Camera 0.0,-130.0,-36.0
              ang_x# = 0.0 : ang_y# = 0.0
         EndIf

         `*****FORMAT OUTPUT*************************************
         Center Text 320,20, "ACROSS THE BRIDGE"
         Center Text 320,40, "Mouse:Turn/Look   Up Arrow:Forward   Down Arrow:Back   L/R Arrow:Sideways"
         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#)

         Text 400,380,"relative_z# : "+str$(relative_z#)
         Text 400,400,"slope# : "+str$(slope#)
         Text 400,420,"y_saved# : "+str$(y_saved#)
         Text 400,440,"factor# : "+str$(factor#)

         If Game_Is$ = "OVER"
            Center Text 320,200, "GAME IS OVER"
         EndIf

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

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