Nice work!
Here’s a simple way to bundle it into a module. Firstly, the calling code in main.lua:
require "ragdoll"
--\> Setup Display
display.setStatusBar (display.HiddenStatusBar)
system.activate ("multitouch")
local walls = ragdoll.createWalls()
local doll1 = ragdoll.newRagDoll(40, 320)
local doll2 = ragdoll.newRagDoll(280, 320)
And here’s ragdoll.lua (all your code of course):
[code]
module(…, package.seeall)
local gameUI = require(“gameUI”)
–> Start Physics
local physics = require (“physics”)
physics.start ()
physics.setGravity (0, 10)
–physics.setDrawMode (“hybrid”)
function createWalls()
–> Create Walls
local walls = display.newGroup()
local leftWall = display.newRect (0, 0, 1, display.contentHeight)
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight)
local ceiling = display.newRect (0, 0, display.contentWidth, 1)
local floor = display.newRect (0, display.contentHeight, display.contentWidth, 1)
physics.addBody (leftWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (rightWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (ceiling, “static”, {bounce = 0.0, friction = 10})
physics.addBody (floor, “static”, {bounce = 0.0, friction = 10})
walls:insert(leftWall)
walls:insert(rightWall)
walls:insert(ceiling)
walls:insert(floor)
return walls
end
function newRagDoll(originX, originY)
–> Create Ragdoll Group
local spacing = 1
local ragdoll = display.newGroup ()
local head = display.newCircle( 0, 0, 15 )
–local head = display.newImage( “head.png” )
head.x = originX
head.y = originY
head:setFillColor (255, 255, 255, 128)
ragdoll:insert (head)
local torsoA = display.newRect( 0, 0, 45, 24 )
torsoA.x = originX
torsoA.y = originY + head.height
torsoA:setFillColor (255, 255, 255, 128)
ragdoll:insert (torsoA)
local torsoB = display.newRect( 0, 0, 37.5, 24 )
torsoB.x = originX
torsoB.y = torsoA.y + (torsoA.height * 0.5) + spacing
torsoB:setFillColor (255, 255, 255, 128)
ragdoll:insert (torsoB)
local torsoC = display.newRect( 0, 0, 30, 24 )
torsoC.x = originX
torsoC.y = torsoB.y + (torsoB.height * 0.5) + spacing
torsoC:setFillColor (255, 255, 255, 128)
ragdoll:insert (torsoC)
local pelvis = display.newRect( 0, 0, 30, 24 )
pelvis.x = originX
pelvis.y = torsoC.y + (torsoC.height * 0.5) + spacing
pelvis:setFillColor (255, 255, 255, 128)
ragdoll:insert (pelvis)
local leftLegA = display.newRect( 0, 0, 12, 52.5 )
leftLegA.x = pelvis.x - ((pelvis.width - leftLegA.width) * 0.5)
leftLegA.y = pelvis.y + (leftLegA.height * 0.5) + spacing
leftLegA:setFillColor (255, 255, 255, 128)
ragdoll:insert (leftLegA)
local rightLegA = display.newRect( 0, 0, 12, 52.5 )
rightLegA.x = pelvis.x + ((pelvis.width - rightLegA.width) * 0.5)
rightLegA.y = pelvis.y + (rightLegA.height * 0.5) + spacing
rightLegA:setFillColor (255, 255, 255, 128)
ragdoll:insert (rightLegA)
local leftLegB = display.newRect( 0, 0, 12, 45 )
leftLegB.x = leftLegA.x
leftLegB.y = leftLegA.y + rightLegA.height - 12
leftLegB:setFillColor (255, 255, 255, 128)
ragdoll:insert (leftLegB)
local rightLegB = display.newRect( 0, 0, 12, 45 )
rightLegB.x = rightLegA.x
rightLegB.y = rightLegA.y + rightLegA.height - 12
rightLegB:setFillColor (255, 255, 255, 128)
ragdoll:insert (rightLegB)
local leftArmA = display.newRect( 125, 70, 10.5, 39 )
leftArmA.x = torsoA.x - (torsoA.width * 0.5) - 3
leftArmA.y = torsoA.y + (leftArmA.height * 0.5) - 6
leftArmA:setFillColor (255, 255, 255, 128)
ragdoll:insert (leftArmA)
local rightArmA = display.newRect( 185, 70, 10.5, 39 )
rightArmA.x = torsoA.x + (torsoA.width * 0.5) + 3
rightArmA.y = torsoA.y + (rightArmA.height * 0.5) - 6
rightArmA:setFillColor (255, 255, 255, 128)
ragdoll:insert (rightArmA)
local leftArmB = display.newRect( 125, 105, 10.5, 37.5 )
leftArmB.x = leftArmA.x
leftArmB.y = leftArmA.y + (leftArmA.height) - 6
leftArmB:setFillColor (255, 255, 255, 128)
ragdoll:insert (leftArmB)
local rightArmB = display.newRect( 185, 105, 10.5, 37.5 )
rightArmB.x = rightArmA.x
rightArmB.y = rightArmA.y + (rightArmA.height) - 6
rightArmB:setFillColor (255, 255, 255, 128)
ragdoll:insert (rightArmB)
physics.addBody (head, {bounce = 0.0, friction = 1.0})
physics.addBody (torsoA, {bounce = 0.0, friction = 1.0})
physics.addBody (torsoB, {bounce = 0.0, friction = 1.0})
physics.addBody (torsoC, {bounce = 0.0, friction = 1.0})
physics.addBody (pelvis, {bounce = 0.0, friction = 1.0})
physics.addBody (leftLegA, {bounce = 0.0, friction = 1.0})
physics.addBody (rightLegA, {bounce = 0.0, friction = 1.0})
physics.addBody (leftLegB, {bounce = 0.0, friction = 1.0})
physics.addBody (rightLegB, {bounce = 0.0, friction = 1.0})
physics.addBody (leftArmA, {bounce = 0.0, friction = 1.0})
physics.addBody (rightArmA, {bounce = 0.0, friction = 1.0})
physics.addBody (leftArmB, {bounce = 0.0, friction = 1.0})
physics.addBody (rightArmB, {bounce = 0.0, friction = 1.0})
local neckJoint = physics.newJoint ( “pivot”, head, torsoA, torsoA.x, torsoA.y)
neckJoint.isLimitEnabled = true
neckJoint:setRotationLimits ( -22.5, 22.5 )
local backboneA = physics.newJoint ( “pivot”, torsoA, torsoB, torsoB.x, torsoB.y )
backboneA.isLimitEnabled = true
backboneA:setRotationLimits ( -22.5, 22.5 )
local backboneB = physics.newJoint ( “pivot”, torsoB, torsoC, torsoC.x, torsoC.y )
backboneB.isLimitEnabled = true
backboneB:setRotationLimits ( -22.5, 22.5 )
local backboneC = physics.newJoint ( “pivot”, torsoC, pelvis, pelvis.x, pelvis.y )
backboneC.isLimitEnabled = true
backboneC:setRotationLimits ( -22.5, 22.5 )
local leftHip = physics.newJoint ( “pivot”, pelvis, leftLegA, leftLegA.x, pelvis.y )
leftHip.isLimitEnabled = true
leftHip:setRotationLimits ( -45, 90 )
local rightHip = physics.newJoint ( “pivot”, pelvis, rightLegA, rightLegA.x, pelvis.y )
rightHip.isLimitEnabled = true
rightHip:setRotationLimits ( -90, 45 )
local leftKnee = physics.newJoint ( “pivot”, leftLegA, leftLegB, leftLegB.x, leftLegA.y + (leftLegA.height * 0.5) - 6 )
leftKnee.isLimitEnabled = true
leftKnee:setRotationLimits ( -45, 90 )
local rightKnee = physics.newJoint ( “pivot”, rightLegA, rightLegB, rightLegB.x, rightLegA.y + (rightLegA.height * 0.5) - 6 )
rightKnee.isLimitEnabled = true
rightKnee:setRotationLimits ( -90, 45 )
local leftShoulder = physics.newJoint ( “pivot”, torsoA, leftArmA, leftArmA.x, torsoA.y )
leftShoulder.isLimitEnabled = true
leftShoulder:setRotationLimits ( 0, 180 )
local rightShoulder = physics.newJoint ( “pivot”, torsoA, rightArmA, rightArmA.x, torsoA.y )
rightShoulder.isLimitEnabled = true
rightShoulder:setRotationLimits ( -180, 0 )
local leftElbow = physics.newJoint ( “pivot”, leftArmA, leftArmB, leftArmB.x, leftArmA.y + (leftArmA.height * 0.5) - 6 )
leftElbow.isLimitEnabled = true
leftElbow:setRotationLimits ( -45, 90 )
local rightElbow = physics.newJoint ( “pivot”, rightArmA, rightArmB, rightArmB.x, rightArmA.y + (rightArmA.height * 0.5) - 6 )
rightElbow.isLimitEnabled = true
rightElbow:setRotationLimits ( -90, 45 )
function ragdoll:touch( event )
gameUI.dragBody( event )
end
head:addEventListener ( “touch”, ragdoll )
leftLegA:addEventListener ( “touch”, ragdoll )
leftLegB:addEventListener ( “touch”, ragdoll )
rightLegA:addEventListener ( “touch”, ragdoll )
rightLegB:addEventListener ( “touch”, ragdoll )
leftArmA:addEventListener ( “touch”, ragdoll )
leftArmB:addEventListener ( “touch”, ragdoll )
rightArmA:addEventListener ( “touch”, ragdoll )
rightArmB:addEventListener ( “touch”, ragdoll )
return ragdoll
end
[/code] [import]uid: 3953 topic_id: 3194 reply_id: 10198[/import]