Collision groupIndex not working as expected

Hi

I am trying to set up a complex collision system where I have 6 floors at different heights, 6 catapults on each floor and waves of enemies moving on each floor. I want the catapult bullets to collide only with the respective floor line and the enemies that spawn in the correspondent line. So catapult bullet 3 collides with floor 3 and enemy 3 and ignores the other levels.

It is a collisionFilter nightmare as there are too many objects to handle with collisionFilters. I am trying to setup a simpler system to control what collides with what by using groupIndex, but it doesn’t seem to work.
In fact I have set up a simpler case to proof concept but groupIndex is not behaving as expected, unless I am missing something basic as I am a newbie.

Here is an example using categoryBits and maskBits which works fine. Circle collides with floor3

require ("physics")  
physics.start()  
physics.setDrawMode("hybrid")  
physics.setGravity (0,20)  
local collisionFilter1 = { categoryBits = 1, maskBits = 1 }  
local collisionFilter2 = { categoryBits = 2, maskBits = 2 }  
local collisionFilter3 = { categoryBits = 4, maskBits = 4 }  
  
--- circle that falls and collides with floor 3, ignoring floor 1 & 2  
circle = display.newCircle(100, 100, 10)  
physics.addBody(circle, "dynamic", {filter = collisionFilter3})  
  
-- create floors with three different collisionFilters  
  
floor1 = display.newRect (0,500, 680, 2)  
floor1:setFillColor (0,255,255)  
physics.addBody(floor1, "static", {filter = collisionFilter1})  
  
floor2 = display.newRect (0,700, 680, 2)  
floor2:setFillColor (0,255,255)  
physics.addBody(floor2, "static", {filter = collisionFilter2})  
  
floor3 = display.newRect (0,900, 680, 2)  
floor3:setFillColor (0,255,255)  
physics.addBody(floor3, "static", {filter = collisionFilter3})  
  

And here is the same case but using groupIndex as stated in the manual reference, but it doens’t work as expected. Circle collides with floor1

require ("physics")  
physics.start()  
physics.setDrawMode("hybrid")  
physics.setGravity (0,20)  
local collisionFilter1 = { groupIndex = 1 }  
local collisionFilter2 = { groupIndex = 2 }  
local collisionFilter3 = { groupIndex = 3 }  
  
--- circle that falls and collides with floor 3, ignoring floor 1 & 2  
circle = display.newCircle(100, 100, 10)  
physics.addBody(circle, "dynamic", {filter = collisionFilter3})  
  
-- create floors with three different collisionFilters  
floor1 = display.newRect (0,500, 680, 2)  
floor1:setFillColor (0,255,255)  
physics.addBody(floor1, "static", {filter = collisionFilter1})  
  
floor2 = display.newRect (0,700, 680, 2)  
floor2:setFillColor (0,255,255)  
physics.addBody(floor2, "static", {filter = collisionFilter2})  
  
floor3 = display.newRect (0,900, 680, 2)  
floor3:setFillColor (0,255,255)  
physics.addBody(floor3, "static", {filter = collisionFilter3})  
  

I have read all the infomration out there and I simply don’t understand why this simple case is not working with groupIndex. Am I missing something incredibly basic here? :frowning:

Thanks in advance [import]uid: 176273 topic_id: 31596 reply_id: 331596[/import]

Hi there,

No, I don’t think you’re missing anything basic. As far as I know, the groupIndex concept doesn’t quite work as you might expect, resulting exactly in the issue you’re facing.

Specifically, the documentation says that “objects with the same positive groupIndex value will always collide with each other”. This seems to imply that objects with different positive groupIndex’s will not collide with each other, but from what I can see, that’s surprisingly not the case. It appears that any object with a positive groupIndex will collide with any other object with a positive groupIndex. I don’t know whether that’s the intended behavior or a bug, but it does make positive groupIndexes not especially useful.

That said, if you only have 6 “sections”, I think you should still be able to handle it using the categoryBits and maskBits method.

  • Andrew [import]uid: 109711 topic_id: 31596 reply_id: 126231[/import]

Thanks Andrew for confirming that groupIndex is not working as it is supposed to do. I’ll report it to Corona and hopefully they can have a look in the near future. In the meantime I’ll stick to categoryBits and Maskbits, I think I have found a way to pass collisionFilters between modules. [import]uid: 176273 topic_id: 31596 reply_id: 126290[/import]

Hi there,

No, I don’t think you’re missing anything basic. As far as I know, the groupIndex concept doesn’t quite work as you might expect, resulting exactly in the issue you’re facing.

Specifically, the documentation says that “objects with the same positive groupIndex value will always collide with each other”. This seems to imply that objects with different positive groupIndex’s will not collide with each other, but from what I can see, that’s surprisingly not the case. It appears that any object with a positive groupIndex will collide with any other object with a positive groupIndex. I don’t know whether that’s the intended behavior or a bug, but it does make positive groupIndexes not especially useful.

That said, if you only have 6 “sections”, I think you should still be able to handle it using the categoryBits and maskBits method.

  • Andrew [import]uid: 109711 topic_id: 31596 reply_id: 126231[/import]

Thanks Andrew for confirming that groupIndex is not working as it is supposed to do. I’ll report it to Corona and hopefully they can have a look in the near future. In the meantime I’ll stick to categoryBits and Maskbits, I think I have found a way to pass collisionFilters between modules. [import]uid: 176273 topic_id: 31596 reply_id: 126290[/import]