"onContactBegin" is called more than one time

Hello guys.
In documentation is said when a contact happens then the start of contact will be “onContactBegin”. In other words, “onContactBegin” should be called once. or I am wrong?

Other question: If I want “onContactBegin” called once then what is your suggestion?

Thanks

Can you show us what you are doing?

void InSceneManager::handlingContactsNormal() {
    contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = [=] (PhysicsContact& contact) -> bool {
        log("contactListener");
        return contactBeging(contact);
    };

    currentScene->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener,
                                                                               currentScene);
}

bool InSceneManager::contactBeging(PhysicsContact &contact) {
    log("contactID : %i", contact._ID);

    int objectATag = contact.getShapeA()->getTag();
    int objectBTag = contact.getShapeB()->getTag();
    auto nodeA = contact.getShapeA()->getBody()->getNode();
    auto nodeB = contact.getShapeB()->getBody()->getNode();

    if ((objectATag == 1)  && (objectBTag == 1))
    {
        onContactBulletBullet(nodeA, nodeB, objectATag, objectBTag);
    } else if ((objectATag == 1)  && (objectBTag == 2) || (objectATag == 2)  && (objectBTag == 1))
    {
        onContactBulletFireFighter(nodeA, nodeB, objectATag, objectBTag);
    } else if ((objectATag == 1)  && (objectBTag == 3) || (objectATag == 3)  && (objectBTag == 1))
    {
        onContactBulletBigStone(nodeA, nodeB, objectATag, objectBTag);
    } else if ((objectATag == 2)  && (objectBTag == 3) || (objectATag == 3)  && (objectBTag == 2))
    {
        onContactBigStoneFireFighter(nodeA, nodeB, objectATag, objectBTag);
    }

    return true;
}

And can I see the relevant log pieces too

Here you are:

D/cocos2d-x debug info:
                       contactListener
                        contactID : 2503
                        contactBeging
                        printed
                        contactListener
                        contactID : 2504
                        contactBeging
                        printed

As you see the “contactBeginig” is printed twice while I just shot a bullet and it collisioned with target once and then disapeared.

What version?

I have a physics based game I can test with to see if my events are getting called twice.

3.17. That’s so good if you do that and inform Me.

PS: What is your game? you released that?

Could you show me where you create and set the PhysicBody for your bullet ?

Yup I plan to look at this today :slight_smile: a good weekend project.

Any luck with this issue, seems like I am having it too on version 3.17.

Think about that for a second. You have TWO bodies. And they collided. Why would there be TWO onContactBegin calls?

Because bodyA began contact with bodyB and in the same way bodyB began contact with bodyA.

1 Like

But I logged the PhysicsContact on shapeA and shapeB and it seemed that the same order
of arguments was sent to onContactBegin, meaning it was indeed called twice.

Well I don’t see your code so can’t really help much in this case. Post the code and we’ll see.

1 Like

What are your masks set at?

Hey guys. You are able to solve this issue in two ways:

1- set time :
– if ( collision time < 0.001 ) accept
else does not accept it.

2- check if bodies exist:
– if your body removed after first collision, examples include bullet hit an enemy.

I used first one, it works well. I will appreciate if you say another ways.
P.S: Do not think in a complicated way :wink: