Here ya go Bro. It's based on CAR movement - not MPH measure from a "Transmission" or Wheel.
Rem Project: car
Rem Created: 12/21/2007 9:57:26 PM
Rem ***** Main Source File *****
global steeringAngle as float
global steeringDelta as float
global force as float
//------------------------------------------------------------------
// Jason - Begin - Some Stuff I Like in DBPro
//------------------------------------------------------------------
#CONSTANT True=1
#CONSTANT False=0
Type rtXYZ
X#
Y#
Z#
ENDTYPE
//------------------------------------------------------------------
//------------------------------------------------------------------
// --- Scancodes for key presses
//------------------------------------------------------------------
#CONSTANT cnScanCode_A=30
#CONSTANT cnScanCode_B=48
#CONSTANT cnScanCode_C=46
#CONSTANT cnScanCode_D=32
#CONSTANT cnScanCode_E=18
#CONSTANT cnScanCode_F=33
#CONSTANT cnScanCode_G=34
#CONSTANT cnScanCode_H=35
#CONSTANT cnScanCode_I=23
#CONSTANT cnScanCode_J=36
#CONSTANT cnScanCode_K=37
#CONSTANT cnScanCode_L=38
#CONSTANT cnScanCode_M=50
#CONSTANT cnScanCode_N=49
#CONSTANT cnScanCode_O=24
#CONSTANT cnScanCode_P=25
#CONSTANT cnScanCode_Q=16
#CONSTANT cnScanCode_R=19
#CONSTANT cnScanCode_S=31
#CONSTANT cnScanCode_T=20
#CONSTANT cnScanCode_U=22
#CONSTANT cnScanCode_V=47
#CONSTANT cnScanCode_W=17
#CONSTANT cnScanCode_X=45
#CONSTANT cnScanCode_Y=21
#CONSTANT cnScanCode_Z=44
#CONSTANT cnScanCode_F1=59
#CONSTANT cnScanCode_F2=60
#CONSTANT cnScanCode_F3=61
#CONSTANT cnScanCode_F4=62
#CONSTANT cnScanCode_F5=63
#CONSTANT cnScanCode_F6=64
#CONSTANT cnScanCode_F7=65
#CONSTANT cnScanCode_F8=66
#CONSTANT cnScanCode_F9=67
#CONSTANT cnScanCode_F10=68
#CONSTANT cnScanCode_LeftSingleQuote=41
#CONSTANT cnScanCode_1=2
#CONSTANT cnScanCode_2=3
#CONSTANT cnScanCode_3=4
#CONSTANT cnScanCode_4=5
#CONSTANT cnScanCode_5=6
#CONSTANT cnScanCode_6=7
#CONSTANT cnScanCode_7=8
#CONSTANT cnScanCode_8=9
#CONSTANT cnScanCode_9=10
#CONSTANT cnScanCode_0=11
#CONSTANT cnScanCode_Minus=12
#CONSTANT cnScanCode_Equal=13
#CONSTANT cnScanCode_BackSpace=14
#CONSTANT cnScanCode_Tab=15
#CONSTANT cnScanCode_LeftBracket=26
#CONSTANT cnScanCode_RightBracket=27
#CONSTANT cnScanCode_BackSlash=43
#CONSTANT cnScanCode_Enter=28
#CONSTANT cnScanCode_Caps=58
#CONSTANT cnScanCode_LShift=42
#CONSTANT cnScanCode_RShift=54
#CONSTANT cnScanCode_SemiColon=39
#CONSTANT cnScanCode_SingleQuote=40
#CONSTANT cnScanCode_Comma=51
#CONSTANT cnScanCode_Period=52
#CONSTANT cnScanCode_ForwardSlash=53
#CONSTANT cncanCode_LWinKey=219
#CONSTANT cnScanCode_LAlt=56
#CONSTANT cnScanCode_RAlt=184
#CONSTANT cnScanCode_RWinKey=220
#CONSTANT cnScanCode_WinMenu=221
#CONSTANT cnScanCode_LCntl=29
#CONSTANT cnScanCode_RCntl=157
#CONSTANT cnScanCode_PrintScreen=183
#CONSTANT cnScanCode_ScollLock=70
#CONSTANT cnScanCode_Pause=197
`#CONSTANT cnScanCode_Break=197 Same Key
#CONSTANT cnScanCode_Ins=210
#CONSTANT cnScanCode_Del=211
#CONSTANT cnScanCode_Home=199
#CONSTANT cnScanCode_End=207
#CONSTANT cnScanCode_PgUp=201
#CONSTANT cnScanCode_PgDn=209
#CONSTANT cnScanCode_UpArrow=200
#CONSTANT cnScanCode_DownArrow=208
#CONSTANT cnScanCode_LeftArrow=203
#CONSTANT cnScanCode_RightArrow=205
#CONSTANT cnScanCode_NumLock=69
#CONSTANT cnScanCode_NumForwardSlash=181
#CONSTANT cnScanCode_NumStar=55
#CONSTANT cnScanCode_NumMinus=74
#CONSTANT cnScanCode_NumPlus=78
#CONSTANT cnScanCode_NumEnter=156
#CONSTANT cnScanCode_NumPeriod=83
#CONSTANT cnScanCode_Num1=79
#CONSTANT cnScanCode_Num2=80
#CONSTANT cnScanCode_Num3=81
#CONSTANT cnScanCode_Num4=75
#CONSTANT cnScanCode_Num5=76
#CONSTANT cnScanCode_Num6=77
#CONSTANT cnScanCode_Num7=71
#CONSTANT cnScanCode_Num8=72
#CONSTANT cnScanCode_Num9=73
#CONSTANT cnScanCode_Num0=82
#CONSTANT cnScanCode_SpaceBar = 57
// Jason - End - Some Stuff I Like in DBPro
//------------------------------------------------------------------
//------------------------------------------------------------------
// Jason - Begin - Set Up a Vector Based Distance Vector
// Contact Number - e.g. cn is the prefix
//------------------------------------------------------------------
#CONSTANT cnVectorDistance = 1
junk=MAKE VECTOR3(cnVectorDistance)
// UDT or User Defined Record - e.g. r = prefix
rCarPosOld as rtXYZ
rCarPosNew as rtXYZ
#CONSTANT cnSpeedArraySize = 20
// Array - e.g. "a" is the prefix
global MPH#
global MPHScale#
MPHScale# = 2000
global dim aMph#(cnSpeedArraySize)
global DistTraveled#
// Jason - End - Set Up a Vector Based Distance Vector
// Time
global TimeOld#
global TimeElapsed#
//------------------------------------------------------------------
`=================================
set display mode 1024, 768, 32
sync on
sync rate 60
backdrop on
hide mouse
`===============================
phy start
rem Camera
position camera -20, 20, -25 : point camera 0, 10, 0
`======================================
rem Load the Ground
load object "ground.x",1
scale object 1,10,10,10 : phy make rigid body static mesh 1
load image "dirt3.bmp",3
texture object 1,3
set object light 1,3
`========================================
rem Load the Car
load object "beach.x", 2
load image "beachBu2.tga",2
texture object 2,2
position object 2, 15, 4, -25
offset limb 2,0,0,-0.5,0
rotate limb 2,0,0,-90,0
`=========================================
rem Load the Tach
load object "tach.x",5
position object 5, 4.8, -3.4, 7
lock object on 5
rotate object 5,0,-35,180
set object light 5,100
rem Load the Tach Image as a sprite
load image "Tachimage.bmp", 100000
sprite 100, 810, 635 - 90, 100000
scale object 5,70,90,0
`=========================================
rem Create body
g_fSpeed# = 0.05
g_fTurn# = 0.3
width# = 0.75
height# = 0.4
length# = 2.2
wheelX# = 1.1
wheelY# = 0.3
wheelZ# = 1.5
radius# = 0.75
wheelHeight# = 0.2
`vehicle width and length orientation
phy create vehicle 2
phy add vehicle body 2, length#, height#, width#, 0.0, height#, 0.0
phy add vehicle body 2, width#, height#, width#, 0.0, height#*2, 0.0
phy add vehicle wheel 2, 5, wheelZ#, wheelY#, wheelX#, radius#, wheelHeight#, 0, 0
phy add vehicle wheel 2, 7, wheelZ#, wheelY#, -wheelX#, radius#, wheelHeight#, 0, 1
phy add vehicle wheel 2, 3, -wheelZ#, wheelY#, -wheelX#, radius#, wheelHeight#, 1, 0
phy add vehicle wheel 2, 9, -wheelZ#, wheelY#, wheelX#, radius#, wheelHeight#, 1, 1
phy set vehicle max motor 2, 100.0
steeringDelta = 0.1 : phy set vehicle steering delta 2, steeringDelta
phy set vehicle static friction 2, 50
phy set vehicle max steering angle 2, 0.4
phy set vehicle auto 2, 0
phy set vehicle wheel multiplier 2,0
phy set vehicle suspension spring 2,10000
phy set vehicle mass 2, 11000
phy build vehicle 2
phy set rigid body angular damping 2, 1
phy set rigid body mass offset local 2, -.24, -.24, 0
`powersliding
phy set vehicle lateral tire force function 2, 0, 0.5, 2.0, 4.0, 1.0, 200.0 - ((mom) * .002)
phy set vehicle lateral tire force function 2, 1, 0.5, 2.0, 4.0, 1.0, 200.0 - ((mom) * .002)
phy set vehicle lateral tire force function 2, 2, 0.5, 2.0, 4.0, 1.0, 200.0 - ((mom) * .002)
phy set vehicle lateral tire force function 2, 3, 0.5, 2.0, 4.0, 1.0, 200.0 - ((mom) * .002)
`==============================
rem follow object invisible
make object sphere 3,10 : hide object 3
`===================================
rem DO
do
//-----Time Stuff
TimeOld = TimeNew
TimeNew = timer()
TimeElapsed = TimeNew - TimeOld
`===================================
rem Follow the Car
position object 3,object position x(2),object position y(2),object position z(2)
set object to object orientation 3,2
move object 3,-10
point object 3,object position x(2),object position y(2),object position z(2)
dist#=5.0 : high#=2.5 : angle#=object angle y(3)+90
set camera to follow object position x(2), object position y(2), object position z(2), angle#, dist#, high#, 1, 0
`
`===================================
rem Control the Car
phy set vehicle motor force 2,0.0
gosub driveCar
`====================================
`Default Cam, Key # 1
if KEYSTATE(cnScanCode_1)=1 then camnum = 1
if camnum = 1 then dist#=5.0 : high#=2.5 : angle#=object angle y(3)+90
`Close Chase Cam, Key # 2
if KEYSTATE(cnScanCode_2)=1 then camnum = 2
if camnum = 2 then dist#=8.0 : high#=4.0 : angle#=object angle y(3)+90
` Far Chase Cam, Key # 3
if KEYSTATE(cnScanCode_3)=1 then camnum = 3
if camnum = 3 then dist#=11.0 : high#=4.0 : angle#=object angle y(3)+90
` Right Front Cam, Key # 4
if KEYSTATE(cnScanCode_4)=1 then camnum = 4
if camnum = 4 then dist#=10.0 : high#=4.0 : angle#=object angle y(3)-15
` Left Front Cam, Key # 5
if KEYSTATE(cnScanCode_5)=1 then camnum = 5
if camnum = 5 then dist#=10.0 : high#=4.0 : angle#=object angle y(3)-150
set camera to follow object position x(2), object position y(2), object position z(2), angle#, dist#, high#, 1, 0
`====================================
set cursor 0,0
print " SCREEN FPS="+str$(screen fps())
//------------------------------------------------------------------
// Jason - Begin MPH Code
//------------------------------------------------------------------
rCarPosOld.X# = rCarPosNew.X#
rCarPosOld.Y# = rCarPosNew.Y#
rCarPosOld.Z# = rCarPosNew.Z#
rCarPosNew.X# = Object Position x(2)
rCarPosNew.Y# = Object Position Y(2)
rCarPosNew.Z# = Object Position z(2)
DistTraveled# = Distance(rCarPosOld.X#,rCarPosOld.Y#,rCarPosOld.Z#,rCarPosNew.X#,rCarPosNew.Y#,rCarPosNew.Z#)
MPH# = (DistTraveled# / TimeElapsed) * MphScale#
array insert at bottom aMph#(0)
HowManyInArray = array count(aMph#(0))
aMPH#(HowManyInArray)=MPH#
if HowManyInArray >= cnSpeedArraySize
array delete element aMph#(0),0
endif
TotalMph# = 0
for i = 1 to HowManyInArray
TotalMph# = TotalMPH# + aMPH#(i-1)
next i
AvgMph# = TotalMph#/HowManyInArray
Print "rCarPosOld.X#:";rCarPosOld.X#
Print "rCarPosOld.Y#:";rCarPosOld.Y#
Print "rCarPosOld.Z#:";rCarPosOld.Z#
Print "rCarPosNew.X#:";rCarPosNew.X#
Print "rCarPosNew.Y#:";rCarPosNew.Y#
Print "rCarPosNew.Z#:";rCarPosNew.Z#
print "TimeElapsed:";TimeElapsed
Print "Distance Traveled:";DistTraveled#
Print "HowMany In array:";HowManyInArray
Print "Mph#:";mph#
Print "AvgMph:"; AvgMph#
// Jason - End MPH Code
//------------------------------------------------------------------
`====================================
rem Update physics
phy update
sync
loop
`============================
`============================
driveCar:
rem movement
key = 0 : force=0.0
if KEYSTATE(cnScanCode_UpArrow) = 1 then force=200 : key = 1 : wheelmult = 50 : prevforce = 300
if KEYSTATE(cnScanCode_DownArrow) = 1 then force=-200.0 : key = 1 : wheelmult = -50 : prevforce = -200
if key = 0 then force=0.0
phy set vehicle motor force 2,force
phy set vehicle wheel multiplier 2, wheelmult
rem steering
steeringAngle = phy get vehicle steering angle ( 2 )
key = 0
if keystate ( cnScanCode_RightArrow ) = 1
if steeringAngle > ( -1.0 + steeringDelta )
steeringAngle = steeringAngle - ( steeringDelta / 2 )
endif
key = 1
endif
if keystate ( cnScanCode_LeftArrow ) = 1
if steeringAngle < ( 1.0 - steeringDelta )
steeringAngle = steeringAngle + ( steeringDelta / 2 )
endif
key = 1
endif
if key = 0
if steeringAngle > 0.0
steeringAngle = steeringAngle - steeringDelta * 2
if steeringAngle < 0
steeringAngle = 0.0
endif
endif
if steeringAngle < 0.0
steeringAngle = steeringAngle + steeringDelta * 2
if steeringAngle > 0
steeringAngle = 0.0
endif
endif
endif
phy set vehicle steering angle 2, steeringAngle
phy set vehicle steering value 2, -( steeringAngle * 45 )
return
`------------------------------------------------------------------------------------------------------------
Function Distance(xpos#,ypos#,zpos#,nx#,ny#,nz#)
`------------------------------------------------------------------------------------------------------------
` SQRT Way
`nx#=object position x(objnum)-xpos#
`ny#=object position y(objnum)-ypos#
`nz#=object position z(objnum)-zpos#
`distance#=sqrt((nx#*nx#)+(ny#*ny#)+(nz#*nz#))
` VECTOR WAY
set vector3 cnVectorDistance, xpos# - nx#, ypos# - ny#, zpos# - nz#
distance# = length vector3(cnVectorDistance)
ENDFUNCTION distance#
`------------------------------------------------------------------------------------------------------------
I'm off to take my son snowboarding - so I won't be around until tonight most likely.
Good Luck. Back up your work before overwriting what ya have with mine - just to be safe. I commented code - and tossed in a bunch of keyboard scancodes for using the keystate command.
Jason