How to use a physics joint inside a collison event function?

I am trying to merge two bodies together but it says that I cant use a physics.joint() inside a collision event. Any help?

Naffah

Create the joint outside of the collision event. So create a timer in the collision event that adds the joint between the objects.

The workaround for this, as well as many other physics gotchas, are described here: http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Okay I am trying but its not working. What changes do I need to make in my code? Here’s my code: 

local function myJoint() physics.newJoint( "weld", obj1, obj2, x, y ) end local function onCollision( event ) physics.setAverageCollisionPositions( true ) physics.setReportCollisionsInContentCoordinates( true ) local obj1 = event.target.id local obj2 = event.other.id if event.target.type ~= "static" and event.other.type ~= "static" then if ( event.phase == "began" ) then transition.to( event.target.id, {time = 10, xScale=1.2, yScale=1.2, alpha=0} ) transition.to( event.other.id, {time = 10, xScale=1.2, yScale=1.2, alpha=0} ) timer.performWithDelay( 100, myJoint(obj1, obj2, event.x, event.y) ) elseif ( event.phase == "ended" ) then print("phase ended") score = score + 25 scoreText.text = "Score: " .. score end end return true end

You could try with following changes:

[lua]

local function myJoint(obj1, obj2, x, y)
    physics.newJoint( “weld”, obj1, obj2, x, y )
end

local function onCollision( event )
    print(“collision”)
    physics.setAverageCollisionPositions( true )
    physics.setReportCollisionsInContentCoordinates( true )
    local obj1 = event.target
    local obj2 = event.other
    if event.target.type ~= “static” and event.other.type ~= “static” then
        if ( event.phase == “began” ) then
            transition.to( event.target, {time = 10000, xScale=1.2, yScale=1.2, alpha=0} )
            transition.to( event.other, {time = 10000, xScale=1.2, yScale=1.2, alpha=0} )
            timer.performWithDelay( 1, function() myJoint(obj1, obj2, event.x, event.y) end )
        elseif ( event.phase == “ended” ) then
            print(“phase ended”)
            
        end
    end
    return true
end
[/lua]

I tried the code you wrote above but it again gives me the same error. Here’s the picture of corona simulator:  23l15r6.png

Are you sure you’re running the code Sape posted? I don’t see the print(“collision”) or print(“phase ended”) being output anywhere.

@ memo 

@ Sape

Okay I tried the code again and it worked! I must have made a mistake the first time  :smiley: Thanks guys!!

Create the joint outside of the collision event. So create a timer in the collision event that adds the joint between the objects.

The workaround for this, as well as many other physics gotchas, are described here: http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Okay I am trying but its not working. What changes do I need to make in my code? Here’s my code: 

local function myJoint() physics.newJoint( "weld", obj1, obj2, x, y ) end local function onCollision( event ) physics.setAverageCollisionPositions( true ) physics.setReportCollisionsInContentCoordinates( true ) local obj1 = event.target.id local obj2 = event.other.id if event.target.type ~= "static" and event.other.type ~= "static" then if ( event.phase == "began" ) then transition.to( event.target.id, {time = 10, xScale=1.2, yScale=1.2, alpha=0} ) transition.to( event.other.id, {time = 10, xScale=1.2, yScale=1.2, alpha=0} ) timer.performWithDelay( 100, myJoint(obj1, obj2, event.x, event.y) ) elseif ( event.phase == "ended" ) then print("phase ended") score = score + 25 scoreText.text = "Score: " .. score end end return true end

You could try with following changes:

[lua]

local function myJoint(obj1, obj2, x, y)
    physics.newJoint( “weld”, obj1, obj2, x, y )
end

local function onCollision( event )
    print(“collision”)
    physics.setAverageCollisionPositions( true )
    physics.setReportCollisionsInContentCoordinates( true )
    local obj1 = event.target
    local obj2 = event.other
    if event.target.type ~= “static” and event.other.type ~= “static” then
        if ( event.phase == “began” ) then
            transition.to( event.target, {time = 10000, xScale=1.2, yScale=1.2, alpha=0} )
            transition.to( event.other, {time = 10000, xScale=1.2, yScale=1.2, alpha=0} )
            timer.performWithDelay( 1, function() myJoint(obj1, obj2, event.x, event.y) end )
        elseif ( event.phase == “ended” ) then
            print(“phase ended”)
            
        end
    end
    return true
end
[/lua]

I tried the code you wrote above but it again gives me the same error. Here’s the picture of corona simulator:  23l15r6.png

Are you sure you’re running the code Sape posted? I don’t see the print(“collision”) or print(“phase ended”) being output anywhere.

@ memo 

@ Sape

Okay I tried the code again and it worked! I must have made a mistake the first time  :smiley: Thanks guys!!