Please add a dist joint to this code

Hey All,

Can someone please add a dist joint between circleDefCenter and all the circ
Just can't find a tutor to show me how.

Code:
package
{

   import flash.display.Sprite;
   import flash.events.Event;
   // Classes used in this example
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;
   import Box2D.Dynamics.Joints.*;
   import flash.events.MouseEvent;

   public class mousejoint extends Sprite
   {

      var body:b2Body;
      var mouseJoint:b2MouseJoint;

      public function mousejoint()
      {
         // Add event for main loop
         addEventListener(Event.ENTER_FRAME, Update, false, 0, true);



         // Creat world AABB
         var worldAABB:b2AABB = new b2AABB();
         worldAABB.lowerBound.Set(-100.0, -100.0);
         worldAABB.upperBound.Set(100.0, 100.0);

         // Define the gravity vector
         var gravity:b2Vec2 = new b2Vec2(0.0, 0.0);

         // Allow bodies to sleep
         var doSleep:Boolean = true;

         // Construct a world object
         m_world = new b2World(worldAABB, gravity, doSleep);

         // Vars used to create bodies
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDefCenter:b2CircleDef;
         var circleDef:b2CircleDef;
         var ObjArray:Array = new Array;


         // Add ground body
         bodyDef = new b2BodyDef();
         //bodyDef.position.Set(15, 19);
         bodyDef.position.Set(10, 21);
         //bodyDef.angle = 0.1;
         boxDef = new b2PolygonDef();
         boxDef.SetAsBox(30, 1);
         boxDef.friction = 0.3;
         boxDef.density = 0;// static bodies require zero density
         /*circleDef = new b2CircleDef();
                  circleDef.radius = 10;
                  circleDef.restitution = 0.2*/
         // Add sprite to body userData
         bodyDef.userData = new PhysGround();
         //bodyDef.userData = new PhysCircle();
         bodyDef.userData.width = 30 * 2 * 30;
         bodyDef.userData.height = 30 * 2 * 1;
         addChild(bodyDef.userData);
         body = m_world.CreateBody(bodyDef);
         body.CreateShape(boxDef);

         body.SetMassFromShapes();
         // Add some objects


         bodyDef = new b2BodyDef();
         bodyDef.position.x = 10;
         bodyDef.position.y = 10;
         var rX:Number = 1;
         var rY:Number = Math.random() + 0.5;

         circleDefCenter = new b2CircleDef();
         circleDefCenter.radius = 2;
         circleDefCenter.friction = 0.5;
         circleDefCenter.restitution = 0.2;
         circleDefCenter.density = 0;
         bodyDef.userData = new PhysCircle();
         bodyDef.userData.width = rX * 2 * 30 * 2;
         bodyDef.userData.height = rX * 2 * 30 * 2;
         body = m_world.CreateBody(bodyDef);
         body.CreateShape(circleDefCenter);
         body.SetMassFromShapes();
         addChild(bodyDef.userData);
         ObjArray.push(bodyDef.userData);
         
         for (var i:int = 1; i < 9; i++)
         {

            bodyDef = new b2BodyDef();
            var angle= i * Math.PI * 2 / 8;
            bodyDef.position.x = Math.cos(angle) * 5 + 10;
            bodyDef.position.y = Math.sin(angle) * 5 + 10;

            circleDef = new b2CircleDef();
            circleDef.radius = 1;
            circleDef.density = .1;
            circleDef.friction = 0.5;
            circleDef.restitution = .2;
            bodyDef.userData = new PhysCircle();
            bodyDef.userData.width = rX * 2 * 30;
            bodyDef.userData.height = rX * 2 * 30;
            body = m_world.CreateBody(bodyDef);
            body.CreateShape(circleDef);
            body.SetMassFromShapes();
            addChild(bodyDef.userData);
            ObjArray.push(bodyDef.userData);

         }
         stage.addEventListener(MouseEvent.MOUSE_DOWN, createMouse);
         stage.addEventListener(MouseEvent.MOUSE_UP, destroyMouse);
      }
      public function createMouse(evt:MouseEvent):void
      {
         var body:b2Body = GetBodyAtMouse();
         if (body)
         {
            var mouseJointDef:b2MouseJointDef = new b2MouseJointDef;
            mouseJointDef.body1 = m_world.GetGroundBody();
            mouseJointDef.body2 = body;
            mouseJointDef.target.Set(mouseX/30, mouseY/30);
            mouseJointDef.maxForce = 30000;
            mouseJointDef.timeStep = m_timeStep;
            mouseJoint = m_world.CreateJoint(mouseJointDef) as b2MouseJoint;
         }
      }
      public function destroyMouse(evt:MouseEvent):void
      {
         if (mouseJoint)
         {
            m_world.DestroyJoint(mouseJoint);
            mouseJoint = null;
         }
      }
      private var mousePVec:b2Vec2 = new b2Vec2();
      public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body
      {
         var mouseXWorldPhys = (mouseX)/30;
         var mouseYWorldPhys = (mouseY)/30;
         // Make a small box.
         mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
         var aabb:b2AABB = new b2AABB();
         aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
         aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);

         // Query the world for overlapping shapes.
         var k_maxCount:int = 10;
         var shapes:Array = new Array();
         var count:int = m_world.Query(aabb, shapes, k_maxCount);
         var body:b2Body = null;
         for (var i:int = 0; i < count; ++i)
         {
            if (shapes[i].GetBody().IsStatic() == false || includeStatic)
            {
               var tShape:b2Shape = shapes[i] as b2Shape;
               var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
               if (inside)
               {
                  body = tShape.GetBody();
                  break;
               }
            }
         }
         return body;
      }
      public function Update(e:Event):void
      {

         m_world.Step(m_timeStep, m_iterations);

         if (mouseJoint)
         {
            var mouseXWorldPhys = mouseX/30;
            var mouseYWorldPhys = mouseY/30;
            var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
            mouseJoint.SetTarget(p2);
         }
         // Go through body list and update sprite positions/rotations
         for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next)
         {
            if (bb.m_userData is Sprite)
            {
               bb.m_userData.x = bb.GetPosition().x * 30;
               bb.m_userData.y = bb.GetPosition().y * 30;
               bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
            }
         }
      }
      public var m_world:b2World;
      public var m_iterations:int = 10;
      public var m_timeStep:Number = 1.0/30.0;

   }

}
Hey, I've only been working with this engine for a couple of days. So don't take my practices as the best way to do things.

