Sorry, I was apprehensive to posting my code because it’s a mess. Here, I’ll try cleaning it up it isolate the specific problem I’m having.
Here I create the balloon and chain:
[lua]local function make_balloon( x, y, lw, lh, c )
--table to keep track of the chain links
local prevs = {}
--image of the balloon
balloon = display.newImage(“red_balloon_small.png”)
physics.addBody(balloon, “dynamic”, {density=100, bounce=0, friction = 1})
balloon.x, balloon.y = x, y
balloon.dampingRatio = 1
balloon.isSensor = true
balloon.gravityScale = 0
--“top of the balloon” effected by gravity. Pivot jointed to the top of the balloon image.
local balloonTop = display.newRect( x, y - balloon.contentHeight/2, lw, lh )
physics.addBody(balloonTop, “dynamic”, {density=100, bounce=0, friction = 1})
balloonTop:setFillColor(0,0,0)
local linker = physics.newJoint(“pivot”, balloonTop, balloon, balloon.x, balloon.y - balloon.contentHeight/2)
balloonTop.gravityScale = 100
balloonTop.alpha = 0
--“bottom of the balloon” not effected by gravity. Pivot jointed to the base of the balloon image.
local balloonBottom = display.newRect( x, y + balloon.contentHeight/2, lw, lh )
physics.addBody(balloonBottom, “dynamic”, {density=100, bounce=0, friction = 1})
balloonBottom:setFillColor(0,0,0)
local linker = physics.newJoint(“pivot”, balloonBottom, balloon, balloon.x, balloon.y + balloon.contentHeight/2)
balloonBottom.gravityScale = 0
--the handle at the base of the chain. Can be touched and dragged
handle = display.newCircle(0,0,25)
handle:setFillColor(1,0.2,0.4)
physics.addBody(handle,“static”, {isSensor = true})
handle.x,handle.y = x, y + ((i)*lh) + 15
handle:addEventListener( “touch”, handleDrag )
--the chain starts at the base of the balloon
prevs[0] = balloonBottom
--creates “links” of the chain
for i = 1, c do
local link = display.newRect( x, y+(i*(lh)), lw, lh )
link:setFillColor(0,0,0)
link.alpha = 0
link.isSensor = true
physics.addBody( link, “dynamic”,{ density=100.0} )
link.dampingRatio = 1
--a series of rope joints. the current link connects to the previous one
local linker = physics.newJoint(“rope”, link, prevs[i-1], 0, 0, 0, 0)
linker.maxLength = link.y - prevs[i-1].y
prevs[i] = link
end
--connects the last link to top of the handle
local linker = physics.newJoint(“rope”, handle, prevs[c], 0, -handle.contentHeight/2, 0, 0)
linker.maxLength = handle.y - prevs[c].y
--links handle to balloon. Even this rope joint overstretches.
local linkerB = physics.newJoint(“rope”, handle, balloonBottom, 0, -handle.contentHeight/2, 0, 0)
linkerB.maxLength = handle.y - balloon.y + 2*c
end
make_balloon(w/2, h/4, 5, 5, 30)
[/lua]
The balloon is affected by upwards gravity. When I pull the handle down, not all of the chain links follow. Specifically it’s the closest rope joint. This happens consistently. I’ve switched the drag-object to other links, and it’s always the closest rope joint that seems to overstretch. Here is the code for the drag-function:
[lua]
local function handleDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”
– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
t.bodyType = “static”
end
end
– Stop further propagation of touch event!
return true
end
[/lua]
The code I’ve presented here results in this effect:
If I don’t drag anything (balloon chain seems normal) - http://puu.sh/izflu/cd8dbb5195.jpg
Once I drag on the handle (balloon chain overstretches) - http://puu.sh/izfpN/2add7b0170.jpg
Obviously, that overstretching is not the intention. I’d like the rope joints to follow their max lengths (which I’ve defined to be no greater than the initial distance between the two objects it’s connecting), so that I have a chain that behaves like a balloon-string.
Thank you for taking the time to look at this.