根据CCPhysicsBody中的描述:
/**
* A mask that defines which categories this physics body belongs to.
* Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
* The default value is 0xFFFFFFFF (all bits set).
*/
void setCategoryBitmask(int bitmask);
/**
* A mask that defines which categories of bodies cause intersection notifications with this physics body.
* When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
* The default value is 0x00000000 (all bits cleared).
*/
void setContactTestBitmask(int bitmask);
/**
* A mask that defines which categories of physics bodies can collide with this physics body.
* When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
* The default value is 0xFFFFFFFF (all bits set).
*/
void setCollisionBitmask(int bitmask);
```
我的代码如下:
#include "test.h"
USING_NS_CC;
Scene* test::createScene() {
auto scene = Scene::create();
auto layer = test::create();
scene->addChild(layer);
return scene;
}
bool test::init() {
if (!Layer::init()) {
return false;
}
else {
auto winSize = Director::getInstance()->getVisibleSize();
auto body1 = PhysicsBody::createBox(sp1->getContentSize());
auto shape1 = PhysicsShapeBox::create(sp1->getContentSize());
body1->setDynamic(false);
body1->setMass(1.0f);
CCLOG("-------body1's Original values-------");
CCLOG("body1's ContactTestBitmask is %d ",body1->getContactTestBitmask());
CCLOG("body1's CategoryBitmask is %d ", body1->getCategoryBitmask());
CCLOG("body1's CollisionBitmask is %d ", body1->getCollisionBitmask());
body1->setContactTestBitmask(0x02);
//body1->setCategoryBitmask(0x01);
body1->setCollisionBitmask(0x03);
CCLOG("-------after setup, body1's values-------");
CCLOG("body1's ContactTestBitmask is %d ", body1->getContactTestBitmask());
CCLOG("body1's CategoryBitmask is %d ", body1->getCategoryBitmask());
CCLOG("body1's CollisionBitmask is %d ", body1->getCollisionBitmask());
body1->addShape(shape1);
sp1->setPosition(Point(winSize.width/2,winSize.height/2+sp1->getContentSize().height));
sp1->setAnchorPoint(Point::ANCHOR_MIDDLE);
sp1->setPhysicsBody(body1);
this->addChild(sp1);
auto body2 = PhysicsBody::createBox(sp2->getContentSize());
auto shape2 = PhysicsShapeBox::create(sp2->getContentSize());
body2->setDynamic(true);
body2->setMass(5.0f);
CCLOG("-------body2's Original values-------");
CCLOG("body2's ContactTestBitmask is %d ", body2->getContactTestBitmask());
CCLOG("body2's CategoryBitmask is %d ", body2->getCategoryBitmask());
CCLOG("body2's CollisionBitmask is %d ", body2->getCollisionBitmask());
body2->setContactTestBitmask(0x02);
//body2->setCategoryBitmask(0x01);
body2->setCollisionBitmask(0x03);
CCLOG("-------after setup, body2's values-------");
CCLOG("body2's ContactTestBitmask is %d ", body2->getContactTestBitmask());
CCLOG("body2's CategoryBitmask is %d ", body2->getCategoryBitmask());
CCLOG("body2's CollisionBitmask is %d ", body2->getCollisionBitmask());
body2->addShape(shape2);
sp2->setPosition(Point(winSize.width/2,winSize.height/2-sp2->getContentSize().height));
sp2->setAnchorPoint(Point::ANCHOR_MIDDLE);
sp2->setPhysicsBody(body2);
this->addChild(sp2);
CCLOG("-------Compare result-------");
CCLOG("body1's ContactTestBitmask & body2's CategoryBitmask result is %d", body1->getContactTestBitmask() & body2->getCategoryBitmask());
CCLOG("body1's CategoryBitmask & body2's CollisionBitmask result is %d", body1->getCategoryBitmask() & body2->getCollisionBitmask());
CCLOG("body2's ContactTestBitmask & body1's CategoryBitmask result is %d", body2->getContactTestBitmask() & body1->getCategoryBitmask());
CCLOG("body2's CategoryBitmask & body1's CollisionBitmask result is %d", body2->getCategoryBitmask() & body2->getCollisionBitmask());
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(test::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener,this);
this->scheduleUpdate();
return true;
}
}
void test::update(float dt) {
sp1->setPositionY(sp1->getPositionY()-2);
sp2->setPositionY(sp2->getPositionY() + 2);
}
bool test::onContactBegin(PhysicsContact& contact) {
CCLOG("onContactBegin");
this->unscheduleUpdate();
return true;
}
```
我的代码输出的结果如下:
-------body1's Original values-------
body1's ContactTestBitmask is 0
body1's CategoryBitmask is -1
body1's CollisionBitmask is -1
-------after setup, body1's values-------
body1's ContactTestBitmask is 2
body1's CategoryBitmask is -1
body1's CollisionBitmask is 3
-------body2's Original values-------
body2's ContactTestBitmask is 0
body2's CategoryBitmask is -1
body2's CollisionBitmask is -1
-------after setup, body2's values-------
body2's ContactTestBitmask is 2
body2's CategoryBitmask is -1
body2's CollisionBitmask is 3
-------Compare result-------
body1's ContactTestBitmask & body2's CategoryBitmask result is 2
body1's CategoryBitmask & body2's CollisionBitmask result is 3
body2's ContactTestBitmask & body1's CategoryBitmask result is 2
body2's CategoryBitmask & body1's CollisionBitmask result is 3
根据CCPhysicsBody中的描述的情况来看,这个结果应该是可以触发onContactBegin事件了的呀,为什么还是没有反应呢?:6:
大神急急如律令!!为毛啊为毛啊?