Hi guys,
Does anybody know how to make the rear end (or tyres) of a 2D car slide? I'm basing my initial physics model on the code Baxslash kindly provided back in 2012 (see below). I've tried every single relevant sprite and physics command (e.g. SetSpritePhysicsFriction, set the sprite's mass and COM), but the rear wheels just trundle along behind. The only permanent change I have made to Baxslash's code is to apply the setSpritePhysicsImpulse to the rear wheels, not the front, because I want to simulate a rear wheel drive vehicle.
BTW, thanks Baxslash for the code. Figuring out those physics calculations myself would have taken forever!
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