Sign in to follow this  

Help with mouse joints in Box2D C++

Recommended Posts

I have been working with Box2D for just a couple of days now, I got the basics down and understanding what I have learned, but instead of moving the objects with keyboard input using ApplyForce and Impulse, I want to use a mouse joint to move the object when I am over them and I click the left button, you don't need to tell me how to use the input device or how to locate when the mouse is over the object I have that figured out, I need to know how to create a mouse joint and attach it to the object when I press the left click then I can drag it around the screen (keeping the phyics intact of course, but just for learning I can settle for no physics if it is easier).
//Set the box's center
myBox.sprite.SetCenter(25, 25);

//Create the box shape (polygon) definition
myBox.polyDef.SetAsBox(myBox.sprite.GetCenter().x / RATIO, myBox.sprite.GetCenter().y / RATIO);
myBox.polyDef.friction      = 0.08f;
myBox.polyDef.restitution   = 0.2f;
myBox.polyDef.density      = 0.4f;

//Create the box body definition
myBox.bodyDef.position.Set(600.0f / RATIO, 50.0f / RATIO);
myBox.angle = 0.0f;

//Create the box body
myBox.body =>CreateBody(&myBox.bodyDef);

//Create the box shape

The myBox.sprite is API exclusive so don't pay attention to that if you are confused by that, I just put it there so you can see the objects center as it is relevant to my creating of the object myBox. Now how can I create a mouse joint to that, I have tried the example I saw on the homepage and what I've seen googled and to no luck. Also what should happen when I click the mouse button, if you need more code let me know, ill post more.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this