碰撞检测的问题, onContactBegin没有触发

以下是我的代码,onContactBegin方法总是触发不到,不知道为什么,仔细对比了一下小鸟中的代码,似乎应该设置的东西都设了,实在搞不明白了,请高手指点一下。

这是.h文件中的代码

class collisionTest : public cocos2d::Layer
{
public:
    collisionTest();
    ~collisionTest();


    bool init();

    static cocos2d::Scene* createScene();

    CREATE_FUNC(collisionTest);

    //碰撞检测
    virtual bool onContactBegin(PhysicsContact& contact);

    void update(float dt);

    //给层创建物理世界用的
    PhysicsWorld* m_world;
    void setPhyWorld(PhysicsWorld* world) { m_world = world; };

private:
    Sprite* sp1 = Sprite::create("Manleft.png");
    Sprite* sp2 = Sprite::create("ManRight.png");
};


```



这是cpp中的代码:

bool collisionTest::init() {

    if (!Layer::init()) {

        return false;
    }
    else {
        auto winSize = Director::getInstance()->getVisibleSize();

        PhysicsBody* body1 = PhysicsBody::createBox(sp1->getContentSize());
        PhysicsBody* body2 = PhysicsBody::createBox(sp2->getContentSize());

        body1->setDynamic(true);
        body2->setDynamic(true);

        //设置碰撞检测
        body1->setCategoryBitmask(1);
        body2->setCategoryBitmask(1);

        body1->setContactTestBitmask(-1);
        body2->setContactTestBitmask(-1);

        body1->setCollisionBitmask(-1);
        body2->setCollisionBitmask(-1);

        //
        body1->setMass(1.0f);
        body2->setMass(1.0f);

        //设置刚体阻尼
        body1->setLinearDamping(0.1f);
        body2->setLinearDamping(0.1f);

        sp1->setPhysicsBody(body1);
        sp2->setPhysicsBody(body2);

        sp1->setPosition(winSize.width / 2, winSize.height-sp1->getContentSize().height);
        sp2->setPosition(winSize.width / 2, sp2->getContentSize().height);

        this->addChild(sp1);
        this->addChild(sp2);

        //
        auto contactListener = EventListenerPhysicsContact::create();
        contactListener->onContactBegin = CC_CALLBACK_1(collisionTest::onContactBegin, this);
        this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

        this->scheduleUpdate();

        return true;
    }
}


Scene* collisionTest::createScene() {

    auto scene = Scene::createWithPhysics();

    auto layer = collisionTest::create();

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

    scene->addChild(layer);

    return scene;

}


// 碰撞检测
bool collisionTest::onContactBegin(PhysicsContact& contact) {

    CCLOG("boom!!!!");

    this->unscheduleAllSelectors();

    return true;

}


void collisionTest::update(float dt) {

    sp1->setPositionY(sp1->getPositionY()-2);

    sp2->setPositionY(sp2->getPositionY()+2);

}


```

设置碰撞没对吧,试试这样

   //设置碰撞检测
    body1->setCategoryBitmask(1); //自己是什么组
    body2->setCategoryBitmask(2);

    body1->setContactTestBitmask(2); //和什么组相交会有回调
    body2->setContactTestBitmask(1);

    body1->setCollisionBitmask(2); //和什么组会碰撞
    body2->setCollisionBitmask(1);

参考:http://cn.cocos2d-x.org/tutorial/show?id=2395

感谢提供那篇文章。

根据那篇文章里面写的,我的设置其实也没错,只要不适用默认值,任意改变那三个属性中的一个的值,其实都应该能够符合触发碰撞检测的要求。

但是现在还是不会触发onContactBegin事件:6:

根据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:

大神急急如律令!!为毛啊为毛啊?

一天一顶。

终于,搞定了,但是我就是不说怎么搞定的:14:

:11: :11:

:877::877::877::877:总算又搞清楚、解决了另一个困扰了一天的问题,还是碰撞检测的。:698::698::698::698:

我居然也遇到了

然后呢?:14:

兄弟你这样,以后谁还会帮你,就是不说!!!汗!孩子一样

要先addshape()吧,都是用shape来设置各种碰撞类型的,一不小心就进坑了

lz 我也碰到这个问题了 ,请问你是怎么解决的呀

你是不是用的物理引擎?