2011-07-02 70 views
0

我已經設置了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

如果其中一個形狀是傳感器,則不會有碰撞反應。 – iforce2d

+0

哇!非常感謝 ! – Curnelious

+0

是的,但您的聯繫人偵聽器仍然可以檢測聯繫人。 –

回答

0

的具體問題是這樣的線:

spriteShapeDef.isSensor = true; 

在Box2D中,當設置一個夾具作爲傳感器,它不會與其他固定裝置碰撞。