Hi Guys,
Here is a simple program to demonstrate how to code a simple player movement and collision system, a third person moving object and a static world with the built-in commands (for those with a phobia of physics):
Rem Project: PlayerCarWorldCollision
Rem Created: 23/09/2006 11:44:37
rem Lead Programmer of TGC gives up his weekend
rem to write a program to demonstates the DBP
rem 'very simple' player collision system
rem Init
set display mode 800,600,32,1 : sync on : sync rate 0 : hide mouse
rem Make a player
p=1 : make object box p,10,30,10 : color object p,rgb(255,0,0)
rem Make a car
c=2 : make object box c,30,30,60 : color object c,rgb(0,0,255)
rem Make a simple world
w=3 : make object cube w,100 : make mesh from object w,w : delete object w
make object box w,1000,10,1000 : add limb w,1,w : offset limb w,1,50,50,200
rotate limb w,1,0,35,0 : color object w,rgb(0,64,0)
rem Setup a simple scene
position object p,0,15,-50
position object c,-10,21,50
rem Set automated sliding collision for player objects
automatic object collision p,5,0
automatic object collision c,0,0
rem Set lighting
color backdrop rgb(192,192,255)
set point light 0,50,200,100
color light 0,1024,1024,1024
set light range 0,2500
set ambient light 0
rem Set up shadows
set shadow shading on p
set shadow shading on c
rem Main loop
do
`
rem Remember old position of player
oldpositionx#=object position x(p)
oldpositiony#=object position y(p)
oldpositionz#=object position z(p)
`
rem Player is controlled by arrowkeys
if upkey()=1 then move object p,3
if downkey()=1 then move object p,-3
if leftkey()=1 then yrotate object p,object angle y(p)-4
if rightkey()=1 then yrotate object p,object angle y(p)+4
`
rem Automatically drive the car around
speed#=speed#+0.1 : if speed#>0.5 then speed#=0.5
move object c,speed# : yrotate object c,object angle y(c)-0.5
if object collision(c,0)<>0 then speed#=-0.5
`
rem Ray cast to make sure player does not enter world scenery
startx#=object position x(p)
starty#=object position y(p)
startz#=object position z(p)
endx#=newxvalue(startx#,object angle y(p),object size x(p))
endz#=newzvalue(startz#,object angle y(p),object size x(p))
if intersect object(w,startx#,starty#,startz#,endx#,starty#,endz#)<>0
rem if raycast hits world geometry, go back to old position
position object p,oldpositionx#,oldpositiony#,oldpositionz#
endif
`
rem Camera follows player
f=p : set camera to follow object position x(f),object position y(f),object position z(f),object angle y(f),60,35,5.0,0
`
rem Prompts
ink rgb(32,32,32),0
center text screen width()/2,30,"DEMONSTRATES AUTOMATED COLLISION + INTERSECT WORLD COLLISION"
ink rgb(255,255,0),0
if object in screen(p)=1 then center text object screen x(p),object screen y(p),"PLAYER"
if object in screen(c)=1 then center text object screen x(c),object screen y(c),"CAR"
`
rem Update screen
sync
`
rem End loop
loop
As payment for this code, please post your feedback on the following question. I am accused once in a while of being lazy and unhelpful, mostly by Support Mail users. Our general support mail policy discourages the answering the day to day coding problems encountered by users learning to program. I think this is a sensible policy, as we do not have the resources to enter into a teacher/student role, and depend on the vibrant forum community to fill this gap. My question is whether you, the actual community, agree with this policy? Should Lee spend his time in Support Mail teaching people how to code, debugging programs and writing samples to demonstate the use of commands? Or should Lee spend his time fixing DBP/GDK/FPSC bugs and creating new products? The source code above is in response to a support mail user and I would like to get a broader range of opinions as to whether my decision to focus on the latter is the right one.
"Small, smart, and running around the legs of dinosaurs to find enough food to survive, bedroom programmers aren't extinct after all
"