【问题标题】:Box2D:Give different fixture propertiesBox2D:赋予不同的夹具属性
【发布时间】:2013-11-19 18:13:05
【问题描述】:

我的游戏中有两个不同的实体,我想为每个实体设置不同的夹具属性。对于brick,我不希望它弹跳,即brickFixture.restitution=0.0,但它确实具有弹跳效果。是因为我们不能给不同的身体提供不同的夹具吗?

b2Body* brick;
b2Body* circle;

而我他们的body和fixture定义如下:

对于brick

b2Body* addBrick(int x,int y,int w,int h,bool dyn=true)
{
    b2BodyDef bodydef;  
    bodydef.position.Set(x*P2M,y*P2M);   //Setting body position
    if(dyn)
    {
            bodydef.type=b2_dynamicBody;  // dynamic body means body will move

    }

    brick=world->CreateBody(&bodydef);        //Creating box2D body

    b2PolygonShape shape;            //Creating shape object
    shape.SetAsBox(P2M*w,P2M*h);

    ////////////// Adding Fixtures(mass, density etc) //////////////


    brickFixture.shape=&shape;
    brickFixture.density=1.0;

    brick->CreateFixture(&brickFixture);
    return brick;
}

circle

b2Body* addCircle(int x, int y,int r, bool dyn = true)
{
    b2BodyDef bodydef;  
    bodydef.position.Set(x*P2M,y*P2M);   //Setting body position
    if(dyn)
    {
            bodydef.type=b2_dynamicBody;  // dynamic body means body will move

    }

    circle =world->CreateBody(&bodydef);        //Creating box2D body

    b2CircleShape shape; //Creating shape object
    shape.m_radius = r*P2M;
    shape.m_p.Set(0,0);



    ////////////// Adding Fixtures(mass, density etc) //////////////

   circleFixture.shape=&shape;
    circleFixture.density=1.0;
    circleFixture.restitution = 0.7;
    circle->CreateFixture(&circleFixture);
    return circle;
}

【问题讨论】:

  • 反弹取决于所涉及的两个灯具的恢复设置。要获得无反弹碰撞,两个固定装置都需要归零。

标签: opengl box2d game-physics fixtures


【解决方案1】:

我们可以给不同的身体提供不同的夹具。如果您可以将主体设为静态,则将其设为静态,因为默认情况下主体是动态的。你手动设置恢复原状。可能对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2021-04-14
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多