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
bodyA->setCategoryBitmask(BITMASK_A);
bodyB->setCategoryBitmask(BITMASK_B);

// Set BOTH contact test bitmasks (expected to only have to set one)
bodyA->setContactTestBitmask(BITMASK_B);
bodyB->setContactTestBitmask(BITMASK_A);

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);
boundaryBody->setCategoryBitmask(BITMASK_BOUNDARY);
boundaryBody->setContactTestBitmask(BITMASK_HERO);
boundaryBody->setDynamic(false);

// Set boundary body and add to scene
boundaryNode->setPhysicsBody(boundaryBody);
this->addChild(boundaryNode);

// 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());
heroBody->setCategoryBitmask(BITMASK_HERO);

// Set hero body and add to scene
hero->setPhysicsBody(heroBody);
this->addChild(hero);

/*

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

heroBody->setContactTestBitmask(BITMASK_BOUNDARY);

// 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;

walzer@cocos2d-x.org 2014-10-08 02:54

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

This issue was moved to https://github.com/cocos2d/cocos2d-x/issues/8399

Atom PDF

Status:Closed
Start date:2014-04-06
Priority:Low
Due date:
Assignee:-
% Done:

0%

Category:-
Target version:-

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