[Solved] Problems after upgrade to V3 RC using EventListenerPhysicsContact

After upgrade to V3 RC, i got this errors:

#1

auto contactListener = EventListenerPhysicsContact::create();
**contactListener->onContactBegin = CC_CALLBACK_2(StageScene::onContactBegin);**

ERROR: No viable overloaded ‘=’

Why i cannot use it more? Can anyone help me?

#2

auto enemyBody = PhysicsBody::createBox(Size(enemy->getWidth(), enemy->getHeight()));
        
enemyBody->setRotationEnable(false);
enemyBody->setDynamic(false);
enemyBody->setGravityEnable(false);
enemy->setPhysicsBody(enemyBody);

Before update, with this code i move my objects manually and it generate Physic contact but is not affected by gravity. After update every object is going down :frowning: What i do to continue moving it manually and generate contact event when collide using cocos2d-x RC v3 ?

#1 - Solved

Changed: CC_CALLBACK_2 to CC_CALLBACK_1

Not solved :frowning:

It compile. But no event of collision happen.

My listener code:

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

My receive event function:

bool StageScene::onContactBegin(const PhysicsContact& contact)
{
       CCLOG("Colisão detectada!");
       ....
}

I put a breakpoint on collision method but nothing happen and not generate log :frowning:

I get the code from here:
https://github.com/cocos2d/cocos2d-x/blob/develop/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp
https://github.com/cocos2d/cocos2d-x/blob/develop/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h

Can anyone help me?

You need to setContactTestBitmask for the body, because the listener only receive the contact event you want to receive. It ignore all the contact event by default.

The problem #1 is solved, but only if i set dynamic = true.

But im using only collision from physic, i dont want that the objects is affected by gravity. Im trying it:

    auto coinBody = PhysicsBody::createBox(Size(coin->getWidth(), coin->getHeight()));
    coinBody->setRotationEnable(false);
    coinBody->setDynamic(false);
    coinBody->setGravityEnable(false);
    coinBody->setContactTestBitmask(0xFFFFFFFF);
    coin->setPhysicsBody(coinBody);

But with “coinBody->setDynamic(false);” no collision is detected, but the COIN position is updated manually(only position X), and when i set “coinBody->setDynamic(true);” the COIN if affected by gravity (but i set the gravity enable = false) and generate collision.

I cannot create a static object, that i update it manually and is not affected by gravity and generate collision event?

@prchakal
Hi:
How to fix this: ERROR: No viable overloaded ‘=’

@prchakal Hmm, maybe it’s a bug. I think you can set the gravity to 0. Try to use this: world->setGravity(Vect::ZERO)

@rinzz Change CC_CALLBACK_2 to CC_CALLBACK_1, delete event param in callback function.

The bug of setGravityEnable is conformed, I create an issue here:
http://cocos2d-x.org/issues/4424

@boyu0 - For now, with Zero gravity, the problem not exists, i dont need it for this project. Thanks

rinzz - You need set your callback for ContactBegin with the CC_CALLBACK_1, and your method need have only one parameter, the PhysicsContact. Example:

CREATING THE LISTENER

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

CREATING THE METHOD THAT WILL RECEIVE THE LISTENER EVENTS

bool StageScene::onContactBegin(const PhysicsContact& contact)
{
       CCLOG("Collision detected!");
       ....
}

Thanks.

@boyu0
Then, whats going on with this gravity bug now ?
I downloaded the latest 3.0rc, and also encountered this problem just now …

We hace to set a ZERO gravity and simulate for each object ?

Im my case i do it, i set gravity to zero and make it move manually without physics. I want use physics only for collision detect.

Cococs2d-X v3 RC has a bug on physics, but in my case i dont need wait solve the problem because my workaround solve my problem.