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?