Detecting multiple collisions of the same object

Hi Guys,

I was wondering if any of you corona elite developers had any tips on how to accomplish the following:

I have marbles a bunch of different colors. I want to fire an event if three or more of the same marbles collide/touch eachother, but not if only two of the same marbles collide/touch eachother. The event I want to fire is to remove the colliding marbles.

Kinda like in Bejeweled. What would be the best way to do this? I tried the collision event but I can’t think of a way to detect three marbles of the same color, instead of two marbles of the same color!

Right now I have this, but as you can see this goes off even if there’s only two of the same marbles touching eachother:

local function onLocalCollision(self, event)  
 if (event.phase == "began") then  
 if (self.myName == "red" and event.other.myName == "red") then  
 self:removeSelf()  
 event.other:removeSelf();  
 end  
 if (self.myName == "yellow" and event.other.myName == "yellow") then  
 self:removeSelf()  
 event.other:removeSelf();  
 end  
 if (self.myName == "green" and event.other.myName == "green") then  
 self:removeSelf()  
 event.other:removeSelf();  
 end  
 end  
end  

Thanks alot! [import]uid: 32882 topic_id: 28563 reply_id: 328563[/import]

How about a function on a marble where in the began phase it sets self.matches = self.matches +1 and -1 on ended phase (obviously only when color matches) - then if self.matches == 2 you remove the marble and the other 2 marbles touching it? [import]uid: 52491 topic_id: 28563 reply_id: 115130[/import]

Hi Peach,

Thanks for your help! I got a little further using your advice (marbles now start dissapearing after 3 of the same color marbles are touching/colliding) but I can’t figure out how to target/remove the marbles connected to the marble that equals self.matches == 2.

I tried removing the touching/colliding marbles by going trough the table and removing every self.matches >= 1 while filtering on the appropriate color, but this cuases other marbles that are connected to be removed as well.

See the illustration below:

Any idea how I could detect which marbles are currently touching another? Sorry for asking so much! I really can’t find much on this subject. [import]uid: 32882 topic_id: 28563 reply_id: 115165[/import]

In the began and ended phase of each collision, in addition to Peach’s suggestion of incrementing or decrementing self.matches to maintain a count of the number of marbles each marble is currently touching, you could also maintain a table, per marble, with references to the marbles that it’s touching. That would make it easier for you to remove them.

  • Andrew [import]uid: 109711 topic_id: 28563 reply_id: 115251[/import]

Great tip! That totally makes sense. This level of programming logic is way above my hat. I’ve tried to introduce a table to store contacts but failed. I think I’ll drop the puzzle idea and go for a more simple concept. I guess that’s the downside of being a designer with a game developing hobby. D: [import]uid: 32882 topic_id: 28563 reply_id: 115577[/import]