How to Make Drag Objects Detect Collision

I am trying to make it so that when I drag an object towards another drag object, they don’t overlap. But I also don’t want drag object two to move when I drag the first object towards it.

local function onDragTouch( event ) local d = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus(d) d.isFocus = true d.x0 = event.x - d.x d.y0 = event.y - d.y elseif d.isFocus then if "moved" == phase then d.x = event.x - d.x0 d.y = event.y - d.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) d.isFocus = false end end return true end objectOne=display.newRect(100, 100, 100, 100) physics.addBody(objectOne, "static") objectOne:addEventListener("touch", onDragTouch) objectTwo=display.newRect(100, 500, 50, 50) physics.addBody(objectTwo, "static") objectTwo:addEventListener("touch", onDragTouch)

How do I make it happen (with no gravity)? Thanks in advance.

I’ve managed to made the two draggable objects detect collision within each other by setting them as dynamic objects and also set their rotation fixed by using “object.isFixedRotation=true”. My last problem is trying to set the object being hit fixed at its X and Y position. So that when I drag objectOne to hit ObjectTwo…objectTwo won’t move, and vice versa. Any help would be great!

I’ve got this. I set the object that I didn’t want to move to static in the onDragTouch function when the event is at “began” and set it back to normal when event has “ended”. If theres a more efficient or smoother way to do this please let me know. Thanks!

Take a look at the code I posted here: http://forums.coronalabs.com/topic/43458-can-i-disablecancel-touch-if-not-on-object/?p=226840You should only need to remove the one object at a time part. Let me know if you have questions.

We did a blog post last summer on how to detect collisions without physics.  It’s a great method for dragging things around and seeing if they hit other things.

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob

I’ve managed to made the two draggable objects detect collision within each other by setting them as dynamic objects and also set their rotation fixed by using “object.isFixedRotation=true”. My last problem is trying to set the object being hit fixed at its X and Y position. So that when I drag objectOne to hit ObjectTwo…objectTwo won’t move, and vice versa. Any help would be great!

I’ve got this. I set the object that I didn’t want to move to static in the onDragTouch function when the event is at “began” and set it back to normal when event has “ended”. If theres a more efficient or smoother way to do this please let me know. Thanks!

Take a look at the code I posted here: http://forums.coronalabs.com/topic/43458-can-i-disablecancel-touch-if-not-on-object/?p=226840You should only need to remove the one object at a time part. Let me know if you have questions.

We did a blog post last summer on how to detect collisions without physics.  It’s a great method for dragging things around and seeing if they hit other things.

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob