set display mode 800, 600, 32
sync on
sync rate 30
randomize timer()
backdrop on
color backdrop 0
set text size 20
ink rgb(0,0,255),0
 
type where
x# as float
y# as float
z# as float
endtype
 
s as where
 
make object box 1,1,1,5
make mesh from object 1,1
delete object 1
 
make object cube 1,3
add limb 1,1,1
offset limb 1,1,0,3,3
 
make object box 2,1,5,1
set object collision off 2
 
s.x#=0.0
s.x#=0.0
s.x#=0.0
 
position=1
velocity=2
accel=3
 
null=make vector3(position)
null=make vector3(velocity)
null=make vector3(accel)
 
 
start_race_here:
 
x=1
xx=0
wid=45
bend=0
sharp=0
for t=100 to 250
 dec bend
 if bend<0 and t>104 then bend=5+rnd(15) : sharp=rnd(11)+5 : x=-x
 if rnd(2)=1 then sharp=sharp+abs(x)*2
 xx=xx+(x*sharp)
if object exist(t)=0 then make object cylinder t,10
 position object t,xx-wid,0,(t-101)*30
 scale object t,100,100,100
 set object collision on t
if object exist(t+250)=0 then  make object cylinder t+250,10
 scale object t+250,100,100,100
 set object collision on t+250
 position object t+250,xx+wid,0,(t-101)*30
next t
 
set vector3 position,0.0,0.0,0.0
set vector3 velocity,0.0,0.0,0.0
set vector3 accel,0.0,0.0,0.0
 
position object 1,0,0,0
rotate object 1,0,0,0
 
hits=0
counting=0
 
 
do
 
 
yrotate object 1,object angle y(1)+(rightkey()-leftkey())*4
 
if counting=0 then t1#=timer()
center text 400,540,"time: "+str$((timer()-t1#)/1000)
 
if upkey() or downkey()
 counting=1
 
 x#=object position x(1)
 y#=object position y(1)
 z#=object position z(1)
 
 move object 1,((upkey()-downkey())*1.0)/3.0
 
 nx#=object position x(1)
 ny#=object position y(1)
 nz#=object position z(1)
 
set vector3 accel,nx#-x#,ny#-y#,nz#-z#
 
add vector3 velocity,velocity,accel
 
endif : ` applied thrust this turn?
 
add vector3 position,position,velocity
 
position object 1,x vector3(position),y vector3(position),z vector3(position)
position object 2,x vector3(position),y vector3(position)+5,z vector3(position)
point object 2,x vector3(position)+x vector3(velocity),y vector3(position)+y vector3(velocity),z vector3(position)+z vector3(velocity)
 
set camera to follow object position x(1),object position y(1),object position z(1),object angle y(1),30,15,10,0
 
text 10,10,"speed: "+str$(length vector3(velocity))
 
 x#=object position x(1)
 y#=object position y(1)
 z#=object position z(1)
 
z=int(z#/30)+101
text 30,30,"flagset: "+str$(z)
IF object exist(z)
 dist#=discent(z)
 text 50,50,"dist: "+str$(dist#)
 if dist#>100.0
  center text 400,160,"OUT  OF  BOUNDS"
  center text 400,400,"press any key"
  sync
  wait 500
  wait key
  goto start_race_here
 endif
ELSE
 sync
 t2#=(timer()-t1#)/1000
 center text 400,20,"Time Trial Complete!"
 center text 400,40,"Your Time: "+str$(t2#)
 center text 400,60,"Number of Collisions: "+str$(hits)
 t3#=t2#+5.0*hits
 center text 400,80,"Adjusted Time: "+str$(t3#)
 center text 400,500,"press any key to race again."
 sync
 wait 500
 wait key
 goto start_race_here
ENDIF
 
 
 ` check for collision with flags.
 
 a=object collision(1,0)
 if a>100
  set object collision off a
  inc hits
  scale object a,25,25,25
 endif
 
text 70,70,"hits: "+str$(hits)
 
 
sync
loop
 
function discent(b)
 ` this is the distance from your object to the center of the flagset you're at.
t#=object position x(1)
u#=object position y(1)
v#=object position z(1)
x#=object position x(b)+45.0
y#=object position y(b)+45.0
z#=object position z(b)+45.0
dist#=sqrt( (t#-x#)^2 + (u#-y#)^2 + (v#-z#)^2 )
endfunction dist#