Here's an update :
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.
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,50,(object size y(2)/2),10
make object box 3,20,10,20
position object 3,80,(object size y(3)/2),0
make object box 4,20,10,20
position object 4,20,(object size y(4)/2),25
return
gravity_settings:
`set the gravity. the gravity will decide how fast the thrust will devrement while
`the player is in the air:
gravity#=0.3
`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=4
`make an array to store the player object positions in
Dim ObjectPosition#(obj1,3)
return
the_main_loop:
do
SaveObjectPosition(1)
gosub camera_stuff
gosub control_player
MultiSlidingCollision(1,2,4)
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
sync
loop
`MY SUB-ROUTINES :
control_player:
set cursor 0,0
print screen fps()
`simple player control:
if keystate(17)
move object 1,-movespeed
endif
if keystate(31)
move object 1,2
endif
if keystate(30)
yrotate object 1,object angle y(1)-2
endif
if keystate(32)
yrotate object 1,object angle y(1)+2
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 4`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
print "collision"
thrust#=0
`if spacekey IS pressed, then set the thrust to 4, and let the jump-routine handle the rest:
else
thrust#=4
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#=4.0
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:
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
Now, (with help from Aura) I've built in real collisions. Not just if you jump on top of an obstacle. it runs with a smooth framerate, and the hole code is still remmed, so you should be able to understand this.
Enjoy
KLU 007