More Code
JUMP MATH

`Illustrating the Basic Jump Demo
`Author: Hop A Long
`June 1, 2004


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


`************************************************
`  Declare Data Structures and Variables
`************************************************
`  Build arrays to hold the values of dy# and posy#
Dim Player_dy#(45)
Dim Player_posy#(45)
`  Some variables to make the code easier to follow
True = 1
False = 0
Record = False
Iterations = 0
`  dy# is a float variable used to increment the player's y position giving the
`  impression of the rise and fall in a jump.  Please see warning above.
dy#=2.0


`**************************************
`  Make the player
`**************************************
make object cylinder 2,10
position object 2,0,0,0
make object collision box 2,-5,-5,-5,5,5,5,0


`**************************************
`  Make the platform
`**************************************
make object box 5,20,2,20 : position object 5,0,-1,0 : color object 5,rgb(120,120,120)
`  Transform our platform into a Static Object
make static object 5
   objx#=object position x(5)
   objy#=object position y(5)
   objz#=object position z(5)
   objsx#=object size x(5)/2.0
   objsy#=object size y(5)/2.0
   objsz#=object size z(5)/2.0
   Make Static Collision Box objx#-objsx#,objy#-objsy#,objz#-objsz#,objx#+objsx#,objy#+objsy#,objz#+objsz#
   delete object 5



`******************************************************************************
`  THE MAIN LOOP
`******************************************************************************
   Do


      `******************************************
      `  STORE THE PRESENT LOCATION OF THE PLAYER
      `******************************************
      `  These are later used in collision detection
      oldposx#=object position x(2)
      oldposy#=object position y(2)
      oldposz#=object position z(2)


      `******************************************
      `  ASSIGN VARIABLES TO WORK WITH
      `******************************************
      posx#=object position x(2)
      posy#=object position y(2)
      posz#=object position z(2)


      `******************************************
      `  LAUNCH THE JUMP
      `******************************************
      If spacekey()=1 and dy#=0.0
         `  Set the change in y value to the starting value of 2.0.
         dy#=2.0
         `  Let the arrays start recording values
         Record = True
         `  Reset the number of Iterations to begin counting the new jump
         Iterations = 0
      Endif


      `******************************************
      `  GENERATE JUMP DATA   (dy#)
      `******************************************
      `  Modify the y value to simulate a gradually slowing
      `  rise and then gradually increasing fall.
      `  Each time through the loop this value is reduced by 0.1
      `  thus the Player will rise smaller and smaller amounts.
      `  After dy# = 0, dy# becomes an increasingly negetive value.
      `  Now the Player will desend faster and faster.
      `  When the Player collides with the platform dy# is set to 0.0
      `  and no more vertical movement is possible
      dy#=dy#-0.1
      `  Adjust the Player's height by the new change in y value
      posy#=posy#+dy#


      `******************************************
      `  STORE THE JUMP DATA
      `******************************************
      If Record = True
      `  Count the number of times we go around the loop until the player ends his jump
         Iterations = Iterations + 1
      `  Protect out arrays from being over run.
         If Iterations > 40 then Iterations = 40
      `  Load the Arrays
         Player_dy#(Iterations) = dy#
         Player_posy#(Iterations) = posy#
      Endif


      `********************************
      `  PLACE THE PLAYER USING JUMP DATA
      `********************************
      position object 2,posx#,posy#,posz#


      `********************************
      `  TEST FOR A COLLISION WITH THE PLATFORM
      `********************************
      `  Set a size for the player object
      `  This will next be used to define two collision boxes for the player, whose
      `  positions can be compared to Static Objects
      s#=object size y(2)/2.0

      `  Detect any Static Collision Boxes and Slide
      If Get Static Collision Hit(oldposx#-s#,oldposy#-s#,oldposz#-s#,oldposx#+s#,oldposy#+s#,oldposz#+s#,posx#-s#,posy#-s#,posz#-s#,posx#+s#,posy#+s#,posz#+s#)=1
         dec posx#,get static collision x()
         dec posy#,get static collision y()
         dec posz#,get static collision z()
         `  When true the command returns 1
         If get static collision y()<>0.0
            `  The player is touching the platform, keep him there by setting change in y = 0
            dy#=0.0
            `  The jump has ended, we will stop recording the Iterations of the loop
            Record = False
         Endif
      Endif

      `********************************
      `  RELOCATE THE PLAYER IF A COLLISION OCCURS
      `********************************
      position object 2,posx#,posy#,posz#


      `********************************
      `  CAMERA ROUTINES
      `********************************
      Position Camera 0,15,-40
      Point Camera 0,15,0

      `******************************************
      `  PRINT JUMP DATA TO THE SCREEN
      `******************************************
      `  Print the final values after all commands have been executed.
      Center Text 320,50, "ILLUSTRATING THE BASIC JUMP"
      Center Text 320,70, "Iterations: "+str$(Iterations)
      Center Text 320,90, "Space Key to Jump"

      For Row = 1 to Iterations
         If Player_dy#(Row)> 0
            Text 40,20,"GOING UP"
            Text 10,40," dy# "
            Text 110,40," posy# "
            Text 10,(Row*20+60),str$(Player_dy#(Row))
            Text 110,(Row*20+60),str$(Player_posy#(Row))
         Else
            Text 500,20,"C0MING DOWN"
            Text 480,40,"dy#"
            Text 580,40," posy# "
            Text 480,((Row-20)*20+80),str$(Player_dy#(Row))
            Text 580,((Row-20)*20+80),str$(Player_posy#(Row))

         Endif
      Next Row

      ` Update screen
      sync

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