2012-12-12 41 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

没关系,我想通了。运动物体仍然与动态物体发生碰撞。 –