Object not moving using MouseJoint

Newbie here

I am developing a game in Single Player mode and I need to simulate Computer Movement [FYI: I am using CoCos2d]
I have been trying to move an object using its mouse Joint.

//code Sample (I am testing to see if I can move the "computer" object along the same X axis as my "ball", Y position stays the same)

b2Vec2 b2Position = b2Vec2(ball.position.x/PTM_RATIO,computer.position.y/PTM_RATIO);

b2MouseJointDef md1;
md1.bodyA = _groundBody;
md1.bodyB = computerb2Body;
md1.target = b2Position;
md1.collideConnected = true;
md1.maxForce = 1000.0 * computerb2Body->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md1);
computerb2Body->SetAwake(true);
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;

I do all of this in my Tick Method....

When I used the SetTransform (Cocos2d) method on the same b2Position Vector, it works. When I switched to the MouseJoint method, nothing happens. The same mouseJoint code is used else where to process TouchEvents and it works. But using it in my tick() doesn't do anything.

What am I doing wrong here 🙁 ?
gizmo99 wrote:
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;


You have to keep the mouse joint around.
For how long ?

a Mouse Joint is created on every tick, won't this be bad? I could potentially have a million mouse joints in the game...


When can I actually destroy it
Think of joints like bodies. Do you create and destroy the same body every tick? Does your game create and destroy the same actor every tick?
Thanks Ein for answering my question, I really appreciate it.

I commented out

//_world->DestroyJoint(_mouseJoint);
//_mouseJoint = NULL;

But the object still isn't moving.

What can you recommend me do to debug this problem ?
So Box2D doesn't actually know where your mouse is. You have to call b2MouseJoint::SetTarget each time the mouse moves (each time step). You also have to make sure the body is awake.
Hi Erin

I now have the following

Code:
   b2Vec2 b2Position = b2Vec2(ball.position.x/PTM_RATIO,ball.position.y/PTM_RATIO);
   
   b2MouseJointDef md1;
   
   md1.bodyA = _groundBody;
   md1.bodyB = computer_paddle_b2Body;
   md1.target = b2Position;
   md1.collideConnected = true;
   md1.maxForce = 9999.0 * computer_paddle_b2Body->GetMass();      
   _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md1);
   _mouseJoint->SetTarget(b2Position);
         computer_paddle_b2Body->SetAwake(true);


However, the object is still not moving. Am I doing something terribly wrong here ?
Quote:
I now have the following

Code:
   b2Vec2 b2Position = b2Vec2(ball.position.x/PTM_RATIO,ball.position.y/PTM_RATIO);
   
   b2MouseJointDef md1;
   
   md1.bodyA = _groundBody;
   md1.bodyB = computer_paddle_b2Body;
   md1.target = b2Position;
   md1.collideConnected = true;
   md1.maxForce = 9999.0 * computer_paddle_b2Body->GetMass();      
   _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md1);
   _mouseJoint->SetTarget(b2Position);
         computer_paddle_b2Body->SetAwake(true);


However, the object is still not moving. Am I doing something terribly wrong here ?

I've never used the mouse joint, but I'll go ahead and venture a guess here.

What Erin is trying to tell you (I think) is that you need to create the mouse joint once, but update its target position every tick.

If the above code is in a 'startup' function of some sort, then you're assigning the mouse joint a single target position that never changes (in which case there'd be no reason to expect it to respond dynamically to subsequent mouse movements).

If the above code is in your 'tick' function, then you're creating a new mouse joint every tick - the 'millions of mouse joints' you referred to earlier. Clearly this is also somewhat nonsensical.

The correct solution (I believe) is to create the mouse joint in your 'startup' function, and set its target position in the 'tick' function. In this respect the mouse joint is really no different than any other body or joint whose properties are modified dynamically (as Erin alluded to earlier).
Thanks for the reply!

The above code exists in TICK, where b2Position is updated on every tick hence I expected the Object to move....
But I see what you mean, I will see if moving the Joint Creation code in to init() and calling SetPosition in the Tick method solves my problem
Also, you seem to be doing SetTarget with a position obtained from the ball.

I did not use mouse joints yet, but from what Erin said:
Quote:
Box2D doesn't actually know where your mouse is. You have to call b2MouseJoint::SetTarget each time the mouse moves

I'd guess that you have to SetTarget with the position of the mouse cursor.

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