【问题标题】:Box2d elastic rope jointBox2d 弹性绳索接头
【发布时间】:2012-02-07 06:29:37
【问题描述】:

我正在写一个游戏,我需要编一根绳子。我是由 b2RopeJoint 制作的,但我还没有找到让它变得有弹性的机会。 然后我找了 b2DistanceJoint,一切都很酷,有弹性,但我找不到只限制最大距离(没有最小距离)的能力。

我该怎么做?

【问题讨论】:

标签: box2d-iphone


【解决方案1】:

试试这个。

-(void) CreateElasticRope {
    //=======Params
    // Position and size
    b2Vec2 lastPos = b2Vec2(4,4); //set position first body
    float widthBody = 0.35;
    float heightBody = 0.1;
    // Body params
    float density = 0.05;
    float restitution = 0.5;
    float friction = 0.5;
    // Distance joint
    float dampingRatio = 0.0;
    float frequencyHz = 0;
    // Rope joint
    float kMaxWidth = 1.1;
    // Bodies
    int countBodyInChain = 15;
    b2Body* prevBody;

    //========Create bodies and joints
    for (int k = 0; k < countBodyInChain; k++) {
        b2BodyDef bodyDef;
        if(k==0 ) bodyDef.type = b2_staticBody; //first body is static
        else bodyDef.type = b2_dynamicBody;
        bodyDef.position = lastPos;
        lastPos += b2Vec2(2*widthBody, 0); //modify b2Vect for next body
        bodyDef.fixedRotation = YES;
        b2Body* body = world->CreateBody(&bodyDef);

        b2PolygonShape distBodyBox; 
        distBodyBox.SetAsBox(widthBody, heightBody);
        b2FixtureDef fixDef;
        fixDef.density = density;
        fixDef.restitution = restitution;
        fixDef.friction = friction;
        fixDef.shape = &distBodyBox;
        body->CreateFixture(&fixDef);
        body->SetHealth(9999999);
        body->SetLinearDamping(0.0005f);

        if(k>0) {
            //Create distance joint
            b2DistanceJointDef distJDef;
            b2Vec2 anchor1 = prevBody->GetWorldCenter();
            b2Vec2 anchor2 = body->GetWorldCenter();
            distJDef.Initialize(prevBody, body, anchor1, anchor2);
            distJDef.collideConnected = false;
            distJDef.dampingRatio = dampingRatio;
            distJDef.frequencyHz = frequencyHz;
            world->CreateJoint(&distJDef);

            //Create rope joint
            b2RopeJointDef rDef;
            rDef.maxLength = (body->GetPosition() - prevBody->GetPosition()).Length() * kMaxWidth;
            rDef.localAnchorA = rDef.localAnchorB = b2Vec2_zero;
            rDef.bodyA = prevBody;
            rDef.bodyB = body;
            world->CreateJoint(&rDef);

        } //if k>0
        prevBody = body;
    } //for 
}

【讨论】:

  • 我需要一个橡胶)这是绳子)如果我解释不好,对不起
  • 如果您需要橡胶,请更改最后一个主体的类型(从动态到静态)
【解决方案2】:

这里有一个 2.2 风格的ropejoint 实现:https://github.com/aaronfarr/box2Dweb

【讨论】:

    【解决方案3】:

    您可以同时使用绳索接头和距离接头。让绳索接头的距离比距离接头长一点。

    【讨论】:

    • ...?看看辛巴的回答。
    【解决方案4】:

    从 Box2D 测试台示例中,我将“Web”示例修改如下:

    #ifndef ELASTICROPE_H
    #define ELASTICROPE_H
    
    #define NUM_JOINTS  7
    #define NUM_LINKS   8
    
    
    class ElasticRope : public Test
    {
    
    public:
        ElasticRope()
        {
            b2Body* ground = NULL;
            {
                b2BodyDef bd;
                ground = m_world->CreateBody(&bd);
    
                b2EdgeShape shape;
                shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
                ground->CreateFixture(&shape, 0.0f);
            }
    
            {
                b2CircleShape shape;
                shape.m_radius = 0.8f;
    
                // create bodies
                for (int b = 0; b < NUM_LINKS; b++) {
                    b2BodyDef bd;
                    bd.type = b2_dynamicBody;
    
                    bd.position.Set(5.0f, NUM_LINKS-b);
                    m_bodies[b] = m_world->CreateBody(&bd);
                    m_bodies[b]->CreateFixture(&shape, 5.0f);
                }
    
                for (int j = 0; j < NUM_JOINTS; j++) {
                    b2DistanceJointDef jd;
                    b2Vec2 p1, p2, d;
                    jd.frequencyHz = 5.0f;
                    jd.dampingRatio = 1.0f;
    
                    jd.bodyA = m_bodies[j];
                    jd.bodyB = m_bodies[j+1];
    
                    m_joints[j] = m_world->CreateJoint(&jd);
                }
            }
        }
    
        void Step(Settings* settings)
        {
            Test::Step(settings);
            m_debugDraw.DrawString(5, m_textLine, "This demonstrates an elastic rope.");
            m_textLine += DRAW_STRING_NEW_LINE;
        }
    
        void JointDestroyed(b2Joint* joint)
        {
            for (int32 i = 0; i < 8; ++i)
            {
                if (m_joints[i] == joint)
                {
                    m_joints[i] = NULL;
                    break;
                }
            }
        }
    
        static Test* Create()
        {
            return new ElasticRope;
        }
    
        b2Body* m_bodies[NUM_LINKS];
        b2Joint* m_joints[NUM_JOINTS];
    };
    
    #endif // ELASTICROPE_H
    

    它远非完美,但可能是一个起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多