Search code examples
cocos2d-xbox2d

Collision Detection not working in cocos2dx v3 using Physics


I want to collide 2 sprites using Physics Engine. But my contact listener is not responding. and no warning/error on log or runtime. So here is what i did :

Declaration

1)cocos2d::PhysicsWorld* m_world;
2)void setPhyWorld(cocos2d::PhysicsWorld* world){m_world = world;}
3)bool onContactBegin(cocos2d::PhysicsContact& contact);

Implementation For PhysicsWorld: Scene ->

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::createWithPhysics();
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

    auto layer = HelloWorld::create();
    layer->setPhyWorld(scene->getPhysicsWorld());

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

PhysicsWorld->

void HelloWorld::createPhysicsWorld(){

    auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
    auto edgeNode = Node::create();
    edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    edgeNode->setPhysicsBody(body);
    this->addChild(edgeNode);
}

Creating Sprite->

void HelloWorld::createCar(){
    car = Sprite::create("car.png");

    auto body3 = PhysicsBody::createBox(car->getContentSize());
    body3->setDynamic(false);
    body3->setCategoryBitmask(2);
    body3->setCollisionBitmask(2);
    body3->setContactTestBitmask(true);
    car->setPhysicsBody(body3);
    // position the sprite on the center of the screen
    car->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(car, 1);

    car->setAnchorPoint(Vec2(0.5, 0));

    car->setFlippedX(true);
}

void HelloWorld::createRocket(){
    rocket = Sprite::create("rocket.png");

    auto body2 = PhysicsBody::createBox(rocket->getContentSize());
    body2->setDynamic(false);
    body2->setCategoryBitmask(1);
    body2->setCollisionBitmask(1);
    body2->setContactTestBitmask(true);
    rocket->setPhysicsBody(body2);

    // position the sprite on the center of the screen
    rocket->setPosition(Vec2(visibleSize.width/2 + origin.x,origin.y));

    // add the sprite as a child to this layer
    this->addChild(rocket, 1);

}

Collision->

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

ContactListner->

bool HelloWorld::onContactBegin(cocos2d::PhysicsContact& contact)
{   
    CCLOG("onContactBegin -------> ");
    return true;
}                                                            

Solution

  • I Just Comment out body2->setDynamic(false); inside createRocket Method and setGravityEnable(false); and it works now.

    void HelloWorld::createRocket(){
        rocket = Sprite::create("rocket.png");
        rocket->setPosition(Vec2(visibleSize.width/2 + origin.x,origin.y - 50));
        rocket->setTag(3);
        auto body2 = PhysicsBody::createBox(rocket->getContentSize());
        //body2->setDynamic(false);
        body2->setTag(3);
        body2->setGravityEnable(false);
        body2->setCollisionBitmask(3);
        body2->setContactTestBitmask(true);
        rocket->setPhysicsBody(body2);
    
        this->addChild(rocket, 1);
    
    }