box2d collision groups

后端 未结 5 2006
鱼传尺愫
鱼传尺愫 2021-01-04 18:47

does anyone know whether bodies only collide when (body1.categoryBits & body2.maskBits) && (body1.maskBits & body2.categoryBits) ? or do they already collid

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 19:06

    This is the way I've understood how maskBits and categoryBits work. Let's say you have 3 objects : objectA, objectB and objectC.

    Define for each object a category :

    objectA.categoryBits = 0x0002;
    objectB.categoryBits = 0x0004;
    objectC.categoryBits = 0x0008;

    Then, set the maskBits, which define the collisions rules for each categoryBits :

    -> objectA collide with everyone (0xFFFF) and (&) not(~) objectB (0x0004)
    objectA.maskBits = 0xFFFF & ~0x0004;

    -> objectB collide with objectA (0x0002) or (|) objectC (0x0008) but no one else
    objectB.maskBits = 0x0002 | 0x0008;

    -> objectC collide with objectA (0x0002) but no one else
    objectC.maskBits = 0x0002;

    I don't know if this is correct, but it worked for me.

    HTH

提交回复
热议问题