Physics - count the objects of some kind “grouped” together

In a physics-based CoronaSDK game I have a number of color balls. They all collide with each other. However I’m interested in those balls grouped together by their color.

So, each of the balls has a “color” property: ball.color = “red” for example.

In the listener for collisions I check if ball collided with the one with the same color:

local function ballCollision ( self, event ) local otherBall = event.other if ( otherBall.color == self.color ) then -- do some stuff here if ( event.phase == "began" ) then -- add the ball to the group else -- remove the ball from the group end end end ball.collision = ballCollision ball:addEventListener ( "collision" ) 

 

Now, I was thinking about creating a global, module-wide, table of “groups”, where I could keep a table of grouped balls. With each began phase of the collision I could add a ball colliding to the group the other collider belongs.

With each ended phase I could remove it from a group.

But this presents some (quite heavy, I think) calculations, for when the larger group is split to several smaller groups by one ball leaving it…

Is there some better solution to perform this? Like - getting a list of “chained” objects, or at least getting a list of colliders for each physics object?

The collision filter chart would the best way to achieve this:

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

But I want the green ball to still collide with red ball… Just not count it as a “group” that is attached to each other (and that counts towards scores). 

Something like in the Frozen Bubble game - where you had the bubbles of similar color react differently (if there were more then 3, all them disappeared). 

The collision filter chart would the best way to achieve this:

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

But I want the green ball to still collide with red ball… Just not count it as a “group” that is attached to each other (and that counts towards scores). 

Something like in the Frozen Bubble game - where you had the bubbles of similar color react differently (if there were more then 3, all them disappeared).