首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取b2body Box2d的b2Fixture?

获取b2body Box2d的b2Fixture?
EN

Stack Overflow用户
提问于 2012-06-01 18:50:39
回答 1查看 1.7K关注 0票数 1

我在我的Box2D示例应用程序中有大约10个实体,它是我在以下方法的帮助下创建的。但在我的更新方法中,我希望获得body的b2FixtureDef,因为我需要那里的filter.groupIndex,这对于每个body都是不同的。我有如下的更新方法,它循环遍历所有的b2Body,但真的找不到如何从b2Body获取b2FixtureDef?

代码语言:javascript
运行
复制
-(void) addNewSprite:(NSString *)spriteName AtPosition:(CGPoint)p isDynamic:(BOOL)dynamic
{
    //CCNode *parent = [self getChildByTag:kTagParentNode];

PhysicsSprite *sprite = [PhysicsSprite spriteWithFile:spriteName];

[self addChild:sprite];

sprite.position = ccp( p.x, p.y);

sprite.tag = check;

// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;  
bodyDef.position.Set(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);

// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox((sprite.contentSize.width/PTM_RATIO/2)*(sprite.scaleX),
                    (sprite.contentSize.height/PTM_RATIO/2)*(sprite.scaleY));//These are mid points for our 1m box

// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox; 
fixtureDef.density = 1.0f;
fixtureDef.friction = 1.0f;
fixtureDef.userData = sprite;

switch (check)
{
    case 1:
        fixtureDef.filter.categoryBits = 0x0002;

        fixtureDef.filter.maskBits = 0x0004;
        break;
    case 2:
        fixtureDef.filter.categoryBits = 0x0004;

        fixtureDef.filter.maskBits = 0x0002;
        break;
}

body->CreateFixture(&fixtureDef);

[sprite setPhysicsBody:body];

check++;
}

-(void) update: (ccTime) dt
{
    //It is recommended that a fixed time step is used with Box2D for stability
    //of the simulation, however, we are using a variable time step here.
    //You need to make an informed choice, the following URL is useful
    //http://gafferongames.com/game-physics/fix-your-timestep/

int32 velocityIterations = 8;
int32 positionIterations = 1;

// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);    

for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {    

    if (b->GetUserData() != NULL) 
    {

        //b2Fixture fixtureDef = *b->GetFixtureList();
        //b2Filter filter = fixtureDef.GetFilterData();

        // how to get b2FixtureDef from b

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-01 22:28:33

实际上,我无法想象为什么你需要在你的update方法中获取这些信息。您无法获取fixtureDef,因为它仅用于创建装置。但是您可以使用GetFilterData()方法获取每个身体固定装置的过滤器数据。它将包含类别位、掩码位和组索引。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10848889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档