好吧,其实这篇也是临时冒出来的,最近朋友要做个物理游戏,以前做物理还是用box2d,呃,确实要花些功夫才能搞懂其中的精髓,但是听讲这套引擎重新封装了一次,要容易很多,所以就简单尝试了一下,感觉确实要简单不少,不过在这其中还是遇到了些问题,首先就来说说onContactBegin这个回调函数响应问题。
先说说情况,简单做了一个打砖块的游戏,前面一切都很顺利,只是做到碰撞检测的时候,发现回调函数弄死都不调用。开始我以为函数写错了,后来查了api,testCpp都没有错,在3.0的api中,没有关于PhysicsBody,PhysicsWorld这些类的说明,所以大家想查移步到3.2的api中吧。
onContactBegin函数的参数就一个,
bool onContactBegin(PhysicsContact& contact),这和3.0beta版本又有些不同,以前是2个,所以在添加事件监听的时候,不要写错,like this,
auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
回调函数是在当两个物体有接触的时候就会响应一次,里面的参数从名字中也能看出来,Contact接触,自然会涉及到两个物体。
好了,下面该到重点了,为啥这个onContactBegin函数不响应呢?问题出在三个掩码值上,这里推荐一下看下这篇博文。
或者打开引擎的源代码,可以在CCPhysicsShape这个头文件里看下这段代码,
/** * 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). */ inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; } inline int getCategoryBitmask() const { return _categoryBitmask; }
/** * 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). */ inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; } inline int getContactTestBitmask() const { return _contactTestBitmask; }
/** * 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). */ inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; } inline int getCollisionBitmask() const { return _collisionBitmask; }
说白了,两个物体间,能不能碰撞,能不能发送接触事件信息,关键就看这个三个参数值。
总结来说:
一个body的CategoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。
一个body的CategoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,会碰撞,否则不碰撞。
这三个参数都有自己的默认值,采用16位表示,
CategoryBitmask, 默认值为 0xFFFFFFFF
ContactTestBitmask, 默认值为 0x00000000
CollisionBitmask, 默认值为 0xFFFFFFFF
大家可以简单的算一下,如果对这个计算不了解,可以查查看哈,或者掏出你电脑上的计算器也可以哇。
按照前面的总结来说,如果我们创建的body都采用默认值的话,那么
CategoryBitmask & ContactTestBitmask = 0
CategoryBitmask & CollisionBitmask = -1
这样看来,情况就清楚了,如果采用默认的数值,碰撞是可以检测的,但是碰撞事件是不会发出的,so我们的onContactBegin就被屏蔽了,那么当然不会做事情。
所以如果想我们的两个物体即发生碰撞又可以检测到,那么很简单,不让它们&的值不等于0就ok了,所以可以将两个需要碰撞的物体的这个三个掩码值都设置成1,
m_ball->getPhysicsBody()->setCategoryBitmask(0x01); m_ball->getPhysicsBody()->setContactTestBitmask(0x01); m_ball->getPhysicsBody()->setCollisionBitmask(0x01);
block->getPhysicsBody()->setCategoryBitmask(0x01); block->getPhysicsBody()->setContactTestBitmask(0x01); block->getPhysicsBody()->setCollisionBitmask(0x01);
这样它们之间怎么按位与的结果都是1,就可以有相应了。
bool HelloWorld::onContactBegin(PhysicsContact& contact) { auto sp1 = (Sprite*)contact.getShapeA()->getBody()->getNode(); auto sp2 = (Sprite*)contact.getShapeB()->getBody()->getNode(); if (sp1->getTag() == 1) sp1->removeFromParentAndCleanup(true); if (sp2->getTag() == 1) sp2->removeFromParentAndCleanup(true); return true; }
前面说了接触是肯定有两个物体的,所以打砖块的逻辑在这里就是判断下,其中一个的tag,如果是我们的砖块,说明碰到了,那么我们移除就ok了。
效果就是这样了。
Cocos2d-x 3.0中 物理碰撞检测中onContactBegin回调函数不响应问题