Reputation: 12719
I am using cocos2dx and setup two Sprites with same Physics Body definitions see below
PhysicsBody *physicsBody=PhysicsBody::createBox(size,PhysicsMaterial(1.0f,0.0,0.5f),Vec2(0, size.height*0.5));
physicsBody->setGravityEnable(true);
physicsBody->setCategoryBitmask(1);
physicsBody->setCollisionBitmask(1);
physicsBody->setContactTestBitmask(true);
physicsBody->setDynamic(true);
physicsBody->setContactTestBitmask(0xFFFFFFFF);
physicsBody->setMass(5.0f);
node->setPhysicsBody(physicsBody);
I am defining a collision detection
between both the physics bodies
auto contactListener=EventListenerPhysicsContactWithBodies::create(player1->getPhysicsBody(), player2->getPhysicsBody());
contactListener->onContactBegin = CC_CALLBACK_1(Game::onPlayerContact, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
I don't want these two bodies to collide with each other or, any effect of physics if they collide. I am doing
bool Game::onPlayerContact(cocos2d::PhysicsContact &contact){
auto nodeA = contact.getShapeA()->getBody()->getNode();
auto nodeB = contact.getShapeB()->getBody()->getNode();
if(nodeA != NULL && nodeB != NULL){
printf("\nPlayers collided");
}
return false;
}
I am not able to bypass the contact between two bodies, what else should be done to avoid collision?
Please Help.
Thanks.
Upvotes: 0
Views: 731
Reputation: 116
You may want to check the Queries Under the Physics http://www.cocos2d-x.org/wiki/Physics Perhaps the Point Query..
Basically the Box2d has sensors which are used to detect contact and not any collision... i recommend using Box2d over Cocos2d Physics...It is much Better..
Upvotes: 0
Reputation: 456
Collision filtering allows you to prevent collision between shapes. Cocos2d-x supports collision filtering using category and groups bitmasks.
Cocos2d-x supports 32 collision categories. For each shape you can specify which category it belongs to. You also specify what other categories this shape can collide with. This is done with masking bits. For example:
auto sprite1 = addSpriteAtPosition(Vec2(s_centre.x - 150,s_centre.y));
sprite1->getPhysicsBody()->setCategoryBitmask(0x02); // 0010
sprite1->getPhysicsBody()->setCollisionBitmask(0x01); // 0001
auto sprite3 = addSpriteAtPosition(Vec2(s_centre.x + 150,s_centre.y + 100),2);
sprite3->getPhysicsBody()->setCategoryBitmask(0x03); // 0011
sprite3->getPhysicsBody()->setCollisionBitmask(0x03); // 0011
then in your collision callback. Do this.
if ((shapeA->getCategoryBitmask() & shapeB->getCollisionBitmask()) == 0
|| (shapeB->getCategoryBitmask() & shapeA->getCollisionBitmask()) == 0)
{
// shapes can't collide
ret = false;
}
source : http://www.cocos2d-x.org/wiki/Physics
Upvotes: 2