Animating adding joints

I have the following code which I have adapted from the a couple of the tutorials.

What I am trying to achieve is adding joints based on a Y axis movement. Dependent on its direction increase or decrease the length of the rope. I get this to work but the joints just appear. What techniques could I employ to make it look like the rope is moving down (increasing in size) or moving up (decreasing) in size?

Is it just a case of decreasing the size of the image and adding more smaller joints in but concerned amount memory management of optimisation

[lua]local physics = require(“physics”)
– Load The Remote
local remote = require(“remote”)

– Start The Remote On Port 8080
remote.startServer( “8080” )

physics.start()

physics.setScale(60)

display.setStatusBar(display.HiddenStatusBar)

local ceiling = display.newRect (0, 0, display.contentWidth, 1)

physics.addBody( ceiling, “static”, { density=0, friction=0.5,bounce=0.2 } )

local prevBody = ceiling
local xCenter = 160
local wCeil = 120
local hCeil = -5
local totalYMovement = 0

local w,h = 10,10
local halfW,halfH = 0.5*w,0.5*h

– center of body
local x = xCenter
local y = hCeil - halfH
local yJoint = y - halfH

for i = 1, 20 do
y = y + h
yJoint = yJoint + h

local body = display.newImage(“rope.png” ,x-halfW, y-halfH) --) display.newRect( x-halfW, y-halfH, w, h )

–body:setFillColor( 128, 0, 0 )
physics.addBody( body, { density=15, friction=0.5, bounce = .2 })
local joint = physics.newJoint( “pivot”, prevBody, body, xCenter, yJoint )

prevBody = body
end
– Get The Latest Accelerometer Values
local function updateAccelerometer()

– This Runtime Listener Is An Example Of How To
– Access The Remote You Can Query remote.xGravity
– Or Any Other Value From Anywhere In Your Application

local xGravity = remote.xGravity
local yGravity = remote.yGravity

totalYMovement = totalYMovement + yGravity
if(totalYMovement > 17) then
print(“Add link”)
totalYMovement = 0

y = y + h
yJoint = yJoint + h

local body = display.newImage(“rope.png” ,x-halfW, y-halfH) --) display.newRect( x-halfW, y-halfH, w, h )

–body:setFillColor( 128, 0, 0 )
physics.addBody( body, { density=15, friction=0.5, bounce = .2 })
local joint = physics.newJoint( “pivot”, prevBody, body, xCenter, yJoint )

prevBody = body

elseif(totalYMovement < -17) then
print(“Remove link”)
totalYMovement = 0
end

print ("TotalYMovement: " … totalYMovement … " yGravity: " … yGravity)

end

–Runtime:addEventListener(“accelerometer”, acc)

– Add Enter Frame Listener
Runtime:addEventListener( “enterFrame” , updateAccelerometer )[/lua] [import]uid: 103970 topic_id: 17957 reply_id: 317957[/import]

You could transition in the extra joints perhaps, like start there alpha at 0, and when adding have a transition that increases the alpha to 1.

Or

You could do the same as i said above except leave the alpha at 1, and start the x, y scale of the new joint at 0, and scale it up to 1 using a transition…

Is any of that advice helpful ?
[import]uid: 84637 topic_id: 17957 reply_id: 68773[/import]

Thanks Danny. I have gone for your second approach but I think I may have not implemented it correctly. I have gone for a transition on height as I don’t want the width to scale only the height so it looks like the rope is extending / shortening.

e.g. transition.to(link[ropeCount], {time=1000, height=18})

The following kind of works other than the transition happens from the middle of the image rather than the top down or bottom up.
[lua]local physics = require(“physics”)
– Load The Remote
local remote = require(“remote”)

– Start The Remote On Port 8080
remote.startServer( “8080” )

physics.start()

physics.setScale(60)

display.setStatusBar(display.HiddenStatusBar)
local beam = display.newImage(“beam_long.png”)
beam.x = 50
beam.y = 50
physics.addBody(beam, “static”, {fricition=0.5})
totalYMovement = 0
local ropeCount = 10
local currentHeight = 0.0000001
local ropeHeight = 18
local myJoints = {}
local acc = {}
local centerX = display.contentWidth * 0.5

–for i = 1,5 do
local link = {}
local i = 1
for j = 1,20 do

link[j] = display.newImage(“rope.png”)
if(j > 10) then
link[j].height = currentHeight
end

link[j]:setReferencePoint(display.CenterReferencePoint)
link[j].x = display.contentWidth * 0.5 + (i*34)
link[j].y = 50+ (j*17)

physics.addBody(link[j], “dynamic”, {density=2.0, friction=0, bounce=0 })

– create joints between links
if(j > 1) then
prevLink = link[j-1] – each link is joined with the one above it
else
prevLink = beam
end
myJoints[#myJoints + 1] = physics.newJoint(“pivot”, prevLink, link[j], display.contentWidth * 0.5 + (i*34), 46 + (j*17))
end

– Get The Latest Accelerometer Values
local function updateAccelerometer()

– This Runtime Listener Is An Example Of How To
– Access The Remote You Can Query remote.xGravity
– Or Any Other Value From Anywhere In Your Application

local xGravity = remote.xGravity
local yGravity = remote.yGravity

totalYMovement = totalYMovement + yGravity
if(totalYMovement > 5) then
if(ropeCount >= 10) then
print(“Add link”)
ropeCount = ropeCount + 1
totalYMovement = 0
transition.to(link[ropeCount], {time=1000, height=18})
end
elseif(totalYMovement < -5) then
if(ropeCount > 10) then

transition.to(link[ropeCount], {time=1000, height=0.0000001})
ropeCount = ropeCount - 1
totalYMovement = 0
end
end

print("ropeCount: "…ropeCount)
end

–Runtime:addEventListener(“accelerometer”, acc)

– Add Enter Frame Listener
Runtime:addEventListener( “enterFrame” , updateAccelerometer )[/lua]

Did I get the wrong idea?

[import]uid: 103970 topic_id: 17957 reply_id: 68803[/import]

I’m having further challenges with this in that the yScale seems to have no effect. Of if I reduce the initial scale to too little and apply this to the highest joints the joints effectively “snap off”.

If I have the alpha as 0 they still remain in place so just hidden so if, for example, you applied this logic to your bridge sample then half the bridge wouldn’t be visible which isn’t appropriate.

Any other ideas? [import]uid: 103970 topic_id: 17957 reply_id: 70026[/import]