Projects > cpp > Issues > Bug #4701

We are migrating issue tracker of Cocos2d-x Project to Github, please create new issue there. Thanks.

Create Issue on Github

Physics Body Collision and Contact Issues

Bug #4701 [Closed]
Thruxton 2014-04-06 18:34 . Updated about 2 years ago

Having an issue where physics body collisions/contacts aren't working the way I anticipate. I may be misinterpreting how they're supposed to work. My understanding is that if physicsBodyA has a category bitmask of 0x1, and physicsBodyB has a contact test bitmask of 0x1, contact tests should evaluate to true and I should be getting a callback from the event dispatcher.

This isn't working, however. The only way that I get a contact evaluating to true is of I also set physicsBodyA's contact bitmask to match the category bitmask of physicsBodyB. Basically, the category and contact test bitmasks must mirror one another. In practice it looks something like this:

// Bitmasks
int BITMASK_A = 0x1 << 0;
int BITMASK_B = 0x1 << 1;

// Set category bitmasks

// Set BOTH contact test bitmasks (expected to only have to set one)

Based on the docs and other engines that I've used, this seems wrong, but maybe I'm confused. Any help would be greatly appreciated. Thanks!

Thruxton 2014-04-06 19:08

Update: wanted to specify that I'm using Cocos2d v3.0rc1, and also attach a small code sample of an init() method to better illustrate my point:

// Call super init
if (!Layer::init()) {
return false;

// Get screen size
Size visibleSize = Director::getInstance()->getVisibleSize();

// Bitmasks
int BITMASK_BOUNDARY = 0x1 << 0;
int BITMASK_HERO = 0x1 << 1;

// Boundary node
Node *boundaryNode = Node::create();
boundaryNode->setAnchorPoint(Point(0.5, 0.5));
boundaryNode->setPosition(Point(visibleSize.width/2.0, visibleSize.height/2.0));

// Physics body for boundary node
PhysicsBody *boundaryBody = PhysicsBody::createEdgeBox(visibleSize);

// Set boundary body and add to scene

// Hero node
Sprite *hero = Sprite::create("hero_ship.png");
hero->setPosition(Point(visibleSize.width/2.0, visibleSize.height - 30.0));

// Physics body for hero node
PhysicsBody *heroBody = PhysicsBody::createBox(hero->getContentSize());

// Set hero body and add to scene


  • NOTICE: If I don't set the contact test bitmask on my hero
  • body, the event listener's onContactBegin callback will not
  • be called. */


// Create an event listener
EventListenerPhysicsContact *listener = EventListenerPhysicsContact::create();

// Use lambda for listener callback
listener->onContactBegin = {
CCLOG("Physics contact began.");
return true;

// Register listener with event dispatcher
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

// Everything worked out on init, return true
return true; 2014-10-08 02:54

Redmine issue system is closed, we are using github issue system instead.

This issue was moved to

Atom PDF

Start date:2014-04-06
Due date:
% Done:


Target version:-

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.