b2GravityController for attractive force between two bodies?

Hi,
I try to use the Box2dFlash 2.1a b2Controllers for the following scenario:

In my world, I have two types of physical objects (which are b2bodies with a b2circleShape) - "ParentCircles" and "ChildCircles". They are arranged randomly in a 0 gravity world.

Now, I want ParentCircles to attract their ChildCircles, so that ChildCircles arrange around the ParentCircles. To create attractive forces between ParentCircles and ChildCircles, I attach a b2GravityController to the world for each ChildCircle:

Code:
for each (var childCircle:ChildCircle in allChildCircles) {
//create controller:
var controller:b2Controller =  world.AddController(new b2GravityController());
var gravityController:b2GravityController = controller as b2GravityController;
// set gravity:
gravityController.G = 50;
//add the bodies that should attract each other
gravityController.AddBody(childCircle.body);
gravityController.AddBody(parentCircle.body);
}


This does obviously not what I want. The circles do move around, but they don't seem to attract. I wonder if I misunderstand the b2GravityController or the use of b2Controllers in general.

- Do I have to call gravityController.Step(WHATGOESHERE); at each world step?

Thanks for your help,
M
Looks ok to me. Step is called for you by the world. Try upping your value of G massively to see if you can discern any affect.
Hi Boris,
I increased gravity, but it did not help. I added my code and a zipped .swf to show you how my bodies react.

- TesterMain.as adds 1 parentObject and 2 childObjects to a world
- TesterObj.as creates an object and adds a gravityController, if is a ChildObject

Thanks for your help!
M

Code:
package {

   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.display.*;
   import flash.utils.Dictionary;
   
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;

   public class TesterMain extends MovieClip {

      private var allTesterObjects:Dictionary;
      private var world:b2World;
      
   
      public function TesterMain() {

         //Dictionary to store all TesterObjects
         allTesterObjects=new Dictionary();
         
         // Define the gravity vector
         var gravity:b2Vec2 = new b2Vec2(0.0,0.0);
         
         // Create b2world object
         world = new b2World(gravity, true);
         
         //start to update world
         addEventListener(Event.ENTER_FRAME, updateWorld, false, 0, true);
         
         // Create a test ParentObject (white circle)
         var parentTesterObj:TesterObj=new TesterObj(world, null);
         addChild(parentTesterObj);
         allTesterObjects["P"] = parentTesterObj;
         
         
         // Create three test ChildObjects (green circle)
         var objNames:Array = new Array("a","b","c");
         for each (var objName:String in objNames) {
            var obj:TesterObj=new TesterObj(world, parentTesterObj);
            addChild(obj);
            allTesterObjects[objName] = obj;
         }
      }

   public function updateWorld(e:Event):void{
         
      var m_velocityIterations:int = 10;
      var m_positionIterations:int = 10;
      var m_timeStep:Number = 1.0/30.0;

      world.Step(m_timeStep,m_velocityIterations,m_positionIterations);

      for each (var obj:TesterObj in allTesterObjects) {
            obj.updatePosition();                           
      }
      
   }
}
}


package {

   import flash.display.Sprite;
   import flash.display.Graphics;
   import flash.display.*;
   
   import Box2D.Dynamics.Controllers.*;
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;
   
   
   public dynamic class TesterObj extends Sprite {
      
      public var body:b2Body;
            
      public function TesterObj(world:b2World, parentObj:TesterObj ){
                     
         var size = 40;
         
         //give Object a random initial position around the center of mc
         var xpos=475+Math.random()*20;
         var ypos=70+Math.random()*10;
         this.x=xpos;
         this.y=ypos;
         
         //Create b2Body:
         var bodyDef:b2BodyDef = new b2BodyDef();
         bodyDef.type = b2Body.b2_dynamicBody;
         bodyDef.position.Set(xpos, ypos);
         //bodyDef.linearDamping=1.0;
         //bodyDef.angularDamping=1.0;
         body=world.CreateBody(bodyDef);
         
         //Create b2Shape and b2Fixture
         var circleShape:b2CircleShape = new b2CircleShape(size/2);
         var fixture:b2Fixture = body.CreateFixture2(circleShape, 1.0);      
         
         //Create Visual Form (white Circle):
         var circle:Sprite = new Sprite();
         circle.graphics.beginFill( 0xffffff, 1 );
         //green, if this is a ChildObject
         if(parentObj!=null){
            circle.graphics.beginFill( 0xffff00, 1 );
         }
         circle.graphics.drawEllipse(-size/2, -size/2, size, size);
         // Add Sprite to Parent
         this.addChild(circle);
         
         
         //Create gravitation to ParentObj, if this is a ChildObject
         if(parentObj!=null){
            
            // Add GravityController,
            var a_controller:b2Controller =  world.AddController(new b2GravityController());
            var gravityController:b2GravityController = a_controller as b2GravityController;
            gravityController.G = 5;
            //gravityController.invSqr = false;
            
            // Add Gravity from ChildObj to ParentObj
            gravityController.AddBody(body);            
            gravityController.AddBody(parentObj.body);
            }
         
      
      }
      
      
      /*
      * Update Position. Called at every Timestep of b2World
      */
      public function updatePosition():void {
         
         // Update position
         this.x=body.GetPosition().x;
         this.y=body.GetPosition().y;
         
      }

}
}
🙄 problem solved.

I didn't realize that I have to call world.ClearForces(); after each timeStep...

M

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