var COUNT:int = 16; var STRONG:Boolean = !false; var anchor:b2Vec2 = new b2Vec2 (5, 0); // Create links. Last link will be a very heavy box. var links:Array = []; for (var i:int = 1; i <= COUNT; i++) { var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.position = anchor.Copy(); bodyDef.position.Add(new b2Vec2(0, i * 0.5)); var link:b2Body = m_world.CreateBody(bodyDef); if (i != COUNT) { // A link var circleDef:b2CircleDef = new b2CircleDef(); circleDef.radius = 0.025; circleDef.density = 1.0; link.CreateShape(circleDef); } else { // A box attached at the end of the rope var polygonDef:b2PolygonDef = new b2PolygonDef(); polygonDef.SetAsBox(0.5, 0.3)//, new b2Vec2(0, 0.3), 0); polygonDef.density = 1.0; link.CreateShape(polygonDef); } link.SetMassFromShapes(); links.push (link); } // Connects links together. First link is connected directly to the "ceiling". var lastLink:b2Body = null; for each (link in links) { var distanceJointDef:b2DistanceJointDef = new b2DistanceJointDef(); if (lastLink == null) { distanceJointDef.Initialize(link, m_world.GetGroundBody(), link.GetPosition(), anchor); } else { distanceJointDef.Initialize(link, lastLink, link.GetPosition(), lastLink.GetPosition()); } m_world.CreateJoint(distanceJointDef); lastLink = link; } if (STRONG) { // To make rope stronger, connect each link directly to the ceiling. for each (link in links) { bodyDef = new b2BodyDef(); bodyDef.position = anchor; var attachement:b2Body = m_world.CreateBody(bodyDef); var massData:b2MassData = new b2MassData(); massData.mass = link.GetMass(); massData.I = link.GetInertia(); attachement.SetMass(massData); var revoluteJointDef:b2RevoluteJointDef = new b2RevoluteJointDef(); revoluteJointDef.Initialize(attachement, m_world.GetGroundBody(), attachement.GetPosition()); m_world.CreateJoint(revoluteJointDef); var prismaticJointDef:b2PrismaticJointDef = new b2PrismaticJointDef(); prismaticJointDef.Initialize(attachement, link, link.GetPosition(), new b2Vec2(0, -1)); prismaticJointDef.enableLimit = true; prismaticJointDef.lowerTranslation = 0; prismaticJointDef.upperTranslation = (link.GetPosition().y - anchor.y); m_world.CreateJoint(prismaticJointDef); } }