cocos2dx 3.2 碰撞检测

查了不少例子,都3.0的,在3.2上编译就通不过, 改成这样,
auto contactListener = EventListenerPhysicsContactWithBodies::create(mBird->getPhysicsBody(),groundNode->getPhysicsBody());
contactListener->onContactBegin= CC_CALLBACK_1(GameLayer::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);

其实就是要按照contactListener->onContactBegin 回调的函数的声明,去声明你的GameLayer::onContactBegin,再来就是CC_CALLBACK_1,为什么不用CC_CALLBACK_2,我也是后面百度了一下了才有点明白,就是接受参数的个数不同,去选择要1,2.(占位符_1)
这样设好碰撞检测的回调了,现在开始设置两个要碰撞的物体(Sprite + PhysicsBody).

// Add the ground
this->groundNode= Node::create();
float landHeight = land1->getContentSize().height;
auto groundBody = PhysicsBody::create();
groundNode->setContentSize(Size(288, landHeight));
groundBody->addShape(PhysicsShapeBox::create(groundNode->getContentSize()));
groundBody->setDynamic(false);
groundBody->setLinearDamping(0.0f);
this->groundNode->setPhysicsBody(groundBody);
this->groundNode->setPosition(144, landHeight/2);

groundBody->setContactTestBitmask(1);
//add bird
mBird= BirdSprite::createWithSpriteFrameName(“bird_0.png”);
auto mBirdBody = PhysicsBody::create();
auto mBirdShape = PhysicsShapeCircle::create(15);
mBirdBody->addShape(mBirdShape);
mBirdBody->setLinearDamping(0);
mBirdBody->setGravityEnable(false);
mBirdBody->setContactTestBitmask(1);

mBird->setPhysicsBody(mBirdBody);

这些其实都挺好懂,但关健的就是这句:mBirdBody->setContactTestBitmask(1);没加这句,onContactBegin是怎么也不被执行到.
虽然这样写,onContactBegin是执行了,但是我是还没搞懂setContactTestBitmask(1)的参数是怎么个意思,我说下我怎么会知道这个,是我去查了cpp-tests里的代码得到的,setContactTestBitmask(0xFFFFFFFF),一个32位的int.

总结: 1.设置回调函数 void GameLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event);
2.PhysicsBody->setContactTestBitmask(1);

inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }//定义了这个物理刚体是属于哪个类别的掩码
inline int getCategoryBitmask() const { return _categoryBitmask; }

inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; }//它定义了哪些类别的刚体与此物理刚体产生交集(相互作用)的通知
inline int getContactTestBitmask() const { return _contactTestBitmask; }

inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }//它定义了哪些类别的物理刚体可以与这物理刚体发生碰撞。
inline int getCollisionBitmask() const { return _collisionBitmask; }

厉害,这下真正搞正了