Drag sample doesn't work together with Physic collision sample. Anyone work on this before?

hi gurus,

i am planing to create two button (top, down), and if i hold & drag the top button to bottom button, the top button’s x, y will be the same as the bottom button when they ‘collide’
it’s try to simulate the RPG inventory, when we drag an item to a slot, it should automatically snap on the slot.

but i found that the ‘collide’ event doesn’t trigger bcoz i am using a body type ‘static’ :frowning:
Can anyone modify my code to make it work? i believe it should be a small tweak :slight_smile:
sample code below

local physics = require("physics")  
physics.start()  
  
local button1 = display.newImage('buttonbg.png')  
button1.x, button1.y = 100, 100  
local button2 = display.newImage('buttonbg.png')  
button2.x, button2.y = 100, 200  
physics.addBody( button1, "static", { density=3.0, friction=0.5, bounce=0.3 } )  
physics.addBody( button2, "static", { density=3.0, friction=0.5, bounce=0.3 } )  
  
-- ADD COLLISION EVENT  
local function onDragCollision(self, event)  
 print('coll function')  
 if ( event.phase == "began" ) then  
 print('collision began')  
  
  
 self.bodyType = 'static'  
  
 elseif ( event.phase == "ended" ) then  
 print('collision ended')  
  
  
 end  
end  
button1.collision = onDragCollision  
button1:addEventListener( "collision", button1 )  
  
-- ADD DRAG EVENT  
local function startDrag(e)  
 local t = e.target  
  
 local phase = e.phase  
  
 if phase == 'began' then  
  
 t.x0, t.y0 = e.x - t.x, e.y - t.y  
  
 display.getCurrentStage():setFocus(t)  
 t.isFocus = true  
  
 elseif t.isFocus then  
 if phase == 'moved' then  
  
 --  
  
  
 t.x, t.y = e.x - t.x0, e.y - t.y0  
 elseif phase == 'ended' then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus = false  
  
 end  
 end  
end  
button1:addEventListener('touch', startDrag)  
button2:addEventListener('touch', startDrag)  
  

Thanks [import]uid: 10373 topic_id: 17679 reply_id: 317679[/import]

it is not working because all the bodies are static which cant collide with each other only dynamic body can collide with all type of body static and kinematic body cant collide with each other
:slight_smile: [import]uid: 12482 topic_id: 17679 reply_id: 67367[/import]