collision detection with images of same color

Is there a way to do a collision detection on images that have been spawned randomly?
I would like to have my random images disappear if another image of same color/type are touching it.
Example:
spawn multiple images (diamond-red.png, diamond-blue.png, diamond-green.png).

If 2 images of same color/type collide then both of them disappear.

I already have the random images and I can manipulate them just don’t know how to have them detect each other.

thanks [import]uid: 127842 topic_id: 22404 reply_id: 322404[/import]

You can’t do this via pixel detection (at present) but you can do it by assigning your objects a color definition.

For instance : (working example)

[code]

–Create a color table, handy for checking colors whilst still being able to use a string identifier for readability, whilst the actual comarison is still an integer check which is a lot faster
local colors = {
[“white”] = 1
}

local img1 = display.newRect(100, 100, 50, 50)
img1.color = colors[“white”]

local img2= display.newRect(200, 100, 50, 50)
img2.color = colors[“white”]

–Then a basic box collision check…
local function hitTestObjects(obj1, obj2)
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

local doTest = true

local function test(event)
–Check if the objects are colliding and are the same color
if doTest == true then
if hitTestObjects(img1, img2) == true then
if img1.color == img2.color then
print(“collided with same color object”)
–Remove the images
display.remove(img1)
display.remove(img2)
doTest = false
end
else
img2:translate(-1, 0)
end
end

return true
end

Runtime:addEventListener(“enterFrame”, test)
[/code] [import]uid: 84637 topic_id: 22404 reply_id: 89310[/import]

Hey danny! Thanks for the help last time.
I don’t understand where you are getting the color collision from.

I can change:
local colors = {[“white”] = 1}
to any color I want but it still shows white rectangles.

I can also change these to different colors and still white
img1.color = colors[“white”]
img2.color = colors[“white”]

I can’t find out how you can change the color of the 2 objects.

Let’s say you have 3 objects white, red, green
How do I make 3 individual rectangles and if 2 of them are the same color then eliminate them. Does that make sense?

I guess if I knew how to change the white rectangles to different colors that would be helpful [import]uid: 127842 topic_id: 22404 reply_id: 89319[/import]

Yes that is just used as a reference. You said you already had green, blue and red images or whatever.

So you would assign them

redImage.color = colors[“red”]

and so on.

The example was just an example.

To change the colors in the example you would need to do :

img1:setFillColor(colorHere) [import]uid: 84637 topic_id: 22404 reply_id: 89320[/import]

I’m kinda getting it now… [import]uid: 127842 topic_id: 22404 reply_id: 89321[/import]

Danny, I’m trying to use the above steps to identify collisions with a randomly generated level. I’ve tested this with normal images that are in a static level, but random barriers are really giving me trouble. Here is my level code:

  
local function buildGrid()  
 -- build map array --  
 for x = 0, kLevelCols do  
 level[x] = {}  
 for y = 0, kLevelRows do  
 local probability = math.random(0,10)  
 if probability \<= kRoadProbability then  
 level[x][y] = 1  
 cell2 = display.newImage(cellTable2, x\*cellWidth, y\*cellHeight)  
 background:insert( cell2 )  
  
 else  
 level[x][y] = 2   
 cell1 = display.newImage(cellTable1, x\*cellWidth, y\*cellHeight)  
 barrier:insert( cell1 )  
  
 end  
 end  
 end  
end  
buildGrid()  
--[[  
 -- build screen now --  
 for x = 0, kLevelCols do  
 for y = 0, kLevelRows do  
 if level[x][y] == 2 then  
 cell1 = display.newImage(cellTable1, x\*cellWidth, y\*cellHeight)  
 barrier:insert( cell1 )  
 elseif level[x][y] == 1 then  
 cell2 = display.newImage(cellTable2, x\*cellWidth, y\*cellHeight)  
 background:insert( cell2 )  
 end  
 end  
 end  
  
]]  
  

I commented out the bottom portion of the level generation code as I can populate the level without it, but collisions aren’t detected regardless of which portion is called. Below is my very very simple collision detection code:

local function drag(event)  
 if event.phase == "moved" then  
 hero.x = event.x  
 hero.y = event.y  
 print (hitTestObjects(hero, img2))  
 end  
end  
hero:addEventListener("touch", drag)  

I know that the barrier cells are being generated uniquely as when I run the code with debug physics enabled, I can see the boundaries clearly. The main problem here is that run my code, collisions sound as soon as the player enters the grid, whether it is touching a barrier or the background. I tried removing the background cell from the barrier group, but that didn’t work at all. I’m kind of at a roadblock. Any suggestions, or am I just creating the level in a poor way? Let me know if I can provide any additional info. Thanks for your time!
[import]uid: 135394 topic_id: 22404 reply_id: 107579[/import]