the Main:
function setup()
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
w = WIDTH
h = HEIGHT
getImages()
debugDraw = PhysicsDebugDraw()
debugDraw:init()
--manager = SceneManager()
--scenes = {}
--manager:init(scenes)
createWorld()
pc = color(125, 30, 30, 255)
npc = color(255, 0, 0, 255)
buttonC = button("closeButton",w - 70,h - 70,w - 30,h - 30,pc,npc,"rect")
buttonR = button("right",w - 70,30,w - 30,70,pc,npc,"rect")
buttonL = button("left",w - 170,30,w - 130,70,pc,npc,"rect")
buttonS = button("stop",w - 170,100,w - 30,140,pc,npc,"rect")
frame = 0
end
function draw()
background(40, 40, 50)
touched()
drawButtons()
debugDraw:draw()
frame = frame + 1
if rotatingLeft then
wheel.angle = wheel.angle + 5
elseif rotatingRight then
wheel.angle = wheel.angle - 5
end
end
function touched()
t = CurrentTouch
buttonC:touched(t)
buttonR:touched(t)
buttonL:touched(t)
buttonS:touched(t)
debugDraw:touched(t)
end
function drawButtons()
buttonC:draw()
buttonR:draw()
buttonL:draw()
buttonS:draw()
local xx = (w - 170 + w - 30) / 2
local yy = (100 + 140) / 2
font("ArialRoundedMTBold")
fill(0, 0, 0, 255)
fontSize(15)
text("stop",xx,yy)
local Cstate = buttonC:getInfo()[2]
local Rstate = buttonR:getInfo()[2]
local Lstate = buttonL:getInfo()[2]
if Cstate == 3 then
close()
end
if Rstate== 1 or Rstate == 2 or Rstate == 3 then
rotatingRight = true
else
rotatingRight = false
end
if Lstate == 1 or Lstate == 2 or Lstate == 3 then
rotatingLeft = true
else
rotatingLeft = false
end
end
function createWorld()
physics.pause()
-- Creating the crane:
crane = physics.body(POLYGON,
vec2(-5,0),
vec2(-5,-50),
vec2(-45,-50),
vec2(-45,-60),
vec2(45,-60),
vec2(45,-50),
vec2(5,-50),
vec2(5,0))
crane.x = 300
crane.y = h - 100
crane.mass = 0.5
debugDraw:addBody(crane)
-- Creating the chainpoints:
cp1 = physics.body(CIRCLE,10)
cp1.x = 315
cp1.y = h - 80
cp1.type = STATIC
debugDraw:addBody(cp1)
cp2 = physics.body(CIRCLE,10)
cp2.x = 285
cp2.y = h - 80
cp2.type = STATIC
debugDraw:addBody(cp2)
-- Creating the chain itself:
--chain = physics.body(CHAIN,
-- vec2(300,h - 100),
-- vec2(300,h - 90),
-- vec2(300,h - 80),
-- vec2(300,h - 70),
-- vec2(300,h - 60),
-- vec2(310,h - 60),
-- vec2(320,h - 60),
-- vec2(330,h - 60),
-- vec2(340,h - 60),
-- vec2(350,h - 60),
-- vec2(360,h - 60))
--debugDraw:addBody(chain)
-- hmmm... the chain seems always to be static. lets try it like this:
chain = {}
chainJoints = {}
local xx = 300
local yy = h - 100
for i = 1, 3 do
chain[i] = physics.body(POLYGON,
vec2(xx - 1,yy),
vec2(xx - 1,yy + 20),
vec2(xx + 1,yy + 20),
vec2(xx + 1,yy))
debugDraw:addBody(chain[i])
chainJoints[i] = physics.joint(REVOLUTE,chain[i - 1],chain[i],vec2(xx,yy))
debugDraw:addJoint(chainJoints[i])
yy = yy + 20
end
for i = 4,15 do
chain[i] = physics.body(POLYGON,
vec2(xx ,yy - 1),
vec2(xx + 20,yy - 1),
vec2(xx + 20,yy + 1),
vec2(xx,yy + 1))
debugDraw:addBody(chain[i])
chainJoints[i] = physics.joint(REVOLUTE,chain[i - 1],chain[i],vec2(xx,yy))
debugDraw:addJoint(chainJoints[i])
xx = xx + 20
end
for i = 16,21 do
chain[i] = physics.body(POLYGON,
vec2(xx + 1,yy),
vec2(xx - 1,yy),
vec2(xx - 1,yy - 20),
vec2(xx + 1,yy - 20))
debugDraw:addBody(chain[i])
chainJoints[i] = physics.joint(REVOLUTE,chain[i - 1],chain[i],vec2(xx,yy))
debugDraw:addJoint(chainJoints[i])
yy = yy - 20
end
for i = 22,28 do
chain[i] = physics.body(POLYGON,
vec2(xx ,yy - 1),
vec2(xx - 20,yy - 1),
vec2(xx - 20,yy + 1),
vec2(xx,yy + 1))
debugDraw:addBody(chain[i])
chainJoints[i] = physics.joint(REVOLUTE,chain[i - 1],chain[i],vec2(xx,yy))
debugDraw:addJoint(chainJoints[i])
xx = xx - 20
end
-- Creating a joint between the crane and the chain:
craneConnection = physics.joint(REVOLUTE,chain[1],crane,vec2(300,h - 100))
debugDraw:addJoint(craneConnection)
-- Creating the wheel:
wheel = physics.body(CIRCLE,30)
wheel.x = 400
wheel.y = h - 130
wheel.type = STATIC
debugDraw:addBody(wheel)
-- Creating a joint between the wheel and the chain:
wheelConnection = physics.joint(REVOLUTE,chain[#chain],wheel,vec2(400,h - 160))
debugDraw:addJoint(wheelConnection)
physics.resume()
end