TGC Codebase Backup



jump program revisited by Muncher

3rd Jul 2005 21:00
Summary

an edited version of a jumping/colliding program



Description

added features, (*Ahem buttons*)



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    remstart
    Author :

               Frederik Lundgaard Hansen

                     A.K.A. KLU 007

              Special thanks to Aura. Couldn't have made this without your help ;)

A short description of the program :

  This program allows an object to jump around on a matrix. There will be some obstacles
that the object will be able to land on, and jump from one object to another.
It's basicly a simple collision snippet, but it's only meant for collisions from above,
when the player lands on the object.
Also, this code contains an easy jump-code, and in general the code SHOULD be easy to
understand, as I've remmed the most spectacular parts of the code. Besides, I've diveded,
well, actually the hole program into sub-routines, so you can pick out any part of the code
that you'll like to use.

  I hope that someone will be able to use this code, either for jumping or the collision.
Even if not, I'll make some updates when I get some more ideas.

Thank you.

    Editor :

               Alex B. ******

                     A.K.A --

               Things changed:

               * Buttons added for easy changing of constants in program

                  I know buttons don't sound like a lot, but it is certainly
               helpful to have them.

                     Warning: Buttons may change at different
                        speeds, and buttons activate without any clicking
                           needed.

                  Reason: No good reason for the speed of the buttons, but
                  as for the "wave over" thing, I thought it would be more
                  helpful. ****Buttons also change colors!**** (Oooooh!)

               Next time,
               Alex

remend


gosub settings
gosub make_matrix
gosub make_player
gosub make_obstacles
gosub gravity_settings
gosub additional_settings_and_variables
gosub the_main_loop

settings:
`set the screen display mode to 1024x768, and set sync on, and the sync rate to 66:
set display mode 1024,768,32
sync on
sync rate 60
return

make_matrix:
make matrix 1,200,200,10,10
position matrix 1,-50,-2.5,-50
return

make_player:
make object cube 1,5
color object 1,RGB(15,139,1)
return

make_obstacles:
`make the obstacles, and position them on the ground:
make object box 2,20,10,20
position object 2,80,(object size y(2)/2),30
make object box 3,20,10,20
position object 3,100,(object size y(3)/2),0
make object box 4,20,10,20
position object 4,20,(object size y(4)/2),50

make object sphere 5,20
position object 5,100,-100,50

return

gravity_settings:
`set the gravity. the gravity will decide how fast the thrust will decrement while
`the player is in the air:
gravity#=.5
`the thrust will be the amount of power stored in the player object.
`this will decide how high the object will jump:
thrust#=4.0
`set the jump variable to 0. this variable will tell the program if the object is jumping or not:
jump = 0
return

additional_settings_and_variables:
`this is the speed our player object will be moved forward with:


movespeed=2

`set the variables for the object numbers
obj1=1
Land1=2
Land2=5

`make an array to store the player object positions in
Dim ObjectPosition#(obj1,3)
dim fric(0) as integer
fric(0)=1000
dim sm(0) as word
sm(0)=10
return


the_main_loop:


do

SaveObjectPosition(1)

gosub camera_stuff

gosub control_player

MultiSlidingCollision(obj1,land1,land2)

gosub check_jumping

 remstart
un-rem this if you have an object as a floor. it'll make sure that you keep the exact same height
all the time. it checks the distance to the floor, and if you're too close, it moves
you away:
   distance#=intersect object(Land1,object position x(obj1),object position y(obj1),object position z(obj1),object position x(obj1),object position y(obj1)-1,object position z(obj1))
   set cursor 10,20 : print distance#
   if distance#=0
   else
   if distance# < 1
      position object obj1,object position x(obj1),object position y(obj1)+1-distance#,object position z(obj1)
   endif
   endif
remend

gosub gravity

gosub collision_routine

center text screen width()/2,100, "Jumping Demo revisited."

ink -1,0
box 2,2,74,24
if mousex()>=2 and mousex()<=74 and mousey()>=2 and mousey()<=24
   if fric(0)<1000
      fric(0)=fric(0)+1
   endif
   ink 0xff00ff00,0
   Ov_fu=1
else
   Ov_fu=0
   ink 0x66000000,0
endif
center text 38,6,"Fric. Up"

ink -1,0
box 2,26,74,48
if mousex()>=2 and mousex()<=74 and mousey()>=26 and mousey()<=48
   if fric(0)>0
      fric(0)=fric(0)-1
   endif
   ink 0xffff0000,0
   Ov_fd=1
else
   Ov_fd=0
   ink 0x66000000,0
endif
center text 38,30,"Fric. Dn"


col=0
if Ov_fu = 1 then col=4278255361
if Ov_fd = 1 then col=4294901761
ink col-1,0
text 76,6, "Friction:"
text 76,30, str$(fric(0)/10)


ink -1,0
box 254,2,328,24
if mousex()>=254 and mousex()<=328 and mousey()>=2 and mousey()<=24
   if sm(0)<100
      sm(0)=sm(0)+1
   endif
   ink 0xffff0000,0
   Ov_ju=1
else
   Ov_ju=0
   ink 0x66000000,0
endif
center text 290,6,"Jump Up"

ink -1,0
box 254,26,328,48
if mousex()>=254 and mousex()<=328 and mousey()>=26 and mousey()<=48
   if sm(0)>0
      sm(0)=sm(0)-1
   endif
   ink 0xffff0000,0
   Ov_jd=1
else
   Ov_jd=0
   ink 0x66000000,0
endif
center text 290,30,"Jump Dn"

col=0
if Ov_ju = 1 then col=4278255361
if Ov_jd = 1 then col=4294901761
ink col-1,0
text 330,6, "Jump Pr."
text 330,30, str$(sm(0)/10)


sync
loop


`MY SUB-ROUTINES :

control_player:
set cursor 0,0
print screen fps()

`simple player control:
if downkey()=1 then inc sp#,0.7
if upkey()=1 then dec sp#,0.7

if sp#>movespeed then sp#=movespeed
if sp#<-movespeed then sp#=-movespeed
move object 1,sp#
sp#=(sp#*fric(0))/999

if leftkey()=1
yrotate object 1,object angle y(1)-4
endif

if rightkey()=1
yrotate object 1,object angle y(1)+4
endif
`if spacekey, then start the jumping process:
if keystate(57)
jump = 1
endif

return



camera_stuff:
set camera to follow object position x(1),object position y(1),object position z(1),object angle y(1),-30,20,10,0
return




collision_routine:
set object radius 1,6
`check for collisions on all the obstacles, and the player object:
for object= 2 to 5`change this if you've made new obstacles
`if any of the objects are colliding with the player:
if object collision(1,object) and object position y(1)>(object position y(object)+object size y(object)/2) and object exist(object)
`and the spacekey is NOT pressed, then set the thrust to zero, and the object will fall with the amount of zero:
if keystate(57)=0
thrust#=0
`if spacekey IS pressed, then set the thrust to 4, and let the jump-routine handle the rest:
 else
thrust#=sm(0)/10
endif
endif


`and then repeat the process for the next object:
next object
set object radius 1,2
return


gravity:
`check if the player is jumping(or if the player is not standing on the ground):
if object position y(1)>get ground height(1,object position x(1),object position y(1))
`if the object is in the air, then decrease the thrust with the gravity, and the object will fall down:
thrust#=thrust#-gravity#
`if the object is not in the air, then reset the thrust, and tell that the object is not in the air(jump=0)
else
thrust#=sm(0)/10
jump = 0
endif
return


check_jumping:
`if jump is positive, then move the player up with the amount of thrust.
`the object will then fall down, as the thrust gets decreased to a negative value
`by the gravity:
if jump = 1
move object up 1,thrust#
endif

`if jump is negative, then position the player on the ground:
if jump = 0
position object 1,object position x(1),get ground height(1,object position x(1),object position y(1)),object position z(1)
endif
return



function MultiSlidingCollision(obj,LandStart,LandEnd)

`store the player objects position for use later on:
X# = object position x(obj)
Y# = object position y(obj)
Z# = object position z(obj)

`position the object in the stored positions
position object obj,ObjectPosition#(obj,1),ObjectPosition#(obj,2),ObjectPosition#(obj,3)

`check for a collision between the player and the obstacles on the X-axis,
`and if a collision is detected, reset the players position:
   position object obj,X#,object position y(obj),object position z(obj)
for land = LandStart to LandEnd
if land <> obj
if object exist(land)
   if object collision(land,obj)
      position object obj,ObjectPosition#(obj,1),object position y(obj),object position z(obj)
   endif
endif
endif
next land

`and the same thing for the Z-axis:
   position object obj,object position x(obj),object position y(obj),Z#
for land = LandStart to LandEnd
if land <> obj
if object exist(land)
   if object collision(land,obj)
      position object obj,object position x(obj),object position y(obj),ObjectPosition#(obj,3)
   endif
endif
endif
next land

`and the Y-axis
   position object obj,object position x(obj),Y#,object position z(obj)
for land = LandStart to LandEnd
if land <> obj
if object exist(land)
   if object collision(land,obj)
      position object obj,object position x(obj),ObjectPosition#(obj,2),object position z(obj)
   endif
endif
endif
next land

endfunction



Function SaveObjectPosition(objnum)
`this will save the players position for later use:
   ObjectPosition#(objnum,1) = object position x(objnum)
   ObjectPosition#(objnum,2) = object position y(objnum)
   ObjectPosition#(objnum,3) = object position z(objnum)

Endfunction