Box2d联系过滤

时间:2011-08-16 00:08:02

标签: objective-c cocos2d-iphone box2d objective-c++

我在iOS和cocos2d库中使用box2d。 我目前正在尝试使用接触过滤,但发现类别位掩码方法根本不适用于我。我已经理解了这种方法,但由于一些奇怪的原因它无法正常工作。

我在创建对象后初始化我的对象过滤器数据,这是因为我使用外部库并且在创建时没有直接访问夹具。 (它驻留在.plist文件中)。

 bodydef.type = b2_dynamicBody;
 bodydef.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO);
 bodydef.userData = self;
 bodydef.fixedRotation = true;
 //bodydef.angularDamping = 2.0f;
 body = world->CreateBody(&bodydef);

 // add the fixture definitions to the body
 [[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"bubblephysics.plist"];
 [[GB2ShapeCache sharedShapeCache] addFixturesToBody:body forShapeName:filename];
 [sprite setAnchorPoint:[[GB2ShapeCache sharedShapeCache] anchorPointForShape:filename]];

 //Apply a new filter on the initialized body
 b2Fixture *tempFixture = self.body->GetFixtureList();
 b2Filter temp = tempFixture->GetFilterData();

 if ([self isMemberOfClass:[Boopop class]]){    
     temp.categoryBits = 0x0004;
     temp.maskBits = 0x0000;
     temp.groupIndex = 0;
     tempFixture->SetFilterData(temp);
 }
 if ([self isMemberOfClass:[BubbleNode class]]){
     temp.categoryBits = 0x0004;
     temp.maskBits = 0x0000;
     temp.groupIndex = 0;
     tempFixture->SetFilterData(temp);
 }

基本上我会动态更新我的联系人过滤器,有两种方法:

-(void)freezeBubble{
    self.isFrozen = NO; //Stops Iteration of the b2Body and CCSprite;
    b2Fixture *tempFixture = self.body->GetFixtureList();
    b2Filter temp = tempFixture->GetFilterData();
    temp.groupIndex = 0;
    temp.categoryBits = 0x0004;
    temp.maskBits = 0x0000;
    tempFixture->SetFilterData(temp);
}
-(void)unfreezeBubble{
    self.isFrozen = NO;
    b2Fixture *tempFixture = self.body->GetFixtureList();
    b2Filter temp = tempFixture->GetFilterData();
    NSLog(@"Previous Bits: cat = %d, mask = %d",temp.categoryBits,temp.maskBits);
    temp.groupIndex = 0;
    temp.categoryBits = 0x0004;
    temp.maskBits = 0x0000;
    tempFixture->SetFilterData(temp);
}

我尝试过两种技术,groupIndex和categoryBits都不行。将所有组指示设置为-1并不会导致我的所有BubbleNode和Boopop对象都过滤掉碰撞。同样,将maskBits设置为0x0000也不会导致它们过滤它们的冲突。

我非常难过,我想也许这与我的方法有关,但是将所有groupIndex值设置为-1应该会使它们全部过滤掉。 所以现在,我对我的代码的质疑超过了比特。

帮助任何人!
谢谢!

1 个答案:

答案 0 :(得分:1)

我多次阅读你的问题,但无论你是否希望它们发生碰撞,我仍然无法得到它。所以在这里,我将根据您是否希望它们发生碰撞来发布设置categoryBitsmaskBits的方式:

uint16 boopopCategoryBits = 1 << 0;
uint16 bubbleCategoryBits = 1 << 1;

uint16 boopopMaskBits = 0;
uint16 bubbleMaskBits = 0;

bool boobop_n_boobop_should_collide = true;
bool bubble_n_bubble_should_collide = true;
bool boobop_n_bubble_should_collide = true;

if (boobop_n_boobop_should_collide) {
    boopopMaskBits = boopopMaskBits | boopopCategoryBits;
}

if (bubble_n_bubble_should_collide) {
    bubbleMaskBits = bubbleMaskBits | bubbleCategoryBits;
}

if (boobop_n_bubble_should_collide) {
    bubbleMaskBits = bubbleMaskBits | boopopCategoryBits;
    boopopMaskBits = boopopMaskBits | bubbleCategoryBits;
}

结果

(boobop_n_boopop : bubble_n_bubble : boobop_n_bubble = boopopMaskBits : bubbleMaskBits)

true  : true  : true  = 3 : 3

true  : true  : false = 1 : 2

true  : false : false = 1 : 0

false : false : false = 0 : 0

false : true  : false = 0 : 2

false : true  : true  = 2 : 3

false : false : true  = 2 : 1

true  : false : true  = 3 : 1

希望这有帮助。

相关问题