How to check for specific collisions in cocos2d-x v3.1? C++
<p>I'm attempting to check for specific collisions between sprites with Physics bodies. The problem is that I don't know how to assign either a tag to each node physics body or set the collision bitmask properly for each sprite. I am successfully checking for collisions but it's only a true or false for all objects.</p>

<p>I'm following a guide in the cocos2d-x documentation that I've linked below. In it, it explains how to set masking bits and masking categories.</p>

<blockquote>
  <p>Collision filtering allows you to prevent collision between shapes. Cocos2d-x >supports collision filtering using category and groups bitmasks.</p>
  
  <p>Cocos2d-x supports 32 collision categories. For each shape you can specify which category it belongs to. You also specify what other categories this shape can collide with. This is done with masking bits</p>
</blockquote>

<p>The problem is that when I set:</p>

<p><code>sprite1-&gt;getPhysicsBody()-&gt;setCategoryBitmask(0x02);    // 0010
sprite1-&gt;getPhysicsBody()-&gt;setCollisionBitmask(0x01);   // 0001</code></p>

<p>There <strong>isn't</strong> collision detection for those sprites. But when I set:</p>

<p><code>invaderPhysicsBody-&gt;setContactTestBitmask(true);</code></p>

<p>There <strong>is</strong> a collision detection.</p>

<p>Does anybody know how to successfully set the collision bitmask or category bitmask?</p>

<p><strong>Objects with possible collisions:</strong> </p>

<ol>
<li>Vector of sprites for invaders.</li>
<li>Vector of player missiles</li>
<li>Vector of invader missiles</li>
<li>Sprite for player</li>
<li>Four sprites for shields</li>
</ol>

<p>This is my onContactBegin function that will remove the Node once they collide. This is where I need to test which objects collided.</p>

<pre><code>bool Gameplay::onContactBegin(PhysicsContact &amp;contact) {
    std::cout &lt;&lt; "onContactBegin -------&gt; " &lt;&lt; std::endl;
    player_score += 10;

    auto nodeA = contact.getShapeA()-&gt;getBody()-&gt;getNode();
    auto nodeB = contact.getShapeB()-&gt;getBody()-&gt;getNode();

    std::cout &lt;&lt; contact.getShapeA()-&gt;getTag() &lt;&lt; std::endl;

    if ((contact.getShapeA()-&gt;getCategoryBitmask() &amp; contact.getShapeB()-&gt;getCollisionBitmask()) == 0
        || (contact.getShapeB()-&gt;getCategoryBitmask() &amp; contact.getShapeA()-&gt;getCollisionBitmask()) == 0)
    {
        std::cout &lt;&lt; "Overlap!" &lt;&lt; std::endl;
    }
    nodeA-&gt;removeFromParent();
    nodeB-&gt;removeFromParent();

    return true;
}
</code></pre>

<p>Here is my collision listener:</p>

<pre><code> // Enable collision listener.
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener-&gt;onContactBegin = CC_CALLBACK_1(Gameplay::onContactBegin, this);
    this-&gt;getEventDispatcher()-&gt;addEventListenerWithSceneGraphPriority(contactListener, this);
</code></pre>

<p>This is how I currently create and set the Physics bodies for my sprites:</p>

<pre><code>int spacing = 0;
    // Four Player Shields
    for (int i = 0; i &lt; 4; i++) {


        auto shield = Sprite::create("player_shield.png");

        auto shield_x = shield-&gt;getContentSize();
        auto shield_y = shield-&gt;getContentSize();
        auto shieldPhysicsBody = PhysicsBody::createBox(Size(shield_x.width,shield_y.height), PHYSICSBODY_MATERIAL_DEFAULT);

        shieldPhysicsBody-&gt;setContactTestBitmask(true);
        shieldPhysicsBody-&gt;setDynamic(true);
        shieldPhysicsBody-&gt;setTag(0);

        shield-&gt;setPosition(Vec2(200 + spacing, 150));

        shield-&gt;addComponent(shieldPhysicsBody);
        this-&gt;addChild(shield);
        spacing += 200;
    }
</code></pre>

<p><a href="http://www.cocos2d-x.org/wiki/Physics" rel="nofollow">Here is the guide I've been following.</a></p>