Hey folks,
The classic question of rag doll physics comes up occasionally and as I’m working with this stuff right now I thought I’d ask you to post your favourite rope/chain/string implementation.
For those who don’t know, the problem of joints coming apart is a tricky one and lots of guys have their own solutions, shaped by solving their particular problem. So, let’s see them!
Please post your fully working code, either here (with a proper [code] block) or as a gist.github.com link.
Here’s mine:
local physics = require("physics") physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode("hybrid") local density, factor = .1, 10 local group = display.newGroup() for i=1, 40 do local rect = display.newCircle( group, 0+i\*20, 100, 10 ) physics.addBody( rect, "dynamic", { density=density\*factor, radius=10 } ) end local a = display.newCircle( group[1].x-55, group[1].y, 50 ) local b = display.newCircle( group[group.numChildren].x+55, group[group.numChildren].y, 50 ) physics.addBody( a, "dynamic", { radius=50, density=density } ) physics.addBody( b, "dynamic", { radius=50, density=density } ) physics.newJoint( "pivot", a, group[1], group[1].x-5, group[1].y ) physics.newJoint( "pivot", b, group[group.numChildren], group[group.numChildren].x+5, group[group.numChildren].y ) for i=1, group.numChildren do if (i == 1) then physics.newJoint( "rope", a, group[1] ) physics.newJoint( "rope", group[1], group[2] ) elseif (i == group.numChildren) then physics.newJoint( "rope", b, group[group.numChildren] ) else physics.newJoint( "rope", group[i], group[i+1] ) end end physics.addBody( display.newCircle( display.contetCenterX, display.contentCenterY, 100 ), "static", { radius=100, density=density } ) timer.performWithDelay( 1000, function() physics.setGravity( 0, 10 ) a.isAwake = true b.isAwake = true end, 1 )