首页 > 代码库 > Cocos2d-x 3.0中 物理碰撞检測中onContactBegin回调函数不响应问题

Cocos2d-x 3.0中 物理碰撞检測中onContactBegin回调函数不响应问题

好吧,事实上这篇也是暂时冒出来的,近期朋友要做个物理游戏,曾经做物理还是用box2d,呃。确实要花些功夫才干搞懂当中的精髓,可是听讲这套引擎又一次封装了一次。要easy非常多,所以就简单尝试了一下,感觉确实要简单不少,只是在这当中还是遇到了些问题,首先就来说说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就被屏蔽了,那么当然不会做事情。

所以假设想我们的两个物体即发生碰撞又能够检測到。那么非常easy,不让它们&的值不等于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回调函数不响应问题