Detecting objects in zone

Hi, 
I have a bunch of objects being spawned with physics and pushed onto the screen with a linear impulse.
Here’s how my objects are being spawed

local function spawnTorpedo() i = i + 1 torpedo[i] = display.newRect( 50, 100, 100, 50 ) torpedo[i].x = 0 torpedo[i].y = display.contentCenterY + math.random( 5 ) physics.addBody( torpedo[i], "dynamic", {density=.5, friction=1 } ) torpedo[i]:applyLinearImpulse( math.random(11), math.random(-3, 3), torpedo[i].x, torpedo[i].y ) torpedo[i]:addEventListener( "touch", physTouch ) torpedo[i]:addEventListener( "collision", inZone ) end timer.performWithDelay( 2000, spawnTorpedo, 0 )

Once the object is dragged into the zone, I want it to fade or shrink as if it was dropping into a hole. Right now my function inZone is trying to detect the collision between object[i] spawned and a rectangle without physics (the zone).

I’ve been stuck on this for a while, does anyone have any hints or ideas that can help me?

Thank you in advance.

If you’ve not interested in giving the “zone” a physics body, you should use non-physics collision detection. That way you can address when your display object overlaps with the “zone”.

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

here is the code for the function inZone

function inZone(obj1, obj2) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end

It’s something I got from a collision detection tutorial

If I give it a physics body will the objects keep bouncing off of it or is there a way to keep that from happening?

Hi Antonio, You can achieve what you asked Alex about (a physics object that doesn’t bounce off other objects, or a “sensor”) by setting the object’s .isSensor flag to true: http://docs.coronalabs.com/daily/api/type/Body/isSensor.html

Thank you to both you! I ran into a small problem.  Is there a way to detect the collision even when the object isn’t being touched by the user? Would I use a runtime listener?

Physics supports something called Region Query where you can get information about objects in an area.  There is a sample app that demonstrates this in the Physics folder in the SampleCode folder. 

Non-physics collision detection can work with physics.  It’s just doing math to compare two objects and see if their bounds overlap.  You don’t get custom shapes, just rectangles and circles which can be “close enough” in many cases.  Usually you would check while moving your object such as dragging or in a Runtime enterFrame listener if it’s moving on its own (transition, physics, etc.)

Of course if you’re using physics, the isSensor method works too.

Rob

If you’ve not interested in giving the “zone” a physics body, you should use non-physics collision detection. That way you can address when your display object overlaps with the “zone”.

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

here is the code for the function inZone

function inZone(obj1, obj2) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end

It’s something I got from a collision detection tutorial

If I give it a physics body will the objects keep bouncing off of it or is there a way to keep that from happening?

Hi Antonio, You can achieve what you asked Alex about (a physics object that doesn’t bounce off other objects, or a “sensor”) by setting the object’s .isSensor flag to true: http://docs.coronalabs.com/daily/api/type/Body/isSensor.html

Thank you to both you! I ran into a small problem.  Is there a way to detect the collision even when the object isn’t being touched by the user? Would I use a runtime listener?

Physics supports something called Region Query where you can get information about objects in an area.  There is a sample app that demonstrates this in the Physics folder in the SampleCode folder. 

Non-physics collision detection can work with physics.  It’s just doing math to compare two objects and see if their bounds overlap.  You don’t get custom shapes, just rectangles and circles which can be “close enough” in many cases.  Usually you would check while moving your object such as dragging or in a Runtime enterFrame listener if it’s moving on its own (transition, physics, etc.)

Of course if you’re using physics, the isSensor method works too.

Rob