Anyway, looking through your code, the worst problem I had was finding your original large circle to reference my joint back to.
So, I went back, and made a new b2Body of your bigCircle:
Code:
bodyDef.userData.height = rX * 2 * 30 * 2;

// Changed
var bigCircle:b2Body = m_world.CreateBody(bodyDef);
bigCircle.CreateShape(circleDefCenter);
bigCircle.SetMassFromShapes();

addChild(bodyDef.userData);


Previously, you re-written over the b2Body (which is fine to do) unless you want to re-reference it easily (which we did).

Then, I made a definition of a distance joint:
Code:
ObjArray.push(bodyDef.userData);

// Changed
var djd:b2DistanceJointDef = new b2DistanceJointDef();

for (var i:int = 1; i < 9; i++)


Finally, after you declare you new b2Bodys and add them to your m_world I Initialised the joint and added it to your world:
Code:
djd.Initialize(body, bigCircle, body.GetWorldCenter(), bigCircle.GetWorldCenter());
m_world.CreateJoint(djd);

To Initialize the joint, simply reference the 2 b2Bodys you want to join and then the two points of exactly where you want to have the joint placed on the body. Here with circles it seems fine just to join them from their central points.

That should now make it work. If you have problems in the future, don't forget to consult the AS3 manual and the testBed examples supplied with the source code.

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