on collision two object became stick

Hello, i have this code here

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local physics = require("physics") physics.start() function scene:createScene( event )     local group = self.view     local rect = display.newRect( 50, 50, 100, 150 )  -- left, top, w, h     rect.x = 150     rect.y = 350     rect.name = "rotate"     rect:setFillColor( 255,255,0 )     physics.addBody(rect, "static", {density = 1.0, friction = 0.3, bounce = 0.2})     group:insert(rect)     local rect2 = display.newRect( 150, 150, 150, 150)  -- left, top, w, h     rect2.x = 150     rect2.y = 150     rect2:setFillColor( 255,255,255 )     rect2.name = "falloff"     physics.addBody(rect2, "dynamic", {density = 1.0, friction = 0.3, bounce = 0.2})     group:insert(rect2)     function onCollision( event )         if(event.object1.name == "rotate" and event.object2.name == "falloff") then                         physics.newJoint( "weld", rect2, rect, rect2.x,rect2.y )         elseif(event.object1.name == "falloff" and event.object2.name == "rotate") then                         physics.newJoint( "weld", rect2, rect, rect2.x,rect2.y )         end     end     function rollingz(event)     rect.rotation = rect.rotation + 1     end end function scene:enterScene( event )     Runtime:addEventListener("enterFrame", rollingz)     Runtime:addEventListener( "collision", onCollision ) end function scene:exitScene( event ) end function scene:destroyScene( event ) end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene  

I want when that rect and rect2 in collision the rect2 stick to rect and follow the rotation of rect and when I tap again that rect2 is unstick and jumping

I read this thread http://forums.coronalabs.com/topic/3166-making-physics-objects-stick-to-one-another/ but still dont understand

 

Hi @bowodesign,

A few comments to help you along with this…

  1. You’ll need some reference to the weld joint you create, so later you can remove (destroy) that joint, thus releasing the object from its sticky-attach state. It’s probably best if you just assign the joint as a property of the item being attached, like:

[lua]

rect2.attachJoint = physics.newJoint( “weld”, … )

[/lua]

Then later, destroy it by doing:

[lua]

rect2.attachJoint:removeSelf()

rect2.attachJoint = nil

[/lua]

  1. You should “filter” the collision handler to only accept the “began” event phase, so there’s no chance of triggering a collision event on the “ended” phase and installing a new weld joint that’s not intended.

  2. You may want to make “rect” a dynamic object with very high density, and rotate it using the application of angular velocity. Just something to consider.

Hope this helps,

Brent Sorrentino

Hi Brent

Thank you for the reply I will try to implement it :slight_smile:

Hi @bowodesign,

A few comments to help you along with this…

  1. You’ll need some reference to the weld joint you create, so later you can remove (destroy) that joint, thus releasing the object from its sticky-attach state. It’s probably best if you just assign the joint as a property of the item being attached, like:

[lua]

rect2.attachJoint = physics.newJoint( “weld”, … )

[/lua]

Then later, destroy it by doing:

[lua]

rect2.attachJoint:removeSelf()

rect2.attachJoint = nil

[/lua]

  1. You should “filter” the collision handler to only accept the “began” event phase, so there’s no chance of triggering a collision event on the “ended” phase and installing a new weld joint that’s not intended.

  2. You may want to make “rect” a dynamic object with very high density, and rotate it using the application of angular velocity. Just something to consider.

Hope this helps,

Brent Sorrentino

Hi Brent

Thank you for the reply I will try to implement it :slight_smile: