3.0 rc0里的物理碰撞检测,为什么调用不到呢?

大家帮我看看代码哪里出了问题,很简单的代码,但是onContactBegin就是没有被调用,研究了半天,实在是没办法了,
只好来求助坛子了。

class HelloWorld : public cocos2d::Layer
{
public:
Sprite *_ball;
PhysicsWorld *m_world;

void setPhyWorld(PhysicsWorld* world){ m_world = world; };

// there’s no ‘id’ in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();

// Here’s a difference. Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone
virtual bool init();

bool onContactBegin(PhysicsContact& contact);

// implement the “static create()” method manually
CREATE_FUNC(HelloWorld);
};

Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();

scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

auto layer = HelloWorld::create();

layer->setPhyWorld(scene->getPhysicsWorld());

scene->addChild(layer);

return scene;

}

bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}

auto visibleSize = Director::getInstance()->getVisibleSize();

_ball = Sprite::create("Ball.jpg");
_ball->setPosition(Point(400,600));
auto ballBody = PhysicsBody::createCircle(_ball->getContentSize().width / 2);
_ball->setPhysicsBody(ballBody);
this->addChild(_ball);

auto edgeSp = Sprite::create();

auto boundBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 30);
edgeSp->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
edgeSp->setPhysicsBody(boundBody);
this->addChild(edgeSp);
edgeSp->setTag(0);

auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);

return true;
}

bool HelloWorld::onContactBegin(PhysicsContact& contact)
{
CCLOG(“onContactBegin”);
return true;

}

运行时,球落地后,bool HelloWorld::onContactBegin(PhysicsContact& contact)没有被调用到。

同求。。 怎么 检测 A物体和 其他很多物体之间有没有发生碰撞?。。:7:

rc0改了代码,要自己设置标志位,后两个设置成-1(0xFFFFFFFF)就可以了
//this->getPhysicsBody()->setCategoryBitmask(1);
this->getPhysicsBody()->setCollisionBitmask(-1);
this->getPhysicsBody()->setContactTestBitmask(-1);

问下 ,这三个函数的参数 设置的原理是什么。
参数都是32位的位掩码。如何设置,才能让两个物体 碰撞或者不碰撞那。有没有什么特别的算法?

已回复,看你的帖子。

多谢,经实验可行。
看了下源码,CollisionBitmask默认为-1,所以只需要设置ContactTestBitmask就可以了。

准备再看下CPP TEST更深入了解一下这几个MASK。

还是使用Box2D