Can't set a body's position and use a b2MouseJoint? (2.1a)

If I create a body and join it to a b2MouseJoint, it tracks with the mouse just fine. However, if I change the body's position at any time, this value becomes an offset which throws off my mouse joint. However far I move it from (0, 0) is the same distance it stays from my mouse cursor.

Am I doing this wrong, or is this a bug in Box2DFlashAS3 2.1a?

Here is the code which creates the joint:

Code:
var jointDefinition:b2MouseJointDef = new b2MouseJointDef ();
jointDefinition.bodyA = Physics.GetGroundBody ();
jointDefinition.bodyB = currentBody;
jointDefinition.maxForce = 300.0 * currentBody.GetMass ();
mouseJoint = Physics.CreateJoint (jointDefinition) as b2MouseJoint;
currentBody.SetAwake (true);


Here is the code which updates the position when dragging:

Code:
mouseJoint.SetTarget (new b2Vec2 (mouseX * PHYSICS_SCALE, mouseY * PHYSICS_SCALE));


And here is an example of setting the position (this would cause the object to be 100 pixels away from the mouse):

Code:
currentBody.SetPosition (new b2Vec2 (0, 100));


Thank you in advance!
I didn't see this in the demo, but my workaround was to save an offset before dragging, and apply it towards my target point.
Hi!

I have a short question. I tried to set a new position for my body with this code, but it didn't worked:
Code:
trace(body.GetPosition().x);
body.SetPosition(new b2Vec2(newX, newY));
trace(body.GetPosition().x);


The position of the body is still the same.
Any ideas why?

Greetings,
Pipo
Here's the basic principle of what I did:

Here's some code from when I start dragging:

Code:
currentBody.SetType (b2Body.b2_dynamicBody);
draggingOffset = new b2Vec2 (position.x * PHYSICS_SCALE, - position.y * PHYSICS_SCALE);

var mouseJointDefinition:b2MouseJointDef = new b2MouseJointDef ();
mouseJointDefinition.bodyA = Physics.GetGroundBody ();
mouseJointDefinition.bodyB = currentBody;
mouseJointDefinition.maxForce = 300.0 * currentBody.GetMass ();
mouseJoint = Physics.CreateJoint (mouseJointDefinition) as b2MouseJoint;
currentBody.SetAwake (true);


position is a Number3D object, which represents the mouse's current coordinates when I start dragging. So you can see that I'm saving an offset based on the starting location, I'm changing the dragging body to a dynamic body (instead of static), and just in case, I'm forcing it to be awake, though I'm not sure that's necessary.

My code is actually a lot more complicated than this, but I think this is the relevent portion which runs while I am dragging:

Code:
var target:b2Vec2 = new b2Vec2 (position.x * PHYSICS_SCALE, - position.y * PHYSICS_SCALE);
target.Subtract (draggingOffset);
mouseJoint.SetTarget (target);


So I'm getting the new position, but removing the original offset before I apply this new target to the mouse joint. I can't remember at this moment while copying and pasting this code, but I remember that the mouse joint worked differently than I expected it to. I think I would have thought that you would have set the target of where you want the joint to go, but actually I had to apply the target as the difference from its original position. Maybe it's because it's joined to the world center? Not sure, but at least its working 🙂

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