遇到物理體碰撞/接觸不按我期望的方式工作的問題。我可能會誤解他們應該如何工作。我的理解是,如果physicsBodyA具有0x1的類別位掩碼,並且physicsBodyB具有0x1的接觸測試位掩碼,則聯繫測試應評估爲true,我應該從事件分派器獲得回調。Cocos2d-x v3.0rc1物理接觸物聯繫人問題
但是這不起作用。我接觸評估爲true的唯一方法是我還設置了physicsBodyA的聯繫位掩碼以匹配physicsBodyB的類別位掩碼。基本上,類別和聯繫測試位掩碼必須相互鏡像。在實踐中,它看起來是這樣的:
bool TestLayer::init() {
// 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 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 = [](PhysicsContact &contact) {
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;
}
請注意,Cocos2d-x v3不使用Box2d,但使用Chipmunk,並且Box2d使用的2位掩碼與Cocos2d-x中使用的3位掩碼相關的方式並不那麼明顯。 – trojanfoe