Mouse Joints

Hi folks,

I'm having a little trouble compiling my Box2D code and I can't quite figure out why.

I'm writing in C++ and compiling in Code::Blocks and my code has compiled fine up until now.

Here's my beef:
Code:
b2MouseJointDef dotDragDef;
dotDragDef.body1 = world.GetGroundBody();
dotDragDef.body2 = body;
dotDragDef.target.Set(500.0f,500.0f);
dotDragDef.maxForce = 1000.0f;

b2MouseJoint* dotDrag = world.CreateJoint(&dotDragDef);


When ever I try and compile, the only error I get is:
Code:
error: invalid conversion from `b2Joint*' to `b2MouseJoint*'


I'm fairly sure I've included and linked everything correctly, but I haven't used Code::Blocks or Box2D extensively before.

Mouse Joints don't seem to be particularly well documented, I've looked through the TestBed files that came with the latest Box2D release and I can't see any difference in what I am doing. If anyone can help me with this error or point me to some more documentation, it'd be a great help.

Thanks,
Wombat
That's because CreateJoint returns a b2Joint pointer. You will need to cast it to a b2MouseJoint pointer like this :

Code:
b2MouseJoint* dotDrag = static_cast<b2MouseJoint*>(world.CreateJoint(&dotDragDef));
That's brilliant.

Worked like a charm, thanks Tiblanc.
More Problems Folks,

I can now successfully create a Mouse joint and get it to follow the mouse, however, the joints I am creating don't seem to make the body move towards the mouse. Does MouseJoint not do this automatically?

The body just seems to swing about the point I am holding with the mouse, like a rigid connection.

Here's my code:

Code:
if(mouseDrag)
        {

            b2MouseJointDef dotDragDef;
            dotDragDef.body1 = world.GetGroundBody();
            dotDragDef.body2 = body;
            dotDragDef.timeStep = timeStep;
            dotDragDef.dampingRatio = 1;
            dotDragDef.frequencyHz = 60.0;
            dotDragDef.target.Set(GameApp.GetInput().GetMouseX(), GameApp.GetInput().GetMouseY());
#ifdef TARGET_FLOAT32_IS_FIXED
            dotDragDef.maxForce = (body->GetMass() < 16.0)?
         (1000.0f * body->GetMass()) : float32(16000.0);
#else
            dotDragDef.maxForce = 1000.0f * body->GetMass();
#endif
            if(dragOn)
            {
                world.DestroyJoint(dotDrag);
            }
            else
            {
                dotDrag = static_cast<b2MouseJoint*>(world.CreateJoint(&dotDragDef));
                dragOn = true;
            }

        }
        else if(dragOn)
        {
            world.DestroyJoint(dotDrag);
            dragOn = false;
        }


Am I doing this right? Well obviously not since it's not working.

Can anyone help me out?

And does anyone know where I can get some proper documentation on how to use MouseJoints properly?

Thanks,
Wombat
Mouse joints were not developed for external usage. So you'll have to poke around to figure things out.

The best place to learn about mouse joint usage is in the testbed.

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