Page 1 of 1

Objects should collide only with one category

Posted: Mon Nov 04, 2019 4:26 pm
by pauls313
I'm making a 2D soccer game, and the players are made of 2 objects: Head and feet.

I managed to, using groups, stop the feet and head from colliding with each other. But after adding a second player, I realized that the feet should actually ONLY collide with the ball and nothing else. How can this be achieved?

Re: Objects should collide only with one category

Posted: Mon Nov 04, 2019 4:38 pm
by ivan
You don't need separate bodies, you can set the category and mask PER fixture. Also, you can solve this without using "groups". You need at least 3 categories "head", "feet" and "ball", correspondingly: 0x1, 0x2 and 0x4. The trick would be setting the collision masks correctly. The masks should be: 0x0, 0x4, 0x2 when the ball is on the ground in short the "head" category is ignored altogether. Set the first mask to 0x1 if you want the player "heads" to collide with each other.
Not sure if it's a good idea to use collision masks in this way, but it's hard to say without seeing the rest of the code.

Re: Objects should collide only with one category

Posted: Mon Nov 04, 2019 7:08 pm
by pauls313
ivan wrote: Mon Nov 04, 2019 4:38 pm You don't need separate bodies, you can set the category and mask PER fixture. Also, you can solve this without using "groups". You need at least 3 categories "head", "feet" and "ball", correspondingly: 0x1, 0x2 and 0x4. The trick would be setting the collision masks correctly. The masks should be: 0x0, 0x4, 0x2 when the ball is on the ground in short the "head" category is ignored altogether. Set the first mask to 0x1 if you want the player "heads" to collide with each other.
Not sure if it's a good idea to use collision masks in this way, but it's hard to say without seeing the rest of the code.
I'm not really familiar with collisions mask, how would I do that?

Re: Objects should collide only with one category

Posted: Mon Nov 04, 2019 8:12 pm
by ivan