Betterificated.
I'm still working on slipping, though it's kinda hard doing it properly...
`Car Physics
`Setup
Sync on : Sync rate 60
Set display mode 1024,768,16 : hide mouse
Autocam off : set camera range 1,0x7fffffff
`Game data
grav#=9.8 :`Gravity
mass#=50.0 :`Car Mass
traction#=0.95 :`Car Traction
drag#=0.4257 :`Drag (Air Resistance)
rr#=0.50 :`Rolling Resistance
eng#=1000.0 :`Engine Force
wheeldist#=60.0 :`Distance apart of wheels
turn#=15.0 :`Turning Angle
turnsmth#=5.0 :`Turning Smoothness
`Player data
type coord
x as float
y as float
z as float
endtype
type angle
x as float
y as float
z as float
tr as float
wa as float
av as float
turn as float
turnsmth as float
beta as float
afrnt as float
arear as float
endtype
type force
long as coord
lat as coord
drag as coord
rr as coord
trac as coord
traction as float
eng as float
drg as float
rlr as float
endtype
type move
acc as coord
vel as coord
spd as float
endtype
type player
pos as coord
ang as angle
frc as force
u as coord
mve as move
mass as float
weight as float
wd as float
endtype
p as player
`Configure
p.mass=mass#
p.weight=p.mass*grav#
p.wd=wheeldist#
p.frc.eng=eng#
p.frc.traction=traction#
p.ang.turn=turn#
p.ang.turnsmth=turnsmth#
p.frc.drg=drag#
p.frc.rlr=rr#
`Create player
create_car(1)
color_car(1,rgb(255,0,0))
`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,50000,50000,50,50
prepare matrix texture 1,10,1,1
`**Main Loop**
Do
`Control
Gosub _Controls
`Physics
Gosub _Physics
`Update Player
Gosub _Update_Player
`Camera
Gosub _Camera
`**End Loop**
Sync
Loop
`**Subroutines**
`Controls
_Controls:
if upkey()=1 then up=1 else up=0
if downkey()=1 then down=1 else down=0
if leftkey()=1 then left=1 else left=0
if rightkey()=1 then right=1 else right=0
if up=1
p.frc.long.x=p.frc.trac.x+p.frc.drag.x+p.frc.rr.x
p.frc.long.z=p.frc.trac.z+p.frc.drag.z+p.frc.rr.z
endif
if down=1
endif
if left=1
p.ang.wa=curveangle(-p.ang.turn,p.ang.wa,p.ang.turnsmth)
endif
if right=1
p.ang.wa=curveangle(p.ang.turn,p.ang.wa,p.ang.turnsmth)
endif
if up=0 and down=0
p.frc.long.x=0
p.frc.long.z=0
endif
if left=0 and right=0
p.ang.wa=curveangle(0,p.ang.wa,p.ang.turnsmth)
endif
Return
`Physics
_Physics:
`Equations
p.u.x = sin(p.ang.y) : p.u.z = cos(p.ang.y)
p.frc.rr.x=-p.frc.rlr*p.mve.vel.x
p.frc.rr.z=-p.frc.rlr*p.mve.vel.z
p.frc.trac.x = p.u.x * p.frc.eng
p.frc.trac.z = p.u.z * p.frc.eng
p.mve.acc.x = p.frc.long.x / p.mass
p.mve.acc.z = p.frc.long.z / p.mass
p.mve.vel.x=(p.mve.vel.x+p.mve.acc.x)*p.frc.traction
p.mve.vel.z=(p.mve.vel.z+p.mve.acc.z)*p.frc.traction
p.mve.spd = sqrt(p.mve.vel.x^2 + p.mve.vel.y^2 + p.mve.vel.z^2)
p.frc.drag.x=-p.frc.drg*p.mve.vel.x*p.mve.spd
p.frc.drag.z=-p.frc.drg*p.mve.vel.z*p.mve.spd
p.ang.tr=p.wd / sin(p.ang.wa)
p.ang.av=p.mve.spd/p.ang.tr
p.ang.y=wrapvalue(p.ang.y+(p.ang.av*(p.ang.turn*2)))
p.ang.beta=atan(p.mve.vel.z/p.mve.vel.x)
`p.ang.afrnt=s
`Calculate new coords
p.pos.x=p.pos.x+sin(p.ang.y)*p.mve.spd
p.pos.z=p.pos.z+cos(p.ang.y)*p.mve.spd
p.pos.y=get ground height(1,p.pos.x,p.pos.y)+20
Return
`Update Player
_Update_Player:
position object 1,p.pos.x,p.pos.y,p.pos.z
yrotate object 1,p.ang.y
rotate limb 1,3,0,p.ang.wa,0
rotate limb 1,4,0,p.ang.wa,0
Return
`Camera
_Camera:
ca#=curveangle(p.ang.y,ca#,20.0)
cx#=p.pos.x-sin(ca#)*600
cz#=p.pos.z-cos(ca#)*600
cy#=p.pos.y+300.0
position camera cx#,cy#,cz#
point camera p.pos.x,p.pos.y,p.pos.z
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,1,col : color limb id,2,col
endfunction
[edit]
If you want to help, check out this thread, and try to explain these symbols for me please