Radial gravity

many apologies if this replicates a previous question - i have looked but nothing seems to address quite what my problem is.

What I'd like to do is set up a point within a world that attracts all other bodies. I've set the world gravity to zero and then variously experimented with ApplyForce and ApplyImpulse but get nothing but errors.

Tried following the example here:

viewtopic.php?f=8&t=1844&hilit=gravity

to no avail.

CODE:

body_def = new b2BodyDef();
shape_def = new b2BoxDef();
shape_def.extents.Set(51,51);
shape_def.friction = 0.2;
shape_def.density = 0.5;
shape_def.restitution = 0.6;
body_def.position.Set(Math.random()*900+50,0);
body_def.AddShape(shape_def);
body_def.userData = new crate();
body_def.userData.name = 'circle';
body_def.userData.width = shape_def.extents.x * 2;
body_def.userData.height = shape_def.extents.y * 2;
addChild(body_def.userData);
the_world.CreateBody(body_def);
body_def.ApplyImpulse(new b2Vec2(6, 4));


which generates the following ERROR:

1061: Call to a possibly undefined method ApplyImpulse through a reference with static type Box2D.Dynamics:b2BodyDef.

Can anyone point me in the right direction as far as correct syntax goes?
You may be using older docs than the Box2D version you have.
ApplyImpulse has been changed to ApplyLinearImpulse and ApplyAngularImpulse.
thanks for the heads up on the changed functions, though I get the same error when I use ApplyForce...which I am pretty sure has not been deprecated.

Here is the syntax I am using, does it look ok?

var force = b2Vec2(8.0, 10.0);
var point = b2Vec2(20.0, 22.0);

body_def.ApplyForce(force,point);

thanks in advance
sorry I should have looked a bit more carefully... you are calling these functions on the body definition, when you should be calling them on the body that was created from the definition.
I don't use the flash port myself, but I'm expecting the_world.CreateBody(body_def) should return something...
As irresistible force said, try this:
Code:
var bodyAsSuch:b2Body;

body_def = new b2BodyDef();
shape_def = new b2BoxDef();
//...All your code...
bodyAsSuch = the_world.CreateBody(body_def);
bodyAsSuch.ApplyImpulse(new b2Vec2(6, 4));
thanks for the input coyote and IF. Some of the errors were caused by me using an old version of the code (fantastic waste of time that was) but I still have the same problem with creating a radial gravitational pull - or rather all bodies in the world being pulled toward a single point.

I've had a good look around the forum but cannot quite get to the solution. At the moment I create a number of bodies and apply this to them all:

Code:
       
var force:b2Vec2 = new b2Vec2(10, 10);
var point:b2Vec2 = new b2Vec2(300,300);
myBody.ApplyForce(force, point);


My reasoning is that force is the speed of which they will be pulled toward the object and point is the position of that object within the world. Does not seem to work though as all objects just spin off away from this point.

I'm sure this has been cracked...care to share if you've managed it?
Notice the parameter you are using for 'force' has two components so it is not simply a speed, it is a direction, eg. (0,1) is straight up, (1,0) is to the right, etc. This is the direction the force will be applied in, so (10,10) will be up and to the right.

The parameter for 'point' is a point in the world at which the force will be applied. If you apply it right at the center of mass of a body, the body will move perfectly in the direction of 'force' with no spinning. If you offset it from the body's center, some rotation will be caused, which is what you're seeing. Most commonly you want to apply a force at a certain position on the body rather than using a global coordinate, so you would use GetWorldPoint to convert a point in local body coordinates before passing it to ApplyLinearForce.

See this for more details: http://www.iforce2d.net/b2dtut/forces

As for your goal of pulling everything to a central point, you will have to apply a different force to each object, since the direction they need to move depends on where they are relative to this central point. You can get the direction to move quite simply:
Code:
b2Vec2 dir = centralPoint - body->GetWorldCenter();
GetWorldCenter returns the center of mass of the body. If you use this dir vector as the force to apply you will find that more force is applied to objects further from the central point. Most likely you will want to adjust the magnitude of the vector somehow. For example if you wanted the force to be stronger for objects closer to the central point (as is the case with radial gravity), you could do something like this:
Code:
b2Vec2 dir = centralPoint - body->GetWorldCenter();
float distanceToCenter = dir.Length();
dir.Normalize(); //make dir 1 unit in length
float forceMagnitude = someForceConstant * (1 / distanceToCenter);
body->ApplyLinearForce( forceMagnitude * dir, body->GetWorldCenter() ); //apply force at center of mass

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