身体不会发生碰撞

时间:2011-07-02 18:07:41

标签: xcode cocos2d-iphone box2d

我设置了2个物体,一个有重力的世界,一个带步进的蜱法,所有的物体都移动得很好,我可以对它们施加力,重力也在起作用。

但没有碰撞。 当body1击中地面时,他只是走出屏幕而不是击中它并像一个真正的球一样跳跃。 当body1击中body2时,只是继续运动,因为什么都没发生。

身体有形状,世界有边缘,但没有碰撞。 我在这里失踪了什么?

这是我从init调用的一些函数:

- (void)addBoxBodyForSprite:(CCSprite *)sprite {

    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
    spriteBodyDef.userData = sprite;
    spriteBody = world->CreateBody(&spriteBodyDef);

    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteShapeDef.density = 10.0;
    spriteShapeDef.isSensor = true;
    spriteBody->CreateFixture(&spriteShapeDef);

}


-(void)worldEdge
{
    CGSize winSize = [CCDirector sharedDirector].winSize;
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0,0);
    b2Body *groundBody = world->CreateBody(&groundBodyDef);
    b2PolygonShape groundBox;
    b2FixtureDef boxShapeDef;
    boxShapeDef.shape = &groundBox;
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);
}




-(void)tick:(ccTime) dt
{


    world->Step(dt,10,10);
    for(b2Body *b=world->GetBodyList(); b; b=b->GetNext()) 
    {
        if(b->GetUserData() !=NULL )
           {
               CCSprite *sprite=(CCSprite *) b->GetUserData();//every b of the world will be update his position
               sprite.position=ccp( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO  ) ;
               sprite.rotation=-1*CC_RADIANS_TO_DEGREES(b->GetAngle());
           }

    }
一切都很好,但没有碰撞。

1 个答案:

答案 0 :(得分:0)

具体问题是这一行:

spriteShapeDef.isSensor = true;

在box2d中,当您将灯具设置为传感器时,它不会与其他灯具发生碰撞。