Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17093

EventListenerPhysicsContact Not Working as Expected

$
0
0

@rram wrote:

Could someone help me, I am trying to use EventListenerPhysicsContact::create() as in many tutorial and code snippets but my onBeginContact(PhysiscContact &contact) method is not getting triggered.

The physics collision is happening but event is not triggering collision check. Am I missing something or is there a bug in the event listener? I have also following the Test CPP PhysicsContactTest sample to make sure my code is setup correctly.

I created my physic shapes using PhysicsEditor and do not have any issue loading the shapes and seeing they are colliding and bouncing off each other after collision.


My Code CCTestingScene.cpp (4.3 KB)

There are 4 methods that are called in the init() method:

void initObjectsWithPhysics(); //creates scene and adds boundary to screen
*** void initPhysicsContact(); //creates the physics contact event listener
void initCatcher(); //creates a box and adds physics body
void initBox(); // creates the 2nd box that collides with 1st box

Code that adds Physics Contact Event Listener

void CCTestingScene::initPhysicsContact()
{
    //physics listener
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = CC_CALLBACK_1(CCTestingScene::onContactBegin, this);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

    this->listener = contactListener; //internal pointer to listener to remove in Destructor
    CCLOG("initPhysicsContact()...");
}

bool CCTestingScene::onContactBegin(const PhysicsContact &contact)
{
    CCLOG("Collision detected!");
    return true;
}

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 17093