【问题标题】:Cannot set "setAsEdge" in Box2D on iPhone无法在 iPhone 上的 Box2D 中设置“setAsEdge”
【发布时间】:2011-07-11 06:58:28
【问题描述】:

这段代码将在 iPhone 屏幕周围创建线条,但底线应该在中间(我需要更改 .y 但我不知道如何更改)。 该怎么做?

有人可以向我解释这些“setAsEdge”方法吗?

// 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.
b2Body* groundBody = world->CreateBody(&groundBodyDef);

// Define the ground box shape.
b2PolygonShape groundBox;       

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

// top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/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/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);

【问题讨论】:

    标签: iphone cocos2d-iphone box2d


    【解决方案1】:
    // bottom
    
    float screenMid = screenSize.height/2;  // Y axis on screen middle
    
            groundBox.SetAsEdge(b2Vec2(0,screenMid/PTM_RATIO),b2Vec2(screenSize.width/PTM_RATIO,screenMid/PTM_RATIO));
    

    这会将您的底线转移到屏幕中间。 SetAsEdge 方法取两个点并从点 1 到点 2 画一条线。在上面的陈述中,第一点是“b2Vec2(0,screenMid/PTM_RATIO)”。其中 0 是 x 轴,screenMid 是第一个点的 y 轴。第二点也是如此。

    您必须将每个点除以 PTM_RATIO 才能将其转换为 box2d 坐标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多