Collision Detection not triggering

My collisions are not detecting.
Here is my code
When enemy touches player this should trigger onContactBegin.
void Player::addPhysics()

{
    auto physicsBody = PhysicsBody::createBox(this->getContentSize());
    physicsBody->setVelocityLimit(maxVelocityLimit);
    auto assetsLoader = new AssetsLoader();
    physicsBody->setContactTestBitmask(assetsLoader->getBotCategoryMask());
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = CC_CALLBACK_1(Player::onContactBegin, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener,this);
    this->addComponent(physicsBody);
}

In Enemy I have the following code for adding physics

void Enemy::addPhysicsBody()
{
    if (this == NULL)
    {
        return;
    }
    auto botContntSize = this->getContentSize();
    auto physicsBody = PhysicsBody::createBox(botContntSize);
    physicsBody->setCategoryBitmask(getCategoryMask());
    this->addComponent(physicsBody);
    return ;
}

Both getCategoryMask and assetsLoader->getBotCategoryMask() return 3(int).
What is causing this error?

I’m unsure of the exact methods (as I use box2d) but you need to set both a category bit mask and a Collision bit mask;

If you set your collision bitmasks to 3 also, then everything will collide with everything else!

See Filtering Collisions - how to? for an explanation of how this works in box2d - which I think is pretty much the same.

Hmm but I am not sure what ContactMask does, are you aware what it does?

Follow the link I gave and read the thread - there is an excellent explanation (if I say so myself)!

1 Like

Do you mean CollisionBitMask?

It defines the other objects, which can can collide with a given one.

No I meant ContactMask only, it provides callback.
http://www.cocos2d-x.org/reference/native-cpp/V3.9/d7/d7b/classcocos2d_1_1_physics_body.html#a6850ee6e1b1aec30b38556ceed1fc4b3

if my contactTestBitmask is BIN xxxxxxxx1

it will collide with any other physics body with a collisionBitmask of xxxxxxx1

Think of the contactTestBitmap as defining the type of object, and the collisionBitmask as defining which object types I want to collide with. a ‘1’ in the same position in the bit mask means Yes - a collision happens.

If you go back to the thread I mentioned and read my post - where I say ‘CategoryBitMask’ read ‘ContactTestBitMask’

Hi @Maxxx
I did follow your thread(must say it is amazing! BTW) .

I thought the same too. But it is not happening for some reason. I have 4 objects. Walls,Player,Bot and Bullet. I have set given them following CategoryBitmask

int playerCategoryMask = 0x1; // 0001
int worldCategoryMask = 0x2; //0010
int botCategoryMask = 0x4;//0100
int bulletCategoryMask = 0x8; //1000

Now say If I want bullet to give contactBody on touching walls then bullet's ContactBitMask should be 2(worldCategoryMask)
So I created method in Bullet

auto physicsBody = PhysicsBody::createBox(this->getContentSize());
    physicsBody->setCategoryBitmask(_assetsLoader->getBulletCategoryMask());
    physicsBody->setCollisionBitmask( _assetsLoader->getBotCategoryMask() + _assetsLoader->getWorldCategoryMask());    
    physicsBody->setContactTestBitmask(_assetsLoader->getWorldCategoryMask());
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = CC_CALLBACK_1(Bullet::onContactBegin, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);

    this->addComponent(physicsBody);

now this contactlistener should work if bullet and walls touch but it doesn’t. In fact other collision type(between player and bot) is picked up even though I have created eventListener Player Class as well.

You shouldn’t use ‘+’ you should use ‘&’ (not saying this is whats causing the problem as I haven’t looked closely, but this only works if you happen to have single bit bitmasks)

e.g. 3 + 2 = 5
011 + 010 = 101 … not what you want
011 &010 = 010 … which is what you want…

** fixed typo above - thanks @iQD

If I were you, to find out where the bug is, I’d hard-code the mask values - if it works, then you know that’s where the problem lies (i.e. the wrong values).

That’s not what you want/get. You mixed & with |. 011 & 010 = 010

  011 = 3
& 010 = 2
  -------  
  010 = 2

  011 = 3
| 010 = 2
  -------  
  011 = 3

Just a typo - I meant AND not OR - just stuffed up my typing - thanks for the spot! hope I haven’t confused anyone beyond all hope!

Hi @Maxxx ,
Thanks for responding. Even though you were right that rather than ‘+’ I should use ‘|’ but in this particular case it wont matter. Thing is I have found out when it catches event and when it doesn’t.

So if let us say if I have to catch ContactBody between bodyA and bodyB then I would have to setContactTestBitmask separately for both of them, then and only then it onContactBegin is fired.
I am not sure but I guess this is bug


PS: all EventListenerPhysicsContact handlers are still called ,is this normal?