[code]
display.setStatusBar( display.HiddenStatusBar )
local physics
local TRACTOR_WIDTH = 40
local TRACTOR_HEIGHT = 75
local TRACTOR_SPEED = 15
local tractor
local motor0
local motor1
local motor2
local motor3
local motor0Joint
local motor1Joint
local motor2Joint
local motor3Joint
local STEER_SPEED = 200
local MAX_STEER_ANGLE = 30
local leftBtn = display.newRect( 10, 420, 50, 50)
leftBtn:setFillColor(200,100,0)
local rightBtn = display.newRect( 70, 420, 50, 50)
local createPhysics = function ()
physics = require( “physics” )
physics.start()
physics.setGravity(0,0)
physics.setDrawMode( “hybrid” )
physics.setVelocityIterations( 10 )
end
local createTractor = function()
tractor = display.newRect(0,0,40,75)
tractor.x = 320*.5 -20
tractor.y = 480-80
physics.addBody(tractor,“dynamic”,{density=3,friction=0,bounce=0.5})
tractor.linearDamping = 1
tractor.angularDamping = 3
motor0 = display.newRect(tractor.x-TRACTOR_WIDTH*0.5-3.5,tractor.y-TRACTOR_HEIGHT*0.5,7,14)
physics.addBody(motor0,dynamic,{density=0.1,friction=0,bounce=0})
motor1 = display.newRect(tractor.x+TRACTOR_WIDTH*0.5-3.5,tractor.y-TRACTOR_HEIGHT*0.5,7,14)
physics.addBody(motor1,dynamic,{density=0.1,friction=0,bounce=0})
motor2 = display.newRect(tractor.x-TRACTOR_WIDTH*0.5-3.5,tractor.y+TRACTOR_HEIGHT*0.5-14,7,14)
physics.addBody(motor2,dynamic,{density=0.1,friction=0,bounce=0})
motor3 = display.newRect(tractor.x+TRACTOR_WIDTH*0.5-3.5,tractor.y+TRACTOR_HEIGHT*0.5-14,7,14)
physics.addBody(motor3,dynamic,{density=0.1,friction=0,bounce=0})
motor0Joint = physics.newJoint( “pivot”, tractor, motor0, motor0.x,motor0.y )
motor1Joint = physics.newJoint( “pivot”, tractor, motor1, motor1.x,motor1.y )
motor2Joint = physics.newJoint( “pivot”, tractor, motor2, motor2.x,motor2.y )
motor3Joint = physics.newJoint( “pivot”, tractor, 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)
lbodySideWayY = bodySideWayY * (bodySideWayXbodySpeedX + bodySpeedYbodySideWayY)
body:setLinearVelocity(bodySideWayX,bodySideWayY)
end]]–
local wheelDirectionX
local wheelDirectionY
local steeringAngle = 0
local render = function (e)
--[[killOrthogonalVelocity(motor0)
killOrthogonalVelocity(motor1)
killOrthogonalVelocity(motor2)
killOrthogonalVelocity(motor3)]]–
--Driving
wheelLDirectionX = math.sin(motor0.rotation/180math.pi) * TRACTOR_SPEED
wheelLDirectionY = math.cos(motor0.rotation/180math.pi) * TRACTOR_SPEED
wheelRDirectionX = math.sin(motor1.rotation/180math.pi) * TRACTOR_SPEED
wheelRDirectionY = math.cos(motor1.rotation/180math.pi) * TRACTOR_SPEED
motor0:applyForce(wheelLDirectionX,-wheelLDirectionY,motor0.x,motor0.y)
motor1:applyForce(wheelRDirectionX,-wheelRDirectionY,motor1.x,motor1.y)
--Steering
local mspeed
mspeed = math.rad( steeringAngle - motor0Joint.jointAngle )
motor0Joint.motorSpeed = mspeed * STEER_SPEED
mspeed = math.rad( steeringAngle - motor1Joint.jointAngle )
motor1Joint.motorSpeed = mspeed * STEER_SPEED
end
local onRightController = function (event)
if event.phase == “began” then
steeringAngle = MAX_STEER_ANGLE
elseif event.phase == “ended” then
steeringAngle = 0
end
end
local onLeftController = function (event)
if event.phase == “began” then
steeringAngle = -MAX_STEER_ANGLE
elseif event.phase == “ended” then
steeringAngle = 0
end
end
local start = function()
Runtime:addEventListener( “enterFrame”, render )
leftBtn:addEventListener(“touch”, onLeftController)
rightBtn:addEventListener(“touch”, onRightController)
end
createPhysics()
createTractor()
start()
[/code]
I found this elsewhere and edited it to my liking, it handled terrible at first but now it is much better. If you copy and paste this, it should work as is.
It works great! But I would like to know why it works. Lines 125-140 are very puzzling to me, I understand what it is asking, but why the math is all there and how it is called makes me interested, perhaps you could elaborate?
Other than that, this pretty much solves all my issues. Next I have to control acceleration with buttons, which shouldn’t be too hard. After that set perimeter of the “field” as well as attach a camera to the tractor.
Using this, I plan on passing it parameters to change wether it is a combine, or what it is. That would make it very universal.
As far as the “plowing” I looked into the masking. I think (correct me if I’m wrong) that I can “tow” something behind the tractor, and have that unmask an image as it goes? Could I use event.contact for this? Not too sure, I could get it eventually I think!
Thank you again 
Ryley [import]uid: 28237 topic_id: 34974 reply_id: 139635[/import]