android - Cocos2d-x v3.0rc1 PhysicsBody Contact Issue -


having issue physics body collisions/contacts aren't working way anticipate. may misinterpreting how they're supposed work. understanding if physicsbodya has category bitmask of 0x1, , physicsbodyb has contact test bitmask of 0x1, contact tests should evaluate true , should getting callback event dispatcher.

this isn't working, however. way contact evaluating true of set physicsbodya's contact bitmask match category bitmask of physicsbodyb. basically, category , contact test bitmasks must mirror 1 another. in practice looks this:

bool testlayer::init() {      // call super init     if (!layer::init()) {         return false;     }      // screen size     size visiblesize = director::getinstance()->getvisiblesize();      // bitmasks     int bitmask_boundary = 0x1 << 0;     int bitmask_hero = 0x1 << 1;      // boundary node     node *boundarynode = node::create();     boundarynode->setanchorpoint(point(0.5, 0.5));     boundarynode->setposition(point(visiblesize.width/2.0, visiblesize.height/2.0));      // physics body boundary node     physicsbody *boundarybody = physicsbody::createedgebox(visiblesize);     boundarybody->setcategorybitmask(bitmask_boundary);     boundarybody->setcontacttestbitmask(bitmask_hero);     boundarybody->setdynamic(false);      // set boundary body , add scene     boundarynode->setphysicsbody(boundarybody);     this->addchild(boundarynode);      // hero node     sprite *hero = sprite::create("hero_ship.png");     hero->setposition(point(visiblesize.width/2.0, visiblesize.height - 30.0));      // physics body hero node     physicsbody *herobody = physicsbody::createbox(hero->getcontentsize());     herobody->setcategorybitmask(bitmask_hero);      // set hero body , add scene     hero->setphysicsbody(herobody);     this->addchild(hero);      /*      * notice: if don't set contact test bitmask on hero      * body, event listener oncontactbegin callback not      * called.      */      herobody->setcontacttestbitmask(bitmask_boundary);      // create event listener     eventlistenerphysicscontact *listener = eventlistenerphysicscontact::create();      // use lambda listener callback     listener->oncontactbegin = [](physicscontact &contact) {         cclog("physics contact began.");         return true;     };      // register listener event dispatcher     this->geteventdispatcher()->addeventlistenerwithscenegraphpriority(listener, this);      // worked out on init, return true     return true; } 

from box2d manual:

-------- quoted

collision filtering allows prevent collision between fixtures. example, make character rides bicycle. want bicycle collide terrain , character collide terrain, don't want character collide bicycle (because must overlap). box2d supports such collision filtering using categories , groups. box2d supports 16 collision categories. each fixture can specify category belongs to. specify other categories fixture can collide with. example, specify in multiplayer game players don't collide each other , monsters don't collide each other, players , monsters should collide. done masking bits. example:

playerfixturedef.filter.categorybits = 0x0002;  monsterfixturedef.filter.categorybits = 0x0004;  playerfixturedef.filter.maskbits = 0x0004;  monsterfixturedef.filter.maskbits = 0x0002; 

here rule collision occur:

uint16 cata = fixturea.filter.categorybits;  uint16 maska = fixturea.filter.maskbits;  uint16 catb = fixtureb.filter.categorybits;  uint16 maskb = fixtureb.filter.maskbits; if ((cata & maskb) != 0 && (catb & maska) != 0)  {   // fixtures can collide } 

-------- end of quote

so bottom line if want 2 things react same way each other in terms of collisions, both must have same category , mask bits.

for more (very good) in-depth discussion of this, go ahead , look @ site (no, not mine).

for other interesting box2d stuff, look here (this 1 mine).


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -