Making mouseJoints rigid

Hi, I've been trying combinations of friction, density, frequencyHz and damping to reduce the inherent 'springiness' of mousejoints. I've also looked at the source code. Is there any way to eliminate the 'lazy following' effect of the mousejoint?

I'm creating an iphone game which can become fast-paced, and I need the player pieces to tightly follow the touch point on-screen.

I appreciate your time and help - thanks!
There's a heirarchy of ways to do this. The "stronger" ones are more responsive, but they ignore more of the physics.
* If you use SetXForm, it'll always be in the right place, to the point of glitching through walls.
* If you use SetLinearVelocity (setting the velocity such that it'll get to the destination in one frame), then it won't accidentally penetrate stuff, but it will act as if it is incredibly powerful, and push heavy objects out the way with ease.
* If you use the mouse joint, you suffer none of these issues, but it can be fiddly to get it responsive enough.
I've been dealing with a situation where I need box2d objects to respond exactly like a normal drag/drop does. I found mouseJoints incredibly frustrating to deal with so instead I just put startDrag/ stopDrag functions in event listeners. Then when I update the locations of all my objects I do this.

Code:
public function update():void
{
   _world.Step(TIMESTEP, ITERATIONS);

   for (var body:b2Body=_world.m_bodyList; body; body=body.m_next)
   {
     if (body.GetUserData() is Sprite)
   {
      if (BODY IS BEING DRAGGED) //body should be static while being dragged
      {
         var vec:b2Vec2=new b2Vec2()
         vec.x=p2m(body.GetUserData().x); //p2m = pixels to meters function
         vec.y=p2m(body.GetUserData().y);
         body.SetXForm(vec, body.GetUserData().rotation / 180 * Math.PI);
      else
      {
         body.GetUserData().x=m2p(body.GetPosition().x); //m2p = meters to pixels function
         body.GetUserData().y=m2p(body.GetPosition().y);
         body.GetUserData().rotation=body.GetAngle() * (180 / Math.PI);
      }
}


I got this to work easily, although with debug drawing on I can see there's a tiny lag between the location of the sprite and the box2d object. Also this will cause the object you're dragging to teleport through anything. These issues don't matter much in my game, but if anyone knows if there's a better way than what I did I'd be interested.
(cross-posting with cocos2d forum)

@JMulligan, @Boris, thank you for sharing your source code solution and explaining the options so clearly. My project is interesting in that it needs strict touch matching and also needs to respond to physics while dragging. I'm studying the Box2D source, in hope of modifying it, although I'm new to Obj-c.


Maybe it's possible to alter or combine some functions of mouseJoints and prismatic joints code directly to allow both axis of movement without the soft joint calculations? For example, an option to set mousejoint to '.isRigid', analogous to setting .fixedRotation in a body, would be fantastic.

I also have Contact listener working with my game code, perhaps preSolve can be used?
(cross-posted from iphone cocos forum)

It's becoming clear that mouseJoint is meant to apply force to an object, to create interesting reactions in the testbed, the effect like a weight on a rubber band. So it's not really a good fit for following a player's touchpoint on the screen and is difficult to tweak for strict touch following - although mousejoints are great for otherwise interacting with physics objects as it imparts momentum to contact objects.

Reading Apress's iPhone Cool Projects the author mentions a cpMouse function in Chipmunk, where momentum is transfered to the object when a player slides it around the screen.

Does Box2D have something similar, perhaps SetTransform? Something that will allow an object to work fully in the physics world, under player control?
You want something which is perfectly rigid. No amount of momentum transfer is going to do that.
Use the velocities approach (i.e. set the velocity so that the point that was under the mouse last frame will be under the mouse this frame). That will accurately drag things around, and will maintain its direction upon release. As I said, the only catch is that you can push very heavy objects out the way. This is necessary, as you cannot have both "always follows the mouse" and "can be pushed out the way by heavy objects". That's a contradiction in terms.

If you've seen an app which behaves otherwise, it is likely changing what dragging does when a collision happens, or the like.

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