Distance Joint

I am trying to connect 2 objects (circles) with a distance joint (like a rope connecting two rocks).

The objective is to have each object connected to a joint (rope or string) that the user can interact with such as throwing across the screen.

My problem is I can get one object (circle) to work just fine, but when I try to add a second object and a joint between them my second circle appears on the screen then falls and disappears.

I used the TimeAnimation example to get me this far but can’t figure out what I am doing wrong.

Any suggestions?

Here is what I have so far:

local ball = display.newCircle( 0, 0, 10 ) – ball size
ball:setFillColor(255, 255, 255, 255 ) – ball color
ball.x = 25
ball.y = 275

local ball2 = display.newCircle( 0, 0, 10 ) – ball size
ball:setFillColor(255, 255, 255, 255 )
ball2.x = 50
ball2.y = 275
function onMoveCircle( event )
local timePassed = event.time - lastTime
lastTime = lastTime + timePassed

speedY = speedY + gravity

ball.x = ball.x + speedX*timePassed
ball.y = ball.y + speedY*timePassed

ball2.x = ball2.x + speedX*timePassed
ball2.y = ball2.y + speedY*timePassed

if ball.x >= screenW - ball.width*0.5 then
ball.x = screenW - ball.width*0.5
speedX = speedX*friction
speedX = speedX*-1 – change direction
elseif ball.x <= ball.width*0.5 then
ball.x = ball.width*0.5
speedX = speedX*friction
speedX = speedX*-1 – change direction
elseif ball.y >= screenH - ball.height*0.5 then
ball.y = screenH - ball.height*0.5
speedY = speedY*friction
speedX = speedX*friction
speedY = speedY*-1 – change direction
elseif ball.y <= ball.height*0.5 then
ball.y = ball.height*0.5
speedY = speedY*friction
speedY = speedY*-1 – change direction

myJoint = physics.newJoint( “distance”, ball, ball2, ball.x,ball.y, ball2.x,ball2.y )
myJoint.length = 2

end
end

– A general function for dragging objects
local function startDrag( 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

– Stop current motion if any
Runtime:removeEventListener( “enterFrame”, onMoveCircle )
– start tracking velocity
Runtime:addEventListener( “enterFrame”, trackVelocity )

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
lastTime = event.time

Runtime:removeEventListener( “enterFrame”, trackVelocity )
Runtime:addEventListener( “enterFrame”, onMoveCircle )

display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

– Stop further propagation of touch event
return true
end

function trackVelocity( event )
local timePassed = event.time - prevTime
prevTime = prevTime + timePassed

speedX = (ball.x - prevX)/timePassed
speedY = (ball.y - prevY)/timePassed

prevX = ball.x
prevY = ball.y

end

ball:addEventListener( “touch”, startDrag )
Runtime:addEventListener( “enterFrame”, onMoveCircle )
[import]uid: 21265 topic_id: 7332 reply_id: 307332[/import]

Be a lot easier to read if you used the code tag. [import]uid: 3953 topic_id: 7332 reply_id: 26076[/import]

Thanks, sorry! Took a minute to figure it out.

[code]function onMoveCircle( event )
local timePassed = event.time - lastTime
lastTime = lastTime + timePassed

speedY = speedY + gravity

ball.x = ball.x + speedX*timePassed
ball.y = ball.y + speedY*timePassed

ball2.x = ball2.x + speedX*timePassed
ball2.y = ball2.y + speedY*timePassed

if ball.x >= screenW - ball.width*0.5 then
ball.x = screenW - ball.width*0.5
speedX = speedX*friction
speedX = speedX*-1 – change direction
elseif ball.x <= ball.width*0.5 then
ball.x = ball.width*0.5
speedX = speedX*friction
speedX = speedX*-1 – change direction
elseif ball.y >= screenH - ball.height*0.5 then
ball.y = screenH - ball.height*0.5
speedY = speedY*friction
speedX = speedX*friction
speedY = speedY*-1 – change direction
elseif ball.y <= ball.height*0.5 then
ball.y = ball.height*0.5
speedY = speedY*friction
speedY = speedY*-1 – change direction

myJoint = physics.newJoint( “distance”, ball, ball2, ball.x,ball.y, ball2.x,ball2.y )
myJoint.length = 2

end
end

– A general function for dragging objects
local function startDrag( 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

– Stop current motion if any
Runtime:removeEventListener( “enterFrame”, onMoveCircle )
– start tracking velocity
Runtime:addEventListener( “enterFrame”, trackVelocity )

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
lastTime = event.time

Runtime:removeEventListener( “enterFrame”, trackVelocity )
Runtime:addEventListener( “enterFrame”, onMoveCircle )

display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

– Stop further propagation of touch event
return true
end

function trackVelocity( event )
local timePassed = event.time - prevTime
prevTime = prevTime + timePassed

speedX = (ball.x - prevX)/timePassed
speedY = (ball.y - prevY)/timePassed

prevX = ball.x
prevY = ball.y

end

ball:addEventListener( “touch”, startDrag )
Runtime:addEventListener( “enterFrame”, onMoveCircle )
[/code] [import]uid: 21265 topic_id: 7332 reply_id: 26196[/import]