I am trying to make a box2d b2Body follow my mouse position. This is the code I am using at the moment but it is not performing very well:

deltaPos = mousePos - body->GetPosition();
deltaPos *= k;
body->ApplyForce(deltaPos, b2Vec2(0, 0));

I have tried playing with k but the body either lags too far behind the mouse or flies round and round the mouse like crazy. Should I be using an exponentiation there instead of multiplication? I am scared of applying too much force and breaking the sim (I suppose I could use a max).

Is there a more straightforward way to do this?


1 Answer 1


You can use a Mouse Joint to "attach" the mouse to the body (see http://www.box2d.org/manual.html#_Toc258082974)

If that doesn't work well for whatever reason, the manual suggests using a kinematic body and adjusting its position manually(http://www.box2d.org/manual.html#_Toc258082973)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .