Ok, here's some better code now.
I made it so it depends on a different traction if it's in the air, otherwise it slows down as if it was still on the ground. Both tractions can be altered.
I also made the matrix slightly prettier, I abs() it, and made it cragier towards the other side of it, so exlore around it.
I discovered that it only tilts correctly 3 out of 4 times, and I dont know why, I'll have to work this out.
`Racing Game Challenge
`Standad Setup
Sync on : Sync rate 60
Set display mode 1024,768,16 : Hide mouse
Autocam off : set camera range 1,99999999999
`Constants
#constant StartLoop for DL=1 to 0x7fffffff
#constant EndLoop next DL
`Game Variables
grav#=10.0
backcl=rgb(240,102,0)
fogdist=6000
amb=30
`Player Variables
id=1
maxspeed#=70.0
scspeed#=20.0
trac#=0.90 : itrac#=trac#
atrac#=0.98
tracd#=0.0006
traci#=0.01
mtrac#=0.9999
acc#=2.0
turnd#=15.0
mturn#=3.0
fxang#=20.0
`Create Player
Create_Car(id)
Color_Car(id,rgb(59,59,255))
make object plain id+1,50,100 : xrotate object id+1,90 : fix object pivot id+1
color object id+1,rgb(0,0,0) : set object ambient id+1,0
set object rotation zyx id+1
`Create world
create bitmap 1,128,128 : set current bitmap 1
cls rgb(0,128,0)
for d=1 To 10000
ink rgb(0,rnd(255),rnd(50)),0
dot rnd(128),rnd(128)
next d
blur bitmap 1,4
get image 10,0,0,128,128
set current bitmap 0 : delete bitmap 1
make matrix 1,10000,10000,50,50
for x=0 to 49
for z=1 to 49
gh#=50-(abs(sin((x*20))*(200*(x/10))))
gh#=gh#+(50.0-(abs(sin((z*20))*(200*(x/10)))))
gh#=gh#/2.0
set matrix height 1,x,z,gh#
next x
next z
for z=1 to 49
for x=1 to 49
h8#=get matrix height(1,x,z-1)
h4#=get matrix height(1,x-1,z)
h#=get matrix height(1,x,z)
h2#=get matrix height(1,x,z)
x1#=(x-1)*25.0 : y1#=h#
x2#=(x+0)*25.0 : y2#=h4#
dx#=x2#-x1#
dy#=y2#-y1#
ax#=atanfull(dx#,dy#)
ax#=wrapvalue(90-ax#)
z1#=(z-1)*25.0 : y1#=h2#
z2#=(z+0)*25.0 : y2#=h8#
dz#=z2#-z1#
dy#=y2#-y1#
az#=atanfull(dz#,dy#)
az#=wrapvalue(90-az#)
nx#=sin(wrapvalue(ax#+45))
ny#=cos(ax#)
nz#=sin(wrapvalue(az#+45))
set matrix normal 1,x,z,nx#,ny#,nz#
next x
next z
prepare matrix texture 1,10,1,1
update matrix 1
fog on
backdrop on
fog distance fogdist
fog color backcl
color backdrop backcl
set ambient light amb
color ambient light backcl
`*Main Loop*
StartLoop
`Controls Elements
Gosub _Control_Player
Gosub _Control_Camera
Gosub _Interfaces
`*End Loop*
If escapekey()=1 then end
Sync
EndLoop
`*Subroutines*
`Control Player
_Control_Player:
`Controls
oldx#=x#
oldy#=y#
oldz#=z#
if y#<truey#+1.0
if upkey()=1 then inc speed# : xspeed#=sin(ay#)*speed# : zspeed#=cos(ay#)*speed# : trac#=trac#+traci#
if upkey()=0 then trac#=trac#-tracd# : dec speed#
turn#=movespeed#/turnd#
if leftkey()=1 then ay#=wrapvalue(ay#-turn#)
if rightkey()=1 then ay#=wrapvalue(ay#+turn#)
endif
`Movement + Physics
if trac#>=mtrac# then trac#=mtrac#
if trac#<=itrac# then trac#=itrac#
if speed#>=maxspeed# then speed#=maxspeed#
if speed#<=0.0 then speed#=0.0
if y#<truey#+1.0
xspeed#=xspeed#*trac#
zspeed#=zspeed#*trac#
else
xspeed#=xspeed#*atrac#
zspeed#=zspeed#*atrac#
endif
x#=x#+xspeed#
z#=z#+zspeed#
moveang#=atanfull(x#-oldx#,z#-oldz#)
movespeed#=sqrt((x#-oldx#)^2+(z#-oldz#)^2)
if upkey()=0 and downkey()=0
if leftkey()=0 and rightkey()=0
if movespeed#>=scspeed#
if y#<truey#+1.0
ay#=curveangle(moveang#,ay#,fxang#)
endif
endif
endif
endif
`Ground Tilting and jumping off
truey#=get ground height(1,x#,z#)+30
if y#<=truey# then y#=truey#
y#=y#+yspeed#
if y#=truey#
yspeed#=y#-oldy#
else
yspeed#=-grav#
endif
out#=1
frontx#=newxvalue(x#,ay#,out#)
frontz#=newzvalue(z#,ay#,out#)
backx#=newxvalue(x#,ay#,out#*-1)
backz#=newzvalue(z#,ay#,out#*-1)
leftx#=newxvalue(x#,wrapvalue(ay#-90),out#)
leftz#=newzvalue(z#,wrapvalue(ay#-90),out#)
rightx#=newxvalue(x#,wrapvalue(ay#+90),out#)
rightz#=newzvalue(z#,wrapvalue(ay#+90),out#)
front#=get ground height(1,frontx#,frontz#)
back#=get ground height(1,backx#,backz#)
left#=get ground height(1,leftx#,leftz#)
right#=get ground height(1,rightx#,rightz#)
ax#=wrapvalue(curveangle((back#-front#)*20,object angle x(id),5.0))
az#=wrapvalue(curveangle((right#-left#)*20,object angle z(id),5.0))
`Update Player
position object id,x#,y#,z#
if y#<truey#+1.0 then rotate object id,ax#,ay#,az#
`Update shadow
position object id+1,x#,truey#-29,z#
rotate object id+1,ax#,ay#,az#
Return
`Control Camera
_Control_Camera:
`Camera
ca#=curveangle(ay#,ca#,20.0)
cx#=x#-sin(ca#)*400.0
cz#=z#-cos(ca#)*400.0
cy#=curvevalue(y#+200.0,cy#,10.0)
position camera cx#,cy#,cz#
point camera x#,y#,z#
Return
`Interfaces
_Interfaces:
speed$="Speed: "
Border_text(10,10,speed$,rgb(255,255,255),rgb(255,0,255))
Border_text(10+text width(speed$),10,str$(int(Movespeed#))+" mph",rgb(255,255,255),rgb(255,0,0))
Return
`*Functions*
Function Create_Car(id)
create bitmap 1,100,100 : set current bitmap 1
cls rgb(0,0,0) : ink rgb(128,128,128),0
for x=1 to 100 : for y=1 to 100
if sqrt((x-50)^2+(y-50)^2)<50 then box x,y,x+1,y+1
next x : next y
get image id,0,0,100,100 : set current bitmap 0 : delete bitmap 1
make object box id,50,20,100 : make mesh from object id,id
add limb id,id,id : delete mesh id
make object box id+1,50,15,50 : xrotate object id+1,343.3 : glue object to limb id+1,id,id
make object plain id+2,20,20 : make object plain id+3,20,20
make object plain id+4,20,20 : make object plain id+5,20,20
for w=2 to 5 : yrotate object id+w,90 : glue object to limb id+w,id,id : next w
color object id,col : color object id+1,col
for c=1 to 5 : make mesh from object id+c,id+c : add limb id,id+c,id+c
delete mesh id+c : delete object id+c : next c
offset limb id,id+1,0,10.0,-23.9
offset limb id,id+2,-26,-10.0,30 : offset limb id,id+3,26,-10.0,30
offset limb id,id+4,-26,-10.0,-30 : offset limb id,id+5,26,-10.0,-30
rotate limb id,id+3,0,180,0 : rotate limb id,id+5,0,180,0
for w=2 to 5 : texture limb id,id+w,id : next w
set object cull id,0 : set object transparency id,1 : set object rotation zyx id
Endfunction
Function Color_Car(id,col)
color limb id,id,col : color limb id,id+1,col
endfunction
Function Border_Text(x,y,text$,col1,col2)
ink col2,0
text x-1,y-1,text$
text x+1,y-1,text$
text x-1,y+1,text$
text x+1,y+1,text$
ink col1,0
text x,y,text$
Endfunction