Here are some functions that I've been using in my projects for collision. There are probably better ones out there, but this is mine. It handles jumping and gravity (even if it is a little buggy). There are 6 commands, four of which are required to get the collision system working on an object (or many...). Here's the code:
END
rem ********************************************
rem
rem COLLISION FUNCTIONS
rem
rem ********************************************
function COLLSetup(MaxObj)
`MaxOBJ=The Highest Object Number that will need collision.
Dim COLL#(MaxOBJ,8)
exitfunction
delete memblock 1
endfunction
function COLLUpdateOld(OBJ,GravityAmount as float,GravityFlag as boolean)
if GravityFlag=1 and GravityAmount=0 then GravityAmount=0.2
COLL#(OBJ,4) = object position x(OBJ)
COLL#(OBJ,5) = Object position y(OBJ)
COLL#(OBJ,6) = object position z(OBJ)
if GravityFlag=1 then COLL#(OBJ,7)=COLL#(Obj,7)-GravityAmount
endfunction
function COLLJump(OBJ,HEIGHT as float,Key as integer)
if KEY>0
if Keystate(Key) and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
else
if controlkey() and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
ENDIF
endfunction
function COLLCompJump(OBJ,HEIGHT as float)
if COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
endfunction
Function COLLUpdateNew(OBJ)
sc_UpdateObject OBJ
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,2) = Object position y(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
sc_UpdateObject OBJ
endfunction
Function COLLHandleCollision(OBJ,GROUP,RADIUS as float,TC as boolean)
`TC=Terrain Collison. (NOT PERFECTED SO JUST SET THIS TO 0)
OFFSET#=0
sc_UpdateObject obj
COLL#(OBJ,2)=COLL#(OBJ,2)+COLL#(OBJ,7)
position object OBJ,COLL#(OBJ,1),COLL#(OBJ,2),COLL#(OBJ,3)
if TC=0
Collide=SC_SphereSlide(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,1),COLL#(OBJ,5)+Offset#,COLL#(OBJ,3),RADIUS,OBJ)
COLL#(OBJ,8)=0
if Collide>0
COLL#(OBJ,1)=sc_GetCollisionslideX()
COLL#(OBJ,3)=SC_GetCollisionSlideZ()
endif
Collide=SC_SphereSlide(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,4),COLL#(OBJ,2)+Offset#,COLL#(OBJ,6),RADIUS,OBJ)
Collide5=SC_SphereSlide(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,4),COLL#(OBJ,2)+Offset#-0.4,COLL#(OBJ,6),RADIUS,OBJ)
if collide>0 or collide5>0
COLL#(OBJ,7)=0 : COLL#(obj,8)=1
COLL#(OBJ,2)=sc_GetCollisionSlideY()-Offset#
endif
Collide2=SC_RayCastGroup(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,1),COLL#(OBJ,2)+Offset#,COLL#(OBJ,3),OBJ)
if collide2>0 and GROUP=1
COLL#(OBJ,7)=0 : COLL#(obj,8)=1
COLL#(OBJ,1)=COLL#(OBJ,4)
COLL#(OBJ,2)=COLL#(OBJ,5)-Offset#
COLL#(OBJ,3)=COLL#(OBJ,6)
endif
endif
position object OBJ,COLL#(OBJ,1),coll#(obj,2),COLL#(OBJ,3)
if TC>0
COLL#(OBJ,8)=1
i#=SC_IntersectObject(1,COLL#(OBJ,1),-10000,COLL#(OBJ,3),COLL#(OBJ,1),10000,COLL#(OBJ,3))
i#=i#+10000
position object OBJ,COLL#(OBJ,1),i#,COLL#(OBJ,3)
endif
sc_UpdateObject OBJ
endfunction
And here's an example:
rem Setup the demo...
sync on
sync rate 60
autocam off
rem Setup the collision system to handle object numbers 1-19.
COLLSetup(19)
rem Make the start box.
make object box 100,100,1,100
position object 100,0,150,0
sc_Setupobject 100,1,2
color object 100,rgb(255,0,255)
rem Make the player
Make object sphere 1,10
position object 1,0,175,0
color object 1,rgb(0,255,0)
sc_setupobject 1,3,1
rem Make a light blue skysphere
MAKE OBJECT SPHERE 200,-2400
disable object zwrite 200
color object 200,rgb(155,155,255)
rem Make lots of random objects that will fall
for o = 2 to 19
make object sphere o,10
position object o,rnd(10)*50,400+rnd(100),rnd(10)*50
sc_setupobject o,2,1
color object o,rgb(0,0,255)
next o
rem Make 40 boxes, position them in random spots, and set them to use box collision.
for c = 20 to 59
make object box c,50+rnd(100),rnd(200),50+rnd(100)
position object c,rnd(10)*50,rnd(100),rnd(10)*50
if rnd(3)=0 then xrotate object c,rnd(90)
sc_setupobject c,1,2
color object c,rgb(100+rnd(155),0,rnd(100))
next c
rem Set the player's movement speed to 2.
Speed=2
DO
set cursor 10,10
print "FPS: ",Screen FPS()
rem Update the objects' old positions
for o = 1 to 19
`COLLUpdateOld(Object Number,Gravity Amount (0 will go to the default: 0.2),Gravity Flag (0=NO GRAVITY,1=GRAVITY)
COLLUpdateOld(o,0.2,1)
next o
rem Allow the player to move around.
if upkey() then move object 1,Speed
if downkey() then move object 1,-Speed
if leftkey() then yrotate object 1,wrapvalue(object angle y(1)-Speed)
if rightkey() then yrotate object 1,wrapvalue(object angle y(1)+Speed)
rem Allow the player to jump using the Spacebar ("57" is written because the spacebar's keystate is 57. Writing a 0 will default to the controlkeys)
Height=4
Key=57
COLLJump(1,Height,Key)
rem Update the objects' new positions and handle their collision
for o = 1 to 19
if controlkey() and o>1 then position object o,object position x(o),200+rnd(100),object position z(o)
rem COLLUpdateNew(Object Number)
COLLUpdateNew(o)
rem COLLHandleCollision(Object Number,Group To Check Against,Terrain Collision (not so great, set it to 0...) )
COLLHandleCollision(o,0,5,0)
next o
rem Control the camera
set camera to follow Object Position X(1),Object Position Y(1),Object Position Z(1),Object Angle Y(1),50,80,3.5,0
point camera Object Position X(1),Object Position Y(1),Object Position Z(1)
rem If you die then get reset
if object position y(1)<-50 then position object 1,0,175,0 : yrotate object 1,0
SYNC
LOOP
END
rem ********************************************
rem
rem COLLISION FUNCTIONS
rem
rem ********************************************
function COLLSetup(MaxObj)
`MaxOBJ=The Highest Object Number that will need collision.
Dim COLL#(MaxOBJ,8)
exitfunction
delete memblock 1
endfunction
function COLLUpdateOld(OBJ,GravityAmount as float,GravityFlag as boolean)
if GravityFlag=1 and GravityAmount=0 then GravityAmount=0.2
COLL#(OBJ,4) = object position x(OBJ)
COLL#(OBJ,5) = Object position y(OBJ)
COLL#(OBJ,6) = object position z(OBJ)
if GravityFlag=1 then COLL#(OBJ,7)=COLL#(Obj,7)-GravityAmount
endfunction
function COLLJump(OBJ,HEIGHT as float,Key as integer)
if KEY>0
if Keystate(Key) and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
else
if controlkey() and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
ENDIF
endfunction
function COLLCompJump(OBJ,HEIGHT as float)
if COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
endfunction
Function COLLUpdateNew(OBJ)
sc_UpdateObject OBJ
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,2) = Object position y(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
sc_UpdateObject OBJ
endfunction
Function COLLHandleCollision(OBJ,GROUP,RADIUS as float,TC as boolean)
`TC=Terrain Collison. (NOT PERFECTED SO JUST SET THIS TO 0)
OFFSET#=0
sc_UpdateObject obj
COLL#(OBJ,2)=COLL#(OBJ,2)+COLL#(OBJ,7)
position object OBJ,COLL#(OBJ,1),COLL#(OBJ,2),COLL#(OBJ,3)
if TC=0
Collide=SC_SphereSlide(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,1),COLL#(OBJ,5)+Offset#,COLL#(OBJ,3),RADIUS,OBJ)
COLL#(OBJ,8)=0
if Collide>0
COLL#(OBJ,1)=sc_GetCollisionslideX()
COLL#(OBJ,3)=SC_GetCollisionSlideZ()
endif
Collide=SC_SphereSlide(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,4),COLL#(OBJ,2)+Offset#,COLL#(OBJ,6),RADIUS,OBJ)
Collide5=SC_SphereSlide(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,4),COLL#(OBJ,2)+Offset#-0.4,COLL#(OBJ,6),RADIUS,OBJ)
if collide>0 or collide5>0
COLL#(OBJ,7)=0 : COLL#(obj,8)=1
COLL#(OBJ,2)=sc_GetCollisionSlideY()-Offset#
endif
Collide2=SC_RayCastGroup(GROUP,COLL#(OBJ,4),COLL#(OBJ,5)+Offset#,COLL#(OBJ,6),COLL#(OBJ,1),COLL#(OBJ,2)+Offset#,COLL#(OBJ,3),OBJ)
if collide2>0 and GROUP=1
COLL#(OBJ,7)=0 : COLL#(obj,8)=1
COLL#(OBJ,1)=COLL#(OBJ,4)
COLL#(OBJ,2)=COLL#(OBJ,5)-Offset#
COLL#(OBJ,3)=COLL#(OBJ,6)
endif
endif
position object OBJ,COLL#(OBJ,1),coll#(obj,2),COLL#(OBJ,3)
if TC>0
COLL#(OBJ,8)=1
i#=SC_IntersectObject(1,COLL#(OBJ,1),-10000,COLL#(OBJ,3),COLL#(OBJ,1),10000,COLL#(OBJ,3))
i#=i#+10000
position object OBJ,COLL#(OBJ,1),i#,COLL#(OBJ,3)
endif
sc_UpdateObject OBJ
endfunction
No media required.