Hi,
Recently I’m developing a race game and I need a top view car simulation in Box2D.
I just found this example: (1st example)
http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/
I’ve ported the it to Lua:
[lua]display.setStatusBar( display.HiddenStatusBar )
local physics
local CAR_WIDTH = 40
local CAR_HEIGHT = 75
local CAR_SPEED = 4
local car
local motor0
local motor1
local motor2
local motor3
local motor0Joint
local motor1Joint
local motor2Joint
local motor3Joint
local positionController = display.newRect(0,0,320,480)
positionController.alpha = 0.01
local createPhysics = function ()
physics = require( “physics” )
physics.start()
physics.setGravity(0,0)
physics.setDrawMode( “debug” )
end
local createCar = function()
car = display.newRect(0,0,40,75)
car.x = 320*.5 -20
car.y = 480-80
physics.addBody(car,“dynamic”,{density=3,friction=0,bounce=0.5})
motor0 = display.newRect(car.x-CAR_WIDTH*0.5-3.5,car.y-CAR_HEIGHT*0.5,7,14)
physics.addBody(motor0,dynamic,{density=0.1,friction=0,bounce=0})
motor1 = display.newRect(car.x+CAR_WIDTH*0.5-3.5,car.y-CAR_HEIGHT*0.5,7,14)
physics.addBody(motor1,dynamic,{density=0.1,friction=0,bounce=0})
motor2 = display.newRect(car.x-CAR_WIDTH*0.5-3.5,car.y+CAR_HEIGHT*0.5-14,7,14)
physics.addBody(motor2,dynamic,{density=0.1,friction=0,bounce=0})
motor3 = display.newRect(car.x+CAR_WIDTH*0.5-3.5,car.y+CAR_HEIGHT*0.5-14,7,14)
physics.addBody(motor3,dynamic,{density=0.1,friction=0,bounce=0})
motor0Joint = physics.newJoint( “pivot”, car, motor0, motor0.x,motor0.y )
motor1Joint = physics.newJoint( “pivot”, car, motor1, motor1.x,motor1.y )
motor2Joint = physics.newJoint( “pivot”, car, motor2, motor2.x,motor2.y )
motor3Joint = physics.newJoint( “pivot”, car, motor3, motor3.x,motor3.y )
motor0Joint.isMotorEnabled = true
motor0Joint.motorSpeed = 0
motor0Joint.maxMotorTorque = 100
motor0Joint.isLimitEnabled = true
motor0Joint:setRotationLimits(-30,30)
motor1Joint.isMotorEnabled = true
motor1Joint.motorSpeed = 0
motor1Joint.maxMotorTorque = 100
motor1Joint.isLimitEnabled = true
motor1Joint:setRotationLimits(-30,30)
motor2Joint.isLimitEnabled = true
motor2Joint:setRotationLimits(0,0)
motor3Joint.isLimitEnabled = true
motor3Joint:setRotationLimits(0,0)
motor0.isSensor = true
motor1.isSensor = true
motor2.isSensor = true
motor3.isSensor = true
end
local bodySpeedX
local bodySpeedY
local bodySideWayX
local bodySideWayY
local killOrthogonalVelocity = function(body)
bodySpeedX,bodySpeedY = body:getLinearVelocity()
bodySideWayX = math.sin(body.rotation/180*math.pi)
bodySideWayY = math.cos(body.rotation/180*math.pi)
– multipyl with dot(,)
bodySideWayX = bodySideWayX * (bodySideWayXbodySpeedX + bodySpeedYbodySideWayY)
bodySideWayY = bodySideWayY * (bodySideWayXbodySpeedX + bodySpeedYbodySideWayY)
body:setLinearVelocity(bodySideWayX,bodySideWayY)
end
local wheelDirectionX
local wheelDirectionY
local targetWheelAngle
local targetSteer = 0
local render = function (e)
killOrthogonalVelocity(motor0)
killOrthogonalVelocity(motor1)
killOrthogonalVelocity(motor2)
killOrthogonalVelocity(motor3)
wheelDirectionX = math.sin(-motor0.rotation/180math.pi)
wheelDirectionY = math.cos(-motor0.rotation/180math.pi)
motor0:applyForce(-wheelDirectionXCAR_SPEED,-wheelDirectionYCAR_SPEED,motor0.x,motor0.y)
motor1:applyForce(-wheelDirectionXCAR_SPEED,-wheelDirectionYCAR_SPEED,motor1.x,motor0.y)
targetWheelAngle = (targetSteer30 - motor0Joint.jointAngle)
motor0Joint.motorSpeed = targetWheelAngle * 10
targetWheelAngle = (targetSteer30 - motor1Joint.jointAngle)
motor1Joint.motorSpeed = targetWheelAngle * 10
end
local controll = function (event)
if event.phase ~= “ended” or event.phase ~= “cancelled” then
targetSteer = ((event.x/320)-.5)*2
end
end
local start = function()
Runtime:addEventListener( “enterFrame”, render )
positionController:addEventListener(“touch”,controll)
end
createPhysics()
createCar()
start()[/lua]
The problem is at killOrthogonalVelocity method. When I change the linear velocity of wheels which are bunch of rectangle objects connected to car body by pivot joint, It doesn’t set the velocity. Even If I set the velocities of wheels to 0,0 car keeps moving.
Is this a bug or?
Thank you! [import]uid: 5629 topic_id: 11567 reply_id: 311567[/import]