10

I'm using mousejoint to drag bodies in box2d, but it causes inertial delay.

Does it exist any way to drag a body instantaneously?

Ricibald
  • 9,369
  • 7
  • 47
  • 62

2 Answers2

18

The solution is to tune up properties frequencyHz and dampingRatio in your b2MouseJointDef.

For example:

b2MouseJointDef md;
md.body1 = _groundBody;
md.body2 = body;
md.target = p;
md.maxForce = 10000.0f * body->GetMass();
md.dampingRatio = 0;
md.frequencyHz = 100;
_world->CreateJoint(&md);
Ricibald
  • 9,369
  • 7
  • 47
  • 62
1

I'm trying to implement a pong-style game in Processing/Box2d library and I anticipate having the same problem. One thing that comes to mind is to maintain a hidden object in the Box2d world, one that operates with joints the conventional way, and then draw a virtual object that follows the mouse with no frame delay. This might be adequate to fool the user.

On the other hand, Box2d is not a strict physics simulation and allows for some forgiveness in overlapping objects, so it really seems like there should be a way to do this.

Matt Montag
  • 7,105
  • 8
  • 41
  • 47
  • See my answer for the solution! – Ricibald Feb 03 '10 at 11:09
  • Thanks! Works perfectly. I found an example of mousejoint usage in Processing here: http://processing.org/discourse/yabb2/YaBB.pl?num=1213404906/30 and modified it with your suggestion. – Matt Montag Feb 03 '10 at 21:39
  • I can't vote up my own answer! Please vote it up so the answer appears on top! – Ricibald Feb 04 '10 at 11:06
  • I can't either. I need "15 reputation" or something. – Matt Montag Feb 04 '10 at 18:56
  • I was looking for this for a long time. I was trying to move a body by just touching and dragging the particular body (on a touch enabled device). But the way I coded, the inertial delay caused me a lot of problems. Thanks a lot! – Shekhar Oct 30 '12 at 19:49