2012-12-12 40 views
1

目前,Cocos2d-Box2d項目正在使用b2Vec2爲遊戲邊緣創建邊界框。正因爲如此,邊界框不會影響運動物體,它是不受力影響的物體(意味着物體通常會飛離屏幕)。我試圖看看是否有辦法讓運動體與屏幕連接。如果沒有,我會很感激,如果有人向我解釋我應該如何在屏幕的角落製造一個帶有靜態物體的邊框。Box2d沒有力量的邊界框

回答

0

下面是兩種方法...嘗試任何一個

//方法 - 1

b2BodyDef groundBodyDef; 
groundBodyDef.position.Set(0, 0); 
b2Body    *mGroundBody ; 


mGroundBody = self.world->CreateBody(&groundBodyDef); 
NSString *strId = @"Ground Body"; 
mGroundBody->SetUserData(strId); 

b2EdgeShape groundBox;  

    //bottom 
    groundBox.Set(b2Vec2(0.0f,0.0f), b2Vec2(mS.width/PTM_RATIO,0.0f)); 
    mGroundBody->CreateFixture(&groundBox,0); 

    // top 
    groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO, mS.height/PTM_RATIO)); 
    mGroundBody->CreateFixture(&groundBox,0); 


    // left 
    groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(0,0)); 
    mGroundBody->CreateFixture(&groundBox,0); 

    // right 
    groundBox.Set(b2Vec2(mS.width/PTM_RATIO,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO,0)); 
    mGroundBody->CreateFixture(&groundBox,0); 

//方法 - 2

//create 4 box2d walls... 

    float bW = (IS_IPAD) ? (8) : 2 ; 
    //top 
    { 
     b2BodyDef bodyDef; 
     bodyDef.type = b2_staticBody; 
     bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, (mS.height)/PTM_RATIO); 
     bodyDef.linearDamping = 0.0f; 
     bodyDef.angularDamping = 0.0f; 

     bodyDef.userData = strId ; 

     b2PolygonShape box; 
     box.SetAsBox(((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO); 

     b2FixtureDef fixDef; 
     fixDef.shape = &box; 
     fixDef.density = 1.0f; 
     fixDef.friction = 0.1f; 
     fixDef.restitution = 1.0f; 
     fixDef.isSensor = false; 

     b2Body *topBody = self.world->CreateBody(&bodyDef); 
     topBody->CreateFixture(&fixDef); 

    } 

    //bottom 
    { 
     b2BodyDef bodyDef; 
     bodyDef.type = b2_staticBody; 
     bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, 0); 
     bodyDef.linearDamping = 0.0f; 
     bodyDef.angularDamping = 0.0f; 

     bodyDef.userData = strId ; 

     b2PolygonShape box; 
     box.SetAsBox(((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO); 

     b2FixtureDef fixDef; 
     fixDef.shape = &box; 
     fixDef.density = 1.0f; 
     fixDef.friction = 0.1f; 
     fixDef.restitution = 1.0f; 
     fixDef.isSensor = false; 

     b2Body *topBody = self.world->CreateBody(&bodyDef); 
     topBody->CreateFixture(&fixDef); 

    } 

    //left 
    { 
     b2BodyDef bodyDef; 
     bodyDef.type = b2_staticBody; 
     bodyDef.position.Set(0, (mS.height*0.5f)/PTM_RATIO); 
     bodyDef.linearDamping = 0.0f; 
     bodyDef.angularDamping = 0.0f; 

     bodyDef.userData = strId ; 

     b2PolygonShape box; 
     box.SetAsBox(((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO); 

     b2FixtureDef fixDef; 
     fixDef.shape = &box; 
     fixDef.density = 1.0f; 
     fixDef.friction = 0.1f; 
     fixDef.restitution = 1.0f; 
     fixDef.isSensor = false; 

     b2Body *topBody = self.world->CreateBody(&bodyDef); 
     topBody->CreateFixture(&fixDef); 

    } 

    //right 
    { 
     b2BodyDef bodyDef; 
     bodyDef.type = b2_staticBody; 
     bodyDef.position.Set((mS.width)/PTM_RATIO, (mS.height*0.5f)/PTM_RATIO); 
     bodyDef.linearDamping = 0.0f; 
     bodyDef.angularDamping = 0.0f; 

     bodyDef.userData = strId ; 

     b2PolygonShape box; 
     box.SetAsBox(((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO); 

     b2FixtureDef fixDef; 
     fixDef.shape = &box; 
     fixDef.density = 1.0f; 
     fixDef.friction = 0.1f; 
     fixDef.restitution = 1.0f; 
     fixDef.isSensor = false; 

     b2Body *topBody = self.world->CreateBody(&bodyDef); 
     topBody->CreateFixture(&fixDef); 
    } 
+0

無論選擇似乎工作。無論如何,我遇到了一個新問題。最初,我認爲Box2d運動體不與Box2d牆相碰是奇怪的,但後來我才知道運動體不會檢測到碰撞.... 這是很奇怪的,因爲Kinematic Body是碰撞動態身體我有... –

+0

沒關係,我想通了。運動物體仍然與動態物體發生碰撞。 –