Joint on collision?

Hi,

I’ve been trying to add a joint when a dynamic body touches a static body, i’ve put the joint in the collision but just crashes. Is there any other way of adding a joint upon collision? Because i am dragging an object and once it touches the dynamic body i want it to attach and act like a chain. fall down. Does any one know how?

Thanks. [import]uid: 23689 topic_id: 13220 reply_id: 313220[/import]

I would do it approximately like this:

[lua]local attachJoint = {}
local collider
local cSelf

local function onCollision( self, event )
collider = event.other
cSelf = self
local collisionTimer = timer.performWithDelay( 1, doCollision, 1)
end

local function doCollision()
collider.x, collider.y = cSelf.x, cSelf.y
attachJoint[#attachJoint + 1] = physics.newJoint( “pivot”, cSelf, collider, cSelf.x, cSelf.y )
end

local ground = display.newRect( 0, 320, display.contentWidth, 100 )
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )

local object1 = display.newCircle( 100, 100, 10 )
physics.addBody( object1, “dynamic” )
object1.collision = onCollision
object1:addEventListener( “collision”, object1 )

local object2 = display.newCircle( 300, 300, 20 )
physics.addBody( object2, “dynamic” )
object2.collision = onCollision
object2:addEventListener( “collision”, object2 )[/lua]

Of course you’d still need to add the dragging touch listeners to the objects so you can drag them around. [import]uid: 27119 topic_id: 13220 reply_id: 48733[/import]

Hello,

Thanks a lot! I have tried your code but not had any success. I added the dragging function and the physics engine. Do you know what the problem is? The code that you gave me was indeed to add a joint on collision, right? Thanks again.

The Updated Code:

[code]

require “physics”
physics.start()

local attachJoint = {}
local colliders

local function onCollision( self, event )
collider = event.other
cSelf = self
local collisionTimer = timer.performWithDelay( 1, doCollision, 1)
end

local function doCollision()
collider.x, collider.y = cSelf.x, cSelf.y
attachJoint[#attachJoint + 100] = physics.newJoint( “pivot”, cSelf, collider, cSelf.x, cSelf.y )
end

local ground = display.newRect( 0, 320, display.contentWidth, 100 )
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )

local object1 = display.newCircle( 100, 100, 10 )
physics.addBody( object1, “dynamic” )
object1.collision = onCollision
object1:addEventListener( “collision”, object1 )

local object2 = display.newCircle( 300, 300, 20 )
physics.addBody( object2, “dynamic” )
object2.collision = onCollision
object2:addEventListener( “collision”, object2 )

group1 = display.newGroup()

group1:insert(ground)

group1:insert(object1)

group1:insert(object2)

eventoffset = {x=0,y=0}

---------------- drag function

local function dragging (event)

local stage = display.getCurrentStage()

local target = event.target

if event.phase == “began” then – on touch

stage:setFocus(target, event.id)

eventoffset.x = event.x - target.x

eventoffset.y = event.y - target.y

group1:insert(target)

end

if event.phase == “moved” then – on move

target.x = event.x - eventoffset.x

target.y = event.y - eventoffset.y

end

if event.phase == “ended” or event.phase == “cancelled” then – on release

stage:setFocus(nil)

end

end


ground:addEventListener(“touch”, dragging)

object1:addEventListener(“touch”, dragging)

object2:addEventListener(“touch”, dragging) [import]uid: 23689 topic_id: 13220 reply_id: 48881[/import]

Are you getting an error? If so, what does the error say? Sorry, I haven’t had a chance to test the code you posted yet. [import]uid: 27119 topic_id: 13220 reply_id: 49001[/import]

I dont’t seem to have an error, when both objects colllide it doesn’t add a joint. Everything is draggable, but they keep on sleeping and waking up. If you could, could you see what the problem is? Thanks a lot!

Thanks again for your time. [import]uid: 23689 topic_id: 13220 reply_id: 49096[/import]

This should do what you’re looking for. Looks like your dragging code needs some work though.

[lua]require “physics”
physics.start()
physics.setDrawMode( “hybrid” )

local attachJoint = {}
local onCollision
local doCollision
local collider
local cSelf

local groundFilter = { categoryBits=1, maskBits=2 }
local objectFilter = { categoryBits=2, maskBits=3 }
local joFilter = { categoryBits=4, maskBits=4 }

function onCollision( self, event )
collider = event.other
cSelf = self
local collisionTimer = timer.performWithDelay( 1, doCollision, 1)
end

function doCollision()
collider.x, collider.y = cSelf.x, cSelf.y
attachJoint[#attachJoint + 1] = physics.newJoint( “pivot”, cSelf, collider, cSelf.x, cSelf.y )
collider:removeEventListener( “collision”, collider )
cSelf:removeEventListener( “cSelf”, cSelf )
end

local ground = display.newRect( 0, 320, display.contentWidth, 100 )
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3, filter=groundFilter } )

local object1 = display.newRect( 100, 100, 10, 40 )
physics.addBody( object1, “dynamic”, { friction=0.5, bounce=0.3, filter=objectFilter } )

local jointObject1 = display.newCircle( object1.x, (object1.y - 20), 10 )
physics.addBody( jointObject1, “dynamic”, { friction=0.5, bounce=0.3, filter=joFilter } )
joJoint1 = physics.newJoint( “weld”, jointObject1, object1, object1.x, (object1.y - 20) )
jointObject1.collision = onCollision
jointObject1:addEventListener( “collision”, jointObject1 )

local object2 = display.newRect( 300, 100, 10, 40 )
physics.addBody( object2, “dynamic”, { friction=0.5, bounce=0.3, filter=objectFilter } )

local jointObject2 = display.newCircle( object2.x, (object2.y - 20), 10 )
physics.addBody( jointObject2, “dynamic”, { friction=0.5, bounce=0.3, filter=joFilter } )
joJoint2 = physics.newJoint( “weld”, jointObject2, object2, object2.x, (object2.y - 20) )
jointObject2.collision = onCollision
jointObject2:addEventListener( “collision”, jointObject2 )

group1 = display.newGroup()

group1:insert(ground)

group1:insert(object1)

group1:insert(object2)

eventoffset = {x=0,y=0}

---------------- drag function

local function dragging (event)

local stage = display.getCurrentStage()

local target = event.target

if event.phase == “began” then – on touch

stage:setFocus(target, event.id)

eventoffset.x = event.x - target.x

eventoffset.y = event.y - target.y

group1:insert(target)

end

if event.phase == “moved” then – on move

target.x = event.x - eventoffset.x

target.y = event.y - eventoffset.y

end

if event.phase == “ended” or event.phase == “cancelled” then – on release

stage:setFocus(nil)

end

end


ground:addEventListener(“touch”, dragging)

object1:addEventListener(“touch”, dragging)

object2:addEventListener(“touch”, dragging)[/lua] [import]uid: 27119 topic_id: 13220 reply_id: 49150[/import]

Thanks so much! It works! But, do you think it’s the dragging code thats making the object push down? Or is it because it’s dynamic? [import]uid: 23689 topic_id: 13220 reply_id: 49159[/import]

The dragging code is definitely the problem. You should take a look at these templates:

Drag Me
http://developer.anscamobile.com/sites/default/files/DragMe.zip

Drag Me Multitouch
http://developer.anscamobile.com/sites/default/files/DragMeMultitouch.zip

Both are also available in the Sample Code folder after you install Corona. [import]uid: 27119 topic_id: 13220 reply_id: 49230[/import]

Thanks! It worked! [import]uid: 23689 topic_id: 13220 reply_id: 49678[/import]