detect-collisions icon indicating copy to clipboard operation
detect-collisions copied to clipboard

Explanation use of property `group`

Open gregmax17 opened this issue 1 year ago • 6 comments

Can you provide more info on how the collision filtering works with the group property, is more for our use or is it used internally?

gregmax17 avatar Jul 20 '24 21:07 gregmax17

for your use. will explain shortly. for now refer to demo stress test @gregmax17

nenjack avatar Jul 24 '24 04:07 nenjack

I read through the code and will give an explanation to the best of my understanding.

It appears to only be used by the canInteract function. Is like a double bitmask where the top 16 bits are the groups that it can interact with and the bottom 16 are the groups it belongs to. Bodies with the same group number will always be able to interact.

The predefined groups use less than the first 8 bits so the upper 8 bits out of the lower 16 should probably be used for custom groups, 0b00000001 << 16 to 0b10000000 << 16.

  • Predefined groups:
    • 0b00000000_00000000_00000000_00111111
  • Candidates for custom groups:
    • 0b00000000_00000000_11111111_11000000
  • "canInteract"-flags (corresponding to the group_value << 16):
    • 0b11111111_11111111_00000000_00000000

Example:

  • Body 1
    • 0b00000000_00000000_00000001_00000000
  • Body 2
    • 0b11111111_11111111_00000011_00000000
  • Body 3
    • 0b00000010_00000000_00000100_00000000

Body 1 has the first custom group but cannot interact with any other groups except itself because the first 16 bits are all zeros, only bodies with an identical value can interact with it. Body 2 has the first and second group and can interact with all other custom groups , but only if that body also can interact with is custom group. Body 3 has the third group but can interact with the second group. This means that Body 2 and Body 3 can interact with each other but no other body can interact with Body 1 because it doesn't allow interactions with any other custom group.

RedPhoenixQ avatar Aug 03 '24 23:08 RedPhoenixQ

@Prozi If you would like I could make a PR with this documentation added to the group property and the canInteract function with a simple version of this explanation, mainly this part:

  • Predefined groups:
    • 0b00000000_00000000_00000000_00111111
  • Candidates for custom groups:
    • 0b00000000_00000000_11111111_11000000
  • "canInteract"-flags (corresponding to the group_value << 16):
    • 0b11111111_11111111_00000000_00000000

RedPhoenixQ avatar Aug 03 '24 23:08 RedPhoenixQ

The concept of group comes from Box2D's filtering. Here are two links, hope they're helpful.

Box2D Doc (Box2D > Simulation > Shapes > Filtering) Box2D Tutorial: Collision filtering

noih avatar Aug 13 '24 17:08 noih

@Prozi If you would like I could make a PR with this documentation added to the group property and the canInteract function with a simple version of this explanation, mainly this part:

  • Predefined groups:

    • 0b00000000_00000000_00000000_00111111
  • Candidates for custom groups:

    • 0b00000000_00000000_11111111_11000000
  • "canInteract"-flags (corresponding to the group_value << 16):

    • 0b11111111_11111111_00000000_00000000

I always love contributions and merge requests so please do

nenjack avatar Aug 22 '24 20:08 nenjack

The concept of group comes from Box2D's filtering. Here are two links, hope they're helpful.

Box2D Doc (Box2D > Simulation > Shapes > Filtering) Box2D Tutorial: Collision filtering

Thank you for the links

@RedPhoenixQ if you could someway include both of those as also references for docs please do

I think if I recall correctly @noih is the author of those group functions in detect-collisions

Thank you both for using our great library!

nenjack avatar Aug 22 '24 20:08 nenjack

@Prozi If you would like I could make a PR with this documentation added to the group property and the canInteract function with a simple version of this explanation, mainly this part:

  • Predefined groups:

    • 0b00000000_00000000_00000000_00111111
  • Candidates for custom groups:

    • 0b00000000_00000000_11111111_11000000
  • "canInteract"-flags (corresponding to the group_value << 16):

    • 0b11111111_11111111_00000000_00000000

@RedPhoenixQ any update on that PR please?

nenjack avatar Sep 05 '24 02:09 nenjack

I'm anxious and excited for collision filtering like this. It would be really helpful!

gregmax17 avatar Sep 19 '24 22:09 gregmax17

@Prozi sorry for the long pause without response. I've made a PR now

RedPhoenixQ avatar Oct 03 '24 09:10 RedPhoenixQ

thank you very much @RedPhoenixQ

your mr was really good

I ran all the precommit scripts on it and published it here (and on npm)

https://github.com/Prozi/detect-collisions/releases/tag/v9.17.0

@gregmax17 can we close this now if the link above is explaining ok?

nenjack avatar Oct 03 '24 23:10 nenjack

Thank you!

gregmax17 avatar Oct 09 '24 03:10 gregmax17

yay! @gregmax17 closing as resolved

nenjack avatar Oct 11 '24 08:10 nenjack