I'm working on a game with cocos2d, need to detect collision between two PhysicsEdgeBody.
I've created an EventListener for contact and added it to the layer i'm working on, PhysicsContact and Collisions between two PhysicsBody or PhysicsBody and PhysicsEdgeBody works fine, but whith two PhysicsEdgeBody it doesn't work at all.
Already tried to:
- set the value of contactTestBitmask, collisionBitmask and categoryBitmask.
- change the value of mass and moment since theirs default value is PHYSICS_INFINITY
- set both the bodies dynamics
- change the materials
- add an event listener for groups and set them on the bodies
but still not working.
Is there something i'm missing?
Here's an example of the GameScene created with: Scene::createWithPhysics()
Also added the command: scene->getPhysicsWorld()->setDebugDrawMask(DEBUGDRAW_ALL);
//Node and PhysicsBody creation
PhysicsBody* bodyA = PhysicsBody::createEdgeBox(someSize, somePhysicsMaterial);
bodyA->setDynamic(true);
bodyA->setMass(100);
bodyA->setCategoryBitmask(0X01); //0001
bodyA->setCollisionBitmask(0X02); //0010
bodyA->setContactTestBitmask(0X02); //0010
PhysicsBody* bodyB = PhysicsBody::createEdgeSegment(Vec2::ZERO, Vec2(visibleSize.width, 0), somePhysicsMaterial);
bodyB->setDynamic(true);
bodyB->setMass(100);
bodyB->setCategoryBitmask(0X02); //0010
bodyB->setCollisionBitmask(0X01); //0001
bodyB->setContactTestBitmask(0X01); //0001
Node* nodeA = Node::create();
nodeA->setPosition(visibleCenter);
nodeA->setPhysicsBody(bodyA);
Node* nodeB = Node::create();
nodeB->setPosition(Vec2::ZERO);
nodeB->setPhysicsBody(bodyB);
this->addChild(nodeA);
this->addChild(nodeB);
//EventListener creation
auto cListener = EventListenerPhysicsContact::create();
cListener->onContactBegan = CC_CALLBACK__1(GameScene::onContactBegan, this);
cListener->onContactPreSolve = CC_CALLBACK_"(GameScene::onContactPreSolve, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(cListener, this);
This callback is never called, even when the two bodies collide on the screen.
//onContactBegan Callback
bool GameScene::onContactBegan(PhysicsContact &contact)
{
PhysicsBody *bodyA = contact.getShapeA()->getPhysicsBody();
PhysicsBody *bodyB = contact.getShapeB()->getPhysicsBody();
if ((bodyA->getCategoryBitmask() == 0X01 && bodyB->getCategoryBitmask() == 0X02) ||
(bodyA->getCategoryBitmask() == 0X02 && bodyB->getCategoryBitmask() == 0X01))
{
CCLOG("COLLISION");
return true;
}
return false;
}