I am able to detect collisions when I create a physics body as a box:
PhysicsBody *body = PhysicsBody::createBox(physicsBox);
body->setGroup(BUNNY_COLLISION_GROUP);
body->setCollisionBitmask(BUNNY_BITMASK);
body->setContactTestBitmask(TRUE);
sprite->setPhysicsBody(body);
scene->addChild(sprite, 1);
And using this event listener:
auto eventContactListener = EventListenerPhysicsContact::create();
eventContactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventContactListener, this);
and this function to detect the collisions:
bool GameScene::onContactBegin(PhysicsContact &contact)
{
PhysicsBody *a = contact.getShapeA()->getBody();
PhysicsBody *b = contact.getShapeB()->getBody();
if ((a->getCollisionBitmask() == BITMASK_1 && b->getCollisionBitmask() == BITMASK_2 ) ||
(a->getCollisionBitmask() == BITMASK_2 && b->getCollisionBitmask() == BITMASK_1 ))
{
CCLOG("Collision!");
}
return true;
}
However, I have started creating physics bodies using polygons and although I am adding the sprites to the same scene as before, the onContactBegin function shown above is no longer detecting collisions.
It is important to note that in the game, the bodies are interacting with each other as they should, I am just not able to detect those collisions in the code.
Is there something different I need to do when detecting physics polygon body collisions?