2012-02-07 65 views
4

我正在寫一個遊戲,我需要組成一根繩子。我通過b2RopeJoint製作了它,但在那裏我沒有找到讓它變得有彈性的機會。 然後,我尋找b2DistanceJoint,一切都很酷的彈性,但我找不到一個限制只能到最大距離(沒有最小的一個)的能力。Box2d彈性繩索接頭

我該怎麼辦?

+0

相關https://gamedev.stackexchange.com/questions/47783/how-to-create-elastic-rope-rubber-band-in-box2d – 2017-12-10 19:49:20

回答

5

試試這個。

-(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 
} 
+0

我需要橡膠)這是繩子)抱歉,如果我不好解釋 – Sk0prion 2012-02-11 12:20:11

+0

如果您需要橡膠,請更改上一個物體的類型(從動態到靜態) – Sinba 2012-02-12 11:43:23

0

您可以同時使用繩索接頭和距離接頭。使繩索接頭的距離比距離接頭長一點。

+0

...?看看辛巴的回答。 – iforce2d 2013-04-20 19:20:04

0

從Box2D的測試平臺例子,我修改了「網絡」舉例如下:

#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 

它遠非完美,但可能是一個開始點。