How to find out if 2 particular objects are touching or overlapping?

I thought this would be relatively easy to do but I’ve spent hours trying to get this to work and still can’t find a solution!

Basically I am creating a game that has a bomb, the bomb is on a timer and will explode after 8 seconds. The bomb moves around the screen and when it explodes I need to know what object (if any) it is touching and or overlapping which will then allow me to fire a function to explode the particular object in question.

I’ve tried lots of different things and the closest I have got to any sort of feedback is using event.contact.isTouching although this will of course only provide true or false but not the object my bomb is actually touching or overlapping.

Any help would be much appreciated,I honestly thought this would be easy!

[import]uid: 107915 topic_id: 34874 reply_id: 334874[/import]

Hi Chris,

Personally i would loop through the object group and run checks to see if there x, y is close enough to the bomb for them to be blown up. Something like the following:

[code]
local i
for i=objectGroup.numChildren, 1, -1 do
local object = objectGroup[i]

–Work out how far away the object is.
local xDif = bomb.x - object.x
local yDif = bomb.y - object.y
local distance = math.abs(xDif) + math.abs(yDif);

–Remove the object as its close enough.
if distance <= bomb.width/2 then
display.remove(objectGroup[i]); objectGroup[i]=nil
end
end
[/code] [import]uid: 69826 topic_id: 34874 reply_id: 138575[/import]

ps. That is completely untested, but it made sense when i wrote it :slight_smile: [import]uid: 69826 topic_id: 34874 reply_id: 138579[/import]

This post covers both methods for rectangles overlapping and circles with radius’s overlapping:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

I tried to find the “Is a point in side a box” method which is also useful for this. There are also methods for testing if two polygons are intersecting, though I never got that to work. Usually if I need to worry about polygons, its time to use the physics engine.

[import]uid: 199310 topic_id: 34874 reply_id: 138604[/import]

Firstly thanks to TandG for your reply, I played around with your idea for a couple of hours and although it was much closer to anything else I had managed before (!) I couldn’t quite get the detection as fine tuned as I might like.

Rob, thanks for the link, I managed to make some changes to the example code and now have it working to my liking.

That was hard work! For some reason I thought there would be a much easier way to achieve that, surprises me that Corona haven’t implemented something simpler…oh well done now…thanks guys. [import]uid: 107915 topic_id: 34874 reply_id: 138615[/import]

Corona SDK’s method of dealing with collisions is based around the physics engine but there are times where the physics engine gets in the way or is overkill to know if two blocks are overlapping (like a drag and drop action) and those two methods are the easiest ones to work with from my experience.
[import]uid: 199310 topic_id: 34874 reply_id: 138620[/import]

Hi Chris,

Personally i would loop through the object group and run checks to see if there x, y is close enough to the bomb for them to be blown up. Something like the following:

[code]
local i
for i=objectGroup.numChildren, 1, -1 do
local object = objectGroup[i]

–Work out how far away the object is.
local xDif = bomb.x - object.x
local yDif = bomb.y - object.y
local distance = math.abs(xDif) + math.abs(yDif);

–Remove the object as its close enough.
if distance <= bomb.width/2 then
display.remove(objectGroup[i]); objectGroup[i]=nil
end
end
[/code] [import]uid: 69826 topic_id: 34874 reply_id: 138575[/import]

ps. That is completely untested, but it made sense when i wrote it :slight_smile: [import]uid: 69826 topic_id: 34874 reply_id: 138579[/import]

This post covers both methods for rectangles overlapping and circles with radius’s overlapping:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

I tried to find the “Is a point in side a box” method which is also useful for this. There are also methods for testing if two polygons are intersecting, though I never got that to work. Usually if I need to worry about polygons, its time to use the physics engine.

[import]uid: 199310 topic_id: 34874 reply_id: 138604[/import]

Firstly thanks to TandG for your reply, I played around with your idea for a couple of hours and although it was much closer to anything else I had managed before (!) I couldn’t quite get the detection as fine tuned as I might like.

Rob, thanks for the link, I managed to make some changes to the example code and now have it working to my liking.

That was hard work! For some reason I thought there would be a much easier way to achieve that, surprises me that Corona haven’t implemented something simpler…oh well done now…thanks guys. [import]uid: 107915 topic_id: 34874 reply_id: 138615[/import]

Corona SDK’s method of dealing with collisions is based around the physics engine but there are times where the physics engine gets in the way or is overkill to know if two blocks are overlapping (like a drag and drop action) and those two methods are the easiest ones to work with from my experience.
[import]uid: 199310 topic_id: 34874 reply_id: 138620[/import]

Rob Miracle’s link is great, but here’s another one if you want it:

http://developer.coronalabs.com/code/simple-shape-collision-routines [import]uid: 147322 topic_id: 34874 reply_id: 139455[/import]

Thanks Caleb. The point in shape functions are very helpful and were ones I was trying to find because I had used something similar before. It’s nice to have all of that in one library. Now if someone could figure out the polygon overlapping we would have a near-complete non-physics collision library. [import]uid: 199310 topic_id: 34874 reply_id: 139461[/import]

There’s a “pointInPolygon” function in Horacebury’s MathLib.

http://developer.coronalabs.com/code/maths-library

As for two polygons overlapping, I’m clueless.

Caleb :slight_smile: [import]uid: 147322 topic_id: 34874 reply_id: 139521[/import]

Rob Miracle’s link is great, but here’s another one if you want it:

http://developer.coronalabs.com/code/simple-shape-collision-routines [import]uid: 147322 topic_id: 34874 reply_id: 139455[/import]

Thanks Caleb. The point in shape functions are very helpful and were ones I was trying to find because I had used something similar before. It’s nice to have all of that in one library. Now if someone could figure out the polygon overlapping we would have a near-complete non-physics collision library. [import]uid: 199310 topic_id: 34874 reply_id: 139461[/import]

There’s a “pointInPolygon” function in Horacebury’s MathLib.

http://developer.coronalabs.com/code/maths-library

As for two polygons overlapping, I’m clueless.

Caleb :slight_smile: [import]uid: 147322 topic_id: 34874 reply_id: 139521[/import]