well I have several collision code examples but I use intersect object:
here's one basic (doesn't prevent player sides through objects)
_______TOP_______:
rem Generic Template
rem Player = object 10
rem Cubes = objects 100-120
_____INITIALIZE_____:
set display mode 1024,768,32
SYNC ON
rem set framerate - 0 = max achievable
SYNC RATE 60
rem set backdrop on to avoid hall of mirros effect
rem unless you are always inside a skybox or skysphere
backdrop on
rem color backdrop red,green,blue values (0-255)
color backdrop rgb(50,100,200)
rem stop autocam to position at last loaded object position
autocam off
set global collision off
rem set variables
global objnum
x#=1000.0:z#=1000.0:a#=0.0:s#=0.0:h#=0.0
cx#=0.0:cy#=0.0:cz#=0.0:ca#=0.0:camght=50
camrot=0:camdist=0:camlen=0:pdist#=0
collx#=0:collz#=0:objnum=100
rem matrix set up
gosub Setupland1
rem self explanatory
gosub makeplayer
gosub makeobjects
gosub makeambient
gosub resetcam
_____DO_LOOP_____:
DO
rem force player not to exceed matrix borders
if x#<100 then x#=100
if z#<100 then z#=100
if x#>landsize-100 then x#=landsize-100
if z#>landsize-100 then z#=landsize-100
rem continually store player position to collision variables
rem for later use
collx#=x#:collz#=z#
rem use e/E to toggle between fixed frame rate to max achievable
if inkey$()="e" then sync rate 0
if inkey$()="E" then sync rate 60
rem print some data on screen
gosub printdata
rem keyboard control of camera keys from ">" to "M"
gosub camera_control
rem camera positioning subroutine
gosub update_camera
rem player control subroutine (with arrow keys)
gosub Playercontrol
rem first check closest object with square root subroutine
gosub checkdistance
rem manage collision with hit object
gosub checkcoll
rem refresh screen
SYNC
rem start over in a loop from DO
LOOP
rem -------------ROUTINES--------------------
____SET_SCENERY___:
Setupland1:
rem ---------ground textures----------------
rem create texture for matrix from built screen image
rem color screen
cls rgb(0,100,20)
rem set ink color and draw lines in a 250 pixel wide box
inkcolor#=rgb(255,255,255)
line 0,0,0,250
line 1,1,1,250
line 2,2,2,250
line 0,0,250,0
line 1,1,250,1
line 2,2,250,2
line 0,0,250,250
line 0,250,250,0
rem grab the built image
get image 2,0,0,250,250
rem Make landscape
rem set matrix size,grid,hillness
landsize=4000:grid=30:mtxrandomize=30
make matrix 1,landsize,landsize,grid,grid
set matrix 1,1,0,0,1,1,1,1
rem grab created image ( n°2 ) and texture the matrix
prepare matrix texture 1,2,1,1
rem create random hills
randomize matrix 1,mtxrandomize
rem get matrix finished
update matrix 1
return
_____CONTROL_PLAYER___:
Playercontrol:
rem calculate next player x#,z# positions
rem accordind to his angle and speed (a# and s#)
x#=newxvalue(x#,a#,s#)
z#=newzvalue(z#,a#,s#)
rem check matrix height at player position for
rem for correct positioning of player height
h#=get ground height(1,x#,z#)
rem finally position and rotate the player (object 10)
position object 10,x#,h#+20,z#
yrotate object 10,a#
rem move player with arrow keys by variating his speed
rem up to max 8
if upkey()=1 and s#<8 then s#=s#+0.1
if downkey()=1 and s#>-8 then s#=s#-0.1
rem change player angle with left/right arrow keys
if leftkey()=1 then a#=wrapvalue(a#-1)
if rightkey()=1 then a#=wrapvalue(a#+1)
rem stop player if "ù" is pressed
if inkey$()="ù" then s#=0.0
rem press "à" for fast speed
if inkey$()="à" then s#=20.0
return
______CAMERA_ROUTINES____:
rem -------CAMERA--------
camera_control:
if mouseclick()=1
position mouse 700,100
mymousex=mousemovex()
if mymousex>0 then camrot=camrot+2
if mymousex<0 then camrot=camrot-2
mymousey=mousemovey()
if mymousey>0 then campointh=campointh+3
if mymousey<0 then campointh=campointh-3
mymousez=mousemovez()
if mymousez>0 then camdist=camdist+10
if mymousez<0 then camdist=camdist-10
endif
rem camera update
update_camera:
rem Position camera to the back of the character
ca#=wrapvalue(a#+camrot)
cx#=newxvalue(x#,ca#,camdist)
cz#=newzvalue(z#,ca#,camdist)
cy#=h#
position camera cx#,cy#+camhgt,cz#
rem Point camera at object
point camera x#,h#+camhgt+campointh,z#
return
rem camera reset
Resetcam:
rem set camera view range (near-far values)
set camera range 1,20000
rem used in camera positioning routines (negative means behind)
camdist=-120
rem camera height above h# (h#=ground level)
camhgt=80
rem rotation of camera around player
camrot=0
rem ahead view setting
campointh=0
return
_____PRINT_ON__SCREEN___:
printdata:
set cursor 0,0
print "Polys=",statistic(1)
print "FPS=",screen fps()
print "player x#= ",x#
print "player h#= ",h#
print "player z#= ",z#
print "player angle a#= ",a#
print "speed s#= ",s#
print "close object distance pdist#=",pdist#
print "last hit object objnum=",objnum
print "intersect value collcheck#=",collcheck#
print "--------------------------------------"
print " left mouse for cam control"
return
rem alternative screen printing to save fps
printdata2:
set cursor 0,0
print statistic(1)
print screen fps()
return
_____CHECK_ROUTINES_____:
rem check closest cube with sqrt distance check
checkdistance:
for n=101 to 121
xdif#=object position x(n)-x#
zdif#=object position z(n)-z#
pdist#=sqrt(xdif#*xdif#+zdif#*zdif#)
rem if distance is below 100 units register object number
rem and exit the loop for more precise collision handling
rem (checkcoll: subroutine with intersect object)
if pdist#<300 then objnum=n:return
next n
rem reset objnum and collcheck# to 0 to avoid to get stuck
rem by in the checkcoll subroutine
objnum=0
collcheck#=0
return
checkcoll:
rem don't check if "r" key is pressed
if inkey$()="r" then return
rem if walking forward check / from player position to 200 units ahead
if s#>0
tox#=newxvalue(x#,a#,200)
toz#=newzvalue(z#,a#,200)
rem check only if the sqrt distance check reports vicinity to an object
if objnum>0
rem this below returns the distance from object walls
collcheck#=intersect object(objnum,x#,50,z#,tox#,50,toz#)
endif
rem if distance check is at min value and not 0 ...
rem ...put player at collx# and collz# positions (set in main loop)
rem and drop player speed to 0 (s#)
if collcheck#<30 and collcheck#>0 then x#=collx#:z#=collz#:s#=0:hitobj=objnum
endif
rem as above but walking backwards
rem i.e. check / from player position to 200 units behind (-200)
if s#<0
tox#=newxvalue(x#,a#,-200)
toz#=newzvalue(z#,a#,-200)
if objnum>0
collcheck#=intersect object(objnum,x#,50,z#,tox#,50,toz#)
endif
if collcheck#<30 and collcheck#>0 then x#=collx#:z#=collz#:s#=0
endif
return
____MAKE_OBJECTS____:
rem -------------Characters-Objects------------
makeplayer:
rem make player as a cone
make object cone 10,30
rem stretch cone height x 2 (200 % scale) from original shape
scale object 10,100,200,100
rem tilt cone so that tip points forward
xrotate object 10,90
rem fix object pivot in order to be always tilted
fix object pivot 10
return
makeobjects:
rem randomize (somevalue) will ensure object will be always
rem at the same position when starting the program
randomize 1
cubesize=100
rem build 20 cubes as object numbers 101 to 120
for n=101 to 120
make object cube n,cubesize
set object n,1,1,1,1,1,1
rem color randomly
color object n,rgb(rnd(150),rnd(150),rnd(150))
rem position randomly within matrix area
xpos=rnd(landsize):zpos=rnd(landsize)
position object n,xpos,cubesize/2,zpos
next n
make object cube 121,300
position object 121,2100,150,2300
return
rem ------------------ambient----------------
_______AMBIENT_______:
rem some ambient settings
makeambient:
set ambient light 70
set directional light 0,0.1,0.2,0.25
position light 0,0.0,2000.0,0.0
point light 0,4000,0,4000
fog on
fog color rgb(150,150,200)
fog distance 10000
return
end
more precise ( does check players sides)
_______TOP_______:
rem Generic Template
rem Player = object 10
rem Cubes = objects 100-120
_____INITIALIZE_____:
set display mode 1024,768,32
SYNC ON
SYNC RATE 60
backdrop on
color backdrop rgb(10,100,100)
autocam off
set global collision off
global objnum
x#=1000.0:z#=1000.0:a#=0.0:s#=0.0:h#=0.0
cx#=0.0:cy#=0.0:cz#=0.0:ca#=0.0:camght=50
camrot=0:camdist=0:camlen=0:pdist#=0:collcheck#=0
collx#=0:collz#=0:objnum=100
gosub Setupland1
gosub makeplayer2
gosub makeobjects
rem gosub makespheres
gosub ambient
gosub resetcam
_____DO_LOOP_____:
do
if x#<100 then x#=100
if z#<100 then z#=100
if x#>landsize-100 then x#=landsize-100
if z#>landsize-100 then z#=landsize-100
collx#=x#:collz#=z#
if inkey$()="e" then sync rate 0
if inkey$()="E" then sync rate 60
gosub printdata
gosub camera_control
gosub update_camera
gosub Playercontrol
rem position object 1000,to1x#,h#+5,to1z#
rem position object 1001,to2x#,h#+5,to2z#
gosub checkdistance
gosub checkcoll
SYNC
loop
rem -------------ROUTINES--------------------
____SET_SCENERY___:
Setupland1:
rem ground textures
rem create texture
cls rgb(0,100,20)
inkcolor#=rgb(255,255,255)
line 0,0,0,250
line 1,1,1,250
line 2,2,2,250
line 0,0,250,0
line 1,1,250,1
line 2,2,250,2
line 0,0,250,250
line 0,250,250,0
rem next n
get image 2,0,0,250,250
rem Make landscape
landsize=4000:grid=30:mtxrandomize=1
make matrix 1,landsize,landsize,grid,grid
set matrix 1,1,0,0,1,1,1,1
prepare matrix texture 1,2,1,1
randomize matrix 1,mtxrandomize
rem ***********
rem get ground done
update matrix 1
return
_____CONTROL_PLAYER___:
Playercontrol:
rem ---- Control Character ----
x#=newxvalue(x#,a#,s#)
z#=newzvalue(z#,a#,s#)
h#=get ground height(1,x#,z#)
position object 10,x#,h#+10,z#
yrotate object 10,a#
if upkey()=1 and s#<8 then s#=s#+0.1
if downkey()=1 and s#>-8 then s#=s#-0.2
if leftkey()=1 then a#=wrapvalue(a#-1)
if rightkey()=1 then a#=wrapvalue(a#+1)
if inkey$()="ù" then s#=0.0
if inkey$()="à" then s#=10.0
return
______CAMERA_ROUTINES____:
rem -------CAMERA--------
camera_control:
if mouseclick()=1
mymousex=mousemovex()
if mymousex>0 then camrot=camrot+2
if mymousex<0 then camrot=camrot-2
mymousey=mousemovey()
if mymousey>0 then campointh=campointh+3
if mymousey<0 then campointh=campointh-3
mymousez=mousemovez()
if mymousez>0 then camdist=camdist+10
if mymousez<0 then camdist=camdist-10
endif
rem camera update
update_camera:
position mouse 300,300
rem Position camera to the back of the character
ca#=wrapvalue(a#+camrot)
cx#=newxvalue(x#,ca#,camdist)
cz#=newzvalue(z#,ca#,camdist)
cy#=h#
position camera cx#,cy#+camhgt,cz#
rem Point camera at object
point camera x#,h#+camhgt+campointh,z#
return
rem camera reset
Resetcam:
set camera range 1,20000
camdist=-150
camhgt=60
camrot=0
campointh=0
return
_____PRINT_ON__SCREEN___:
printdata:
set cursor 0,0
print "Polys=",statistic(1)
print "FPS=",screen fps()
print "player x#= ",x#
print "player h#= ",h#
print "player z#= ",z#
print "player angle a#= ",a#
print "speed s#= ",s#
print "hit object objnum=",objnum
return
printdata2:
set cursor 0,0
print statistic(1)
print screen fps()
return
_____CHECK_ROUTINES_____:
checkdistance:
for n=100 to 120
xdif#=object position x(n)-x#
zdif#=object position z(n)-z#
pdist#=sqrt(xdif#*xdif#+zdif#*zdif#)
if pdist#<100 then objnum=n:return
next n
objnum=0
collcheck1#=0
collcheck2#=0
return
checkcoll:
if inkey$()="r" then return
if s#>0
a1#=wrapvalue(a#+40)
a2#=wrapvalue(a#-40)
to1x#=newxvalue(x#,a1#,80)
to1z#=newzvalue(z#,a1#,80)
to2x#=newxvalue(x#,a2#,80)
to2z#=newzvalue(z#,a2#,80)
if objnum>0
collcheck1#=intersect object(objnum,x#,50,z#,to1x#,50,to1z#)
collcheck2#=intersect object(objnum,x#,50,z#,to2x#,50,to2z#)
endif
if collcheck1#<16 and collcheck1#>0 then x#=collx#:z#=collz#:s#=0
if collcheck2#<16 and collcheck2#>0 then x#=collx#:z#=collz#:s#=0
endif
if s#<0
a1#=wrapvalue(a#+130)
a2#=wrapvalue(a#-130)
to1x#=newxvalue(x#,a1#,80)
to1z#=newzvalue(z#,a1#,80)
to2x#=newxvalue(x#,a2#,80)
to2z#=newzvalue(z#,a2#,80)
if objnum>0
collcheck1#=intersect object(objnum,x#,50,z#,to1x#,50,to1z#)
collcheck2#=intersect object(objnum,x#,50,z#,to2x#,50,to2z#)
endif
if collcheck1#<16 and collcheck1#>0 then x#=collx#:z#=collz#:s#=0
if collcheck2#<16 and collcheck2#>0 then x#=collx#:z#=collz#:s#=0
endif
return
____MAKE_OBJECTS____:
rem -------------Characters-Objects------------
makeplayer:
make object cone 10,30
scale object 10,100,200,100
xrotate object 10,90
fix object pivot 10
return
makeplayer2:
make object cube 10,20
return
makeobjects:
randomize 1
cubesize=100
for n=100 to 120
make object cube n,cubesize
set object n,1,1,1,1,1,1
color object n,rgb(rnd(150),rnd(150),rnd(150))
xpos=rnd(landsize):zpos=rnd(landsize)
position object n,xpos,cubesize/2,zpos
next n
return
makespheres:
make object sphere 1000,10
make object sphere 1001,10
return
rem ------------------ambient----------------
_______AMBIENT_______:
ambient:
set ambient light 70
set directional light 0,0.1,0.2,0.25
position light 0,0.0,2000.0,0.0
point light 0,4000,0,4000
fog on
fog color rgb(150,150,200)
fog distance 10000
return
end
P.S. Soon the board will cringe at these typical Scorpyo's green painted matrixes