TGC Codebase Backup



basic camera and object collision/moving platforms by ruben

15th Nov 2006 11:01
Summary

this is a test room I used for the platform game I'm creating. It's basicly a really small platform game. I think it can be usefull for starting programmers with collision problems



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `this program uses sliding box collision
`some parts of this are also in the darcbasic examples but there 
`the camera collision only works with static objects, here it works with
`normal objects too, the only need a collision box.
`have fun tweaking this code.... =D

`the start...
autocam off
sync on
sync rate 60

`make a player object
gosub _speler

`make a simple level
gosub _level1


rem Main loop
do

`movement
if upkey()=1 then move object 1,0.8
if leftkey()=1 then yrotate object 1,wrapvalue(object angle y(1)-2.5)
if rightkey()=1 then yrotate object 1,wrapvalue(object angle y(1)+2.5)
if spacekey()=1 and playergrav#=0.0 then playergrav#=2.0

rem Use camera tracker to follow player object
`change these parameters for other camera positions
angle#=object angle y(1)
camdist#=25.0 : camhigh#=posy#+8.0 : camfade#=3.5
set camera to follow posx#,posy#,posz#,angle#,camdist#,camhigh#,camfade#,1

`player position
posx#=object position x(1)
posy#=object position y(1)
posz#=object position z(1)

`camera position
cposx#=camera position x()
cposy#=camera position y()
cposz#=camera position z()
position object 2,cposx#,cposy#,cposz#

rem Handle a touch of gravity
playergrav#=playergrav#-0.1
posy#=posy#+playergrav#

rem Handle sliding collision for player object with other objects
position object 1,posx#,posy#,posz#
if object collision(1,0)>0
	dec posx#,get object collision x()
	dec posy#,get object collision y()
	dec posz#,get object collision z()
	if get object collision y()<0 then playergrav#=0.0
endif

`move one of the platforms and update the players position if he hits it
`for smooth movement i use a sinus
a=wrapvalue(a+1)
position object 508,object position x(508)+sin(a),-5,200
if object collision(1,508)=1 then posx#=posx#+sin(a)
position object 509,object position x(509)-sin(a),-5,200
if object collision(1,509)=1 then posx#=posx#-sin(a)

rem camera collision
if object collision(2,0)>0
	dec cposx#,get object collision x()
	dec cposy#,get object collision y()
	dec cposz#,get object collision z()
endif
position object 2,cposx#,cposy#,cposz#
position camera cposx#,cposy#,cposz#

rem Update with new object position
position object 1,posx#,posy#,posz#

rem Tilt camera down a little
xrotate camera 15 

rem Update screen
sync

rem End loop
loop


`maak de speler
_speler:
make object box 1,10,10,10
color object 1,rgb(0,0,255)
position object 1,0,0,0
make object collision box 1,-5,-5,-5,5,5,5,0

`en het object voor de camera
make object cube 2,1
make object collision box 2,-0.5,-0.5,-0.5,0.5,0.5,0.5,0
hide object 2

return


`maak level 1
_level1:

`the floor
make object box 100,1000,10,1000
position object 100,0,-20,0
color object 100,rgb(10,200,10)
make object collision box 100,-500,-5,-500,500,5,500,0

`some boxes and platforms
make object box 500,10,5,20
position object 500,100,-12.5,350
color object 500,rgb(200,0,0)
make object collision box 500,-5,-2.5,-10,5,2.5,10,0

make object box 501,50,20,20
position object 501,0,-5,350
color object 501,rgb(200,0,0)
make object collision box 501,-25,-10,-10,25,10,10,0

for t=502 to 507
  make object box t,20,5,20
  position object t,-100+(t-502)*35,-5+(t-502)*3,100
  color object t,rgb(rnd(255),rnd(255),rnd(255))
  make object collision box t,-10,-2.5,-10,10,2.5,10,0
next t

`these will be the moving platforms
make object box 508,20,5,20
position object 508,-130,-5,200
color object 508,rgb(rnd(255),rnd(255),rnd(255))
make object collision box 508,-10,-2.5,-10,10,2.5,10,0
make object box 509,20,5,20
position object 509,130,-5,200
color object 509,rgb(rnd(255),rnd(255),rnd(255))
make object collision box 509,-10,-2.5,-10,10,2.5,10,0

`and walls around the scene
for t=0 to 1
  make object box t+512,10,40,500
  position object t+512,-200+t*400,5,220
  color object t+512,rgb(rnd(255),rnd(255),rnd(255))
  make object collision box t+512,-5,-20,-250,5,20,250,0
  make object box t+514,400,40,10
  position object t+514,0,5,-30+t*500
  color object t+514,rgb(rnd(255),rnd(255),rnd(255))
  make object collision box t+514,-200,-20,-5,200,20,5,0
next t

return