Your best physics chain code

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 )

“best” in what sense?  what are you trying to accomplish?

“best” at stability / at not being stretchy?  solution depends on type of chain.  You can make valid chains out of rope, pivot or distance joints, though they represent different things.  (fe a bike’s chain is rigid, should be rectangles and pivot joints)

“best” at being stretchy (elastic) but not “exploding”?  you appear to have two joints (a pivot and rope) attached to both end balls, was that to keep the final rope joint from compressing? (if so, then you probably don’t need the rope joint)

“best” at conciseness?  i could offer some thoughts on reducing-complexity improving-readability of the presented code, but they’d likely just come off as nitpicky, so I’ll refrain.

(aside: display.actualCenterX|Y s/b display.contentCenterX|Y)

Best as in, “I think this is really good and will be useful to others.”

 i could offer some thoughts on reducing-complexity improving-readability

Tone aside, please post better (whichever definition you want to adopt) code - whether it produces a chain/rope/string/similar - if you have it. That’s what I’m asking for here. We’re all here to learn and improve.

what i try to do is make such code “incremental”, and avoid constructs like the 3-way if statement to figure out what type of “special” processing i do at the ends.  for example, if you have a reference to “previousBall”, initially null, then set it to the current ball at the end of each loop, then your joint creation simplifies to something like (pseudocode)

for i=1,N do   currentBall = display.newCircle(...   physics.addBody(currentBall... if (previousBall) then   physics.addJoint("rope", previousBall, currentBall) end   previousBall = currentBall end

and, not sure that it directly applies, but a soft-body lattice is very much like chains, here’s an older (just uploaded, but circa 2015) tech demo, maybe just for “inspiration” or such - to whatever extent it seems relevant

[youtube]https://www.youtube.com/watch?v=ZUxh8qB3XLM[/youtube]

“best” in what sense?  what are you trying to accomplish?

“best” at stability / at not being stretchy?  solution depends on type of chain.  You can make valid chains out of rope, pivot or distance joints, though they represent different things.  (fe a bike’s chain is rigid, should be rectangles and pivot joints)

“best” at being stretchy (elastic) but not “exploding”?  you appear to have two joints (a pivot and rope) attached to both end balls, was that to keep the final rope joint from compressing? (if so, then you probably don’t need the rope joint)

“best” at conciseness?  i could offer some thoughts on reducing-complexity improving-readability of the presented code, but they’d likely just come off as nitpicky, so I’ll refrain.

(aside: display.actualCenterX|Y s/b display.contentCenterX|Y)

Best as in, “I think this is really good and will be useful to others.”

 i could offer some thoughts on reducing-complexity improving-readability

Tone aside, please post better (whichever definition you want to adopt) code - whether it produces a chain/rope/string/similar - if you have it. That’s what I’m asking for here. We’re all here to learn and improve.

what i try to do is make such code “incremental”, and avoid constructs like the 3-way if statement to figure out what type of “special” processing i do at the ends.  for example, if you have a reference to “previousBall”, initially null, then set it to the current ball at the end of each loop, then your joint creation simplifies to something like (pseudocode)

for i=1,N do   currentBall = display.newCircle(...   physics.addBody(currentBall... if (previousBall) then   physics.addJoint("rope", previousBall, currentBall) end   previousBall = currentBall end

and, not sure that it directly applies, but a soft-body lattice is very much like chains, here’s an older (just uploaded, but circa 2015) tech demo, maybe just for “inspiration” or such - to whatever extent it seems relevant

[youtube]https://www.youtube.com/watch?v=ZUxh8qB3XLM[/youtube]