In the Box2D sample example, I modified the "Web" example as follows:
#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
This is far from ideal, but may be the starting point.
source share