Collision - How to check when dragging

Hi Ho folks,
                  I’m trying to check if two objects have collided when I release my finger after I’ve dragged an object around. (Essentially did I leave one object over another?)

I setup Physics.addbodies and they collide fine but I need to reference that they are still colliding or only check if they are colliding when I release my finger. (during my Drag Functions event.phase == “ended” )

I’m not sure what to check for or fire off a collision at that end phase of the drag. if that’s what I need to do.

Any help is appreciated.
Here’s a simplified code:

 local function onCollision( event ) if ( event.phase == "began" ) then -- these 2 objects are touching check print(event.target.ID .. " and " .. event.other.ID) return true end end -- touch listener function onTouch = function( event, collision) local self = event.target local phase = event.phase if "began" == phase then print("Pressed") --Drag code remove for ease. elseif "ended" == phase or "cancelled" == phase then if( --- TRY TO CHECK IF COLLISION IS OCCURING HERE --- ) then print("wow object1 and object 2 are colliding") ... Do Stuff end display.getCurrentStage():setFocus( nil ) self.isFocus = false end end end Object1:addEventListener("collision", onCollision) Object1:addEventListener( "touch", onTouch) ... and other objects

Hi,

If you just need to know if it’s colliding at the point of release, then a simple approach would be to set a flag on the colliding objects in the begin phase of the collision, and clear it on the ended phase. You then just need to check the flag when your touch phase has ended.

Something like this (Untested 'cos I’m at work)

local function onCollision( event ) if ( event.phase == "began" ) then event.target.inCollision = true event.other.inCollision = true print(event.target.ID .. " and " .. event.other.ID) else if (event.phase == "ended") then event.target.inCollision = false event.other.inCollision = false end return true end -- touch listener function local function( self, event) local phase = event.phase if "began" == phase then print("Pressed") --Drag code remove for ease. elseif "ended" == phase or "cancelled" == phase then if(self.inCollision == true) then print("wow object1 and object 2 are colliding") ... Do Stuff end end end

If you need to know which object it is colliding with, then that’s a little more complicated as you’ll need to store a reference to the colliding object. I’m not sure of any other way to find out which two objects are currently in collision from the Object itself.

Doug

Hi,

If you just need to know if it’s colliding at the point of release, then a simple approach would be to set a flag on the colliding objects in the begin phase of the collision, and clear it on the ended phase. You then just need to check the flag when your touch phase has ended.

Something like this (Untested 'cos I’m at work)

local function onCollision( event ) if ( event.phase == "began" ) then event.target.inCollision = true event.other.inCollision = true print(event.target.ID .. " and " .. event.other.ID) else if (event.phase == "ended") then event.target.inCollision = false event.other.inCollision = false end return true end -- touch listener function local function( self, event) local phase = event.phase if "began" == phase then print("Pressed") --Drag code remove for ease. elseif "ended" == phase or "cancelled" == phase then if(self.inCollision == true) then print("wow object1 and object 2 are colliding") ... Do Stuff end end end

If you need to know which object it is colliding with, then that’s a little more complicated as you’ll need to store a reference to the colliding object. I’m not sure of any other way to find out which two objects are currently in collision from the Object itself.

Doug