Theo Jansen walker model

I've created simple walker as a Test for Box2d TestBed.

Here it is:
Code:
   
   float g_x = 0.0f, g_y = 5.0f, g_o = 1;

   struct Line
   {
      Line(b2World * world_, float x1, float y1, float x2, float y2, float w = 0.1f)
         : world(world_), p1(g_x + g_o*x1, g_y + y1), p2(g_x + g_o*x2, g_y + y2)
      {
         b2Vec2 center((p1.x + p2.x)/2, (p1.y + p2.y)/2);
         float length = sqrt((p2.x - p1.x)*(p2.x - p1.x) + (p2.y - p1.y)*(p2.y - p1.y));
         float a = atan2(p2.y - p1.y, p2.x - p1.x);

         b2BoxDef def;
         def.density = 10.0f;
         def.extents.Set(length*0.5f, w);
         def.groupIndex = -1;

         b2BodyDef bdef;
         bdef.position = center;
         bdef.rotation = a;
         
         bdef.AddShape(&def);
         body = world->CreateBody(&bdef);
      }

      b2World * world;
      b2Vec2 p1, p2;
      b2Body * body;
   };

   b2Joint * CreateJoint(b2World * world, b2Body * b1, b2Body * b2, float x, float y)
   {
      b2RevoluteJointDef jd;
      jd.body1 = b1;
      jd.body2 = b2;
      jd.anchorPoint.Set(x, y);
      jd.collideConnected = false;
      return world->CreateJoint(&jd);
   }

   void CreateJoint(Line &l1, Line&l2)
   {
      if(l1.world != l2.world)
         return;

      float x, y;
      float z  = (l1.p2.y-l1.p1.y)*(l2.p1.x-l2.p2.x)-(l2.p1.y-l2.p2.y)*(l1.p2.x-l1.p1.x);
      if(abs(z) < 0.0001)
      {
         float d11 = abs(l1.p1.x - l2.p1.x) + abs(l1.p1.y - l2.p1.y);
         float d12 = abs(l1.p1.x - l2.p2.x) + abs(l1.p1.y - l2.p2.y);
         float d21 = abs(l1.p2.x - l2.p1.x) + abs(l1.p2.y - l2.p1.y);
         float d22 = abs(l1.p2.x - l2.p2.x) + abs(l1.p2.y - l2.p2.y);
         if(d11 < d12 && d11 < d21 && d11 < d22)
         {
            x = (l1.p1.x + l2.p1.x)/2;
            y = (l1.p1.y + l2.p1.y)/2;
         }
         else if(d12 < d21 && d12 < d22)
         {
            x = (l1.p1.x + l2.p2.x)/2;
            y = (l1.p1.y + l2.p2.y)/2;
         }
         else if(d21  < d22)
         {
            x = (l1.p2.x + l2.p1.x)/2;
            y = (l1.p2.y + l2.p1.y)/2;
         }
         else
         {
            x = (l1.p2.x + l2.p2.x)/2;
            y = (l1.p2.y + l2.p2.y)/2;
         }
      }
      else
      {
         float t = ((l2.p1.y-l1.p1.y)*(l2.p1.x-l2.p2.x)-(l2.p1.y-l2.p2.y)*(l2.p1.x-l1.p1.x)) / z;
         x = l1.p1.x + (l1.p2.x - l1.p1.x) * t;
         y = l1.p1.y + (l1.p2.y - l1.p1.y) * t;
      }
      CreateJoint(l1.world, l1.body, l2.body, x, y);
   }

   class MyTestImpl : public Test
   {
   public:
      
      void CreateLeg(float x, float o, b2Body * body, b2Body * wheel)
      {
         float tx = g_x, to = g_o;
         g_x = x;
         g_o = o;
         Line line1(m_world, 0, 0, -1, 4);
         Line line2(m_world, -1, 4, -3, 3.5f);
         Line line3(m_world, 0, 0, -3, 3.5f);
         Line line4(m_world, -1, 4, -1.5f, 6);
         Line line5(m_world, -3, 3.5f, -3.5f, 5.5f);
         Line line6(m_world, -1.5f, 6, -3.5f, 5.5f);
         Line line7(m_world, -2, 8, -1.5f, 6);
         Line line8(m_world, -2, 8, -3.5f, 5.5f);
         Line line9(m_world, -1, 4, 1, 6.5f);
         Line line10(m_world, -2, 8, 1, 6.5f);
         CreateJoint(line1, line2);
         CreateJoint(line1, line3);
         CreateJoint(line1, line4);
         CreateJoint(line1, line9);
         CreateJoint(line2, line3);
         CreateJoint(line2, line5);
         CreateJoint(line2, line9);
         CreateJoint(line4, line6);
         CreateJoint(line4, line7);
         CreateJoint(line5, line6);
         CreateJoint(line5, line8);
         CreateJoint(line7, line8);
         CreateJoint(line7, line10);
         CreateJoint(line9, line10);
         CreateJoint(m_world, line4.body, body, line4.p2.x, line4.p2.y);
         CreateJoint(m_world, line10.body, wheel, 0, g_y + 6.5f);
         CreateJoint(m_world, line9.body, wheel, 0, g_y + 6.5f);

         g_x = tx;
         g_o = to;
      }

      b2Body * body, * wheel, * ground, * fixed;
      b2Joint * motorJoint;

      MyTestImpl()
      {
         {
            b2BoxDef sd;
            sd.type = e_boxShape;
            sd.extents.Set(50.0f, 10.0f);
            sd.friction = 0.3f;
            sd.categoryBits = 0x0001;

            b2BodyDef bd;
            bd.position.Set(0.0f, -10.0f);
            bd.AddShape(&sd);
            ground = m_world->CreateBody(&bd);
            fixed = m_world->CreateBody(&bd);
         }
         {
            b2BoxDef def;
            def.density = 2.0f;
            def.extents.Set(2.5f, 0.5f);
            def.groupIndex = -1;
            b2BodyDef bdef;
            bdef.position.Set(0, 6 + g_y);
            bdef.AddShape(&def);
            body = m_world->CreateBody(&bdef);
         }
         {
            b2BoxDef def;
            def.density = 2.0f;
            def.extents.Set(1.0f, 1.0f);
            def.groupIndex = -1;
            b2BodyDef bdef;
            bdef.position.Set(0, 6 + g_y);
            bdef.AddShape(&def);
            wheel = m_world->CreateBody(&bdef);
         }
         {
            b2RevoluteJointDef jd;
            jd.body1 = wheel;
            jd.body2 = body;
            jd.anchorPoint.Set(0, 6 + g_y);
            jd.collideConnected = false;
            jd.motorSpeed = 1.0f;
            jd.motorTorque = 1000.0f;
            jd.enableMotor = true;
            motorJoint = m_world->CreateJoint(&jd);
         }
         CreateJoint(m_world, fixed, body, 0, 0);
         CreateJoint(m_world, fixed, body, 0, 10);

      }

      void Step(const Settings* settings)
      {
         int r = ((int(wheel->GetRotation() * 180 / 3.1415f) % 360) + 360) % 360;
         DrawString(5, m_textLine, "Rotation = %d", r);
         static bool b1 = false, b2 = false, b3 = false;
         if(!b1 && r == 0)
         {
            b1 = true;
            CreateLeg(-1, 1, body, wheel);
            CreateLeg(1, -1, body, wheel);
         }
         if(!b2 && r == 120)
         {
            b2 = true;
            CreateLeg(-1, 1, body, wheel);
            CreateLeg(1, -1, body, wheel);
         }
         if(!b3 && r == 240)
         {
            b3 = true;
            CreateLeg(-1, 1, body, wheel);
            CreateLeg(1, -1, body, wheel);
         }
         if(b1 && b2 && b3 && fixed)
         {
            m_world->DestroyBody(fixed);
            fixed = 0;
         }
         Test::Step(settings);
      }

   };

Original thread: https://box2d.org/forum/viewtopic.php?t=209