This is far from a complete system but I dug it out for another post and thought it might be useful for anyone wanting to make a top down physics based driving game like those parking lot games you see on miniclip etc.
It could easily be altered to make more complicated vehicles like trucks with trailers etc. No media required for this version...
Hope you can find it useful!!
rem
rem AGK Application
rem
rem Landscape App
#constant VW 640
#constant VH 480
setVirtualResolution(VW,VH)
rem set physics world up
setPhysicsScale(0.15)
setPhysicsGravity(0,0)
setPhysicsDebugOn()
rem create UDT
type vehicleType
body as integer
wheelFL as integer
wheelFR as integer
wheelBL as integer
wheelBR as integer
maxturn as float
endtype
global car as vehicleType
car.maxturn = 30.0
rem create car body
carl# = 100
carw# = 60
car.body = createObject(carl#,carw#,VW/2,VH/2)
setSpritePhysicsDamping(car.body,0.9)
setSpritePhysicsAngularDamping(car.body,0.9)
rem create wheels
l# = 20
w# = 12
car.wheelFL = createObject(l#,w#,VW*0.5+carl#*0.3,VH*0.5-carw#*0.5)
car.wheelFR = createObject(l#,w#,VW*0.5+carl#*0.3,VH*0.5+carw#*0.5)
car.wheelBL = createObject(l#,w#,VW*0.5-carl#*0.3,VH*0.5-carw#*0.5)
car.wheelBR = createObject(l#,w#,VW*0.5-carl#*0.3,VH*0.5+carw#*0.5)
j1 = createRevoluteJoint(car.wheelFL,car.body,getSpriteXbyOffset(car.wheelFL),getSpriteYbyOffset(car.wheelFL),0)
j2 = createRevoluteJoint(car.wheelFR,car.body,getSpriteXbyOffset(car.wheelFR),getSpriteYbyOffset(car.wheelFR),0)
j3 = createWeldJoint(car.wheelBL,car.body,getSpriteXbyOffset(car.wheelBL),getSpriteYbyOffset(car.wheelBL),0)
j4 = createWeldJoint(car.wheelBR,car.body,getSpriteXbyOffset(car.wheelBR),getSpriteYbyOffset(car.wheelBR),0)
rem A baxslash Did It!
do
rem get input
Print("Use arrowkeys to drive and shift to skid")
ACC# = getRawKeyState(38)-getRawKeyState(40)
TURN# = getRawKeyState(39)-getRawKeyState(37)
ANGLE# = ANGLE# + TURN#*2.0
if TURN#=0
ANGLE# = ANGLE#*0.9
endif
if ANGLE#>car.maxturn then ANGLE# = car.maxturn
if ANGLE#<-car.maxturn then ANGLE# = -car.maxturn
rem control vehicle
`restrict lateral movement
restrictLateralMovement(car.wheelFL,0.9)
restrictLateralMovement(car.wheelFR,0.9)
`back wheels can skid
if getRawKeyState(16)>0
restrictLateralMovement(car.wheelBL,0.1)
restrictLateralMovement(car.wheelBR,0.1)
else
restrictLateralMovement(car.wheelBL,0.7)
restrictLateralMovement(car.wheelBR,0.7)
endif
`apply acceleration
a# = getSpriteAngle(car.wheelFL)
vx# = cos(a#)*ACC#*500
vy# = sin(a#)*ACC#*500
x# = getSpriteXbyOffset(car.wheelFL)
y# = getSpriteYbyOffset(car.wheelFL)
setSpritePhysicsImpulse(car.wheelFL,x#,y#,vx#,vy#)
x# = getSpriteXbyOffset(car.wheelFR)
y# = getSpriteYbyOffset(car.wheelFR)
setSpritePhysicsImpulse(car.wheelFR,x#,y#,vx#,vy#)
`turn wheels
a# = getSpriteAngle(car.body)
setSpriteAngle(car.wheelFL,a#+ANGLE#)
setSpriteAngle(car.wheelFR,a#+ANGLE#)
sync()
loop
function restrictLateralMovement(spr,amount#)
vx# = getSpritePhysicsVelocityX(spr)
vy# = getSpritePhysicsVelocityY(spr)
v# = sqrt(vx#*vx#+vy#*vy#)
ta# = atanfull(vx#,vy#)
a# = getSpriteAngle(spr)
da# = ta#-a#
lv# = v#*sin(da#)
newvx# = cos(a#)*lv#
newvy# = sin(a#)*lv#
vx# = vx#-(vx#-newvx#)*amount#
vy# = vy#-(vy#-newvy#)*amount#
setSpritePhysicsVelocity(spr,vx#,vy#)
endfunction
function createObject(lx#,ly#,x#,y#)
spr = createSprite(0)
setSpriteSize(spr,lx#,ly#)
setSpritePositionByOffset(spr,x#,y#)
setSpritePhysicsOn(spr,2)
setSpriteVisible(spr,0)
endfunction spr
this.mess = abs(sin(times#))