Box2D V2.x中SetAsEdge的替代方法

来源:互联网 发布:淘宝退款被卖家拒绝了 编辑:程序博客网 时间:2024/05/21 17:01
// Define the ground body.
    b2BodyDef groundBodyDef
;
    groundBodyDef
.position.Set(0, 0); // bottom-left corner

    
// Call the body factory which allocates memory for the ground body
    
// from a pool and creates the ground box shape (also from a pool).
    
// The body is also added to the world.
    groundBody 
= world->CreateBody(&groundBodyDef);

    
// Define the ground box shape.
    b2PolygonShape groundBox
;       

    
// bottom
    groundBox
.SetAsEdge(b2Vec2(0,FLOOR_HEIGTH/PTM_RATIO),b2Vec2(screenSize.width*2.0f/PTM_RATIO,FLOOR_HEIGTH/PTM_RATIO));
    groundBody
->CreateFixture(&groundBox,0);

    
// top
    groundBox
.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO),b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO));
    groundBody
->CreateFixture(&groundBox,0);

    
// left
    groundBox
.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
    groundBody
->CreateFixture(&groundBox,0);

    
// right
   groundBox
.SetAsEdge(b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO),b2Vec2(screenSize.width*2.0f/PTM_RATIO,0));
    groundBody
->CreateFixture(&groundBox,0);

Box2D v2.*中API已经移除了v1.*中的SetAsEdge方法,所以我们可以用如下方法来替换SetAsEdge方法

用b2EdgeShape 而不是 b2PolygonShape,直接用b2EdgeShape的Set方法来替换

// for the screenBorder body we'll need these values
    
CGSize screenSize = [CCDirector sharedDirector].winSize;
    
float widthInMeters = screenSize.width / PTM_RATIO;
    
float heightInMeters = screenSize.height / PTM_RATIO;
    b2Vec2 lowerLeftCorner 
= b2Vec2(0, 0);
    b2Vec2 lowerRightCorner 
= b2Vec2(widthInMeters, 0);
    b2Vec2 upperLeftCorner 
= b2Vec2(0, heightInMeters);
    b2Vec2 upperRightCorner 
= b2Vec2(widthInMeters, heightInMeters);

    
// Define the static container body, which will provide the collisions at screen borders.
    b2BodyDef screenBorderDef
;
    screenBorderDef
.position.Set(0, 0);
    b2Body
* screenBorderBody = world->CreateBody(&screenBorderDef);
    b2EdgeShape screenBorderShape
;

    
// Create fixtures for the four borders (the border shape is re-used)
    screenBorderShape
.Set(lowerLeftCorner, lowerRightCorner);
    screenBorderBody
->CreateFixture(&screenBorderShape, 0);
    screenBorderShape
.Set(lowerRightCorner, upperRightCorner);
    screenBorderBody
->CreateFixture(&screenBorderShape, 0);
    screenBorderShape
.Set(upperRightCorner, upperLeftCorner);
    screenBorderBody
->CreateFixture(&screenBorderShape, 0);
    screenBorderShape
.Set(upperLeftCorner, lowerLeftCorner);
    screenBorderBody
->CreateFixture(&screenBorderShape, 0);


原创粉丝点击