Rigid joint connector

I'm attempting to "connect" two bodies together without destroying one (due to the way my renderer works, it would be too much housekeeping to try to add/remove shapes).
Basically I have a ragdoll, and he can grab things with his hands by creating an on-the-fly revolute joint; now, this works fine and dandy for regular-acting objects that you can throw around, but the game will also include shooting, and therefore you must be able to "pick up" guns.

Basically what my system consists of right now are three key elements:
* When grabbed, the gun is XFormed into the center of origin of the hand, and copies the angle of the hand
* The joint's upper/lower angles are set to 0, to ensure it never goes anywhere
* When grabbed, the gun's shape's filter masks are changed so that it does not collide with the ragdoll

Now, the problem is that these three don't seem to be working together!
* The gun doesn't seem to actually move into the hand and change his angles; sometimes, the gun will look like it's angles are flipping, but then flips back to it's old position like it was being reset, and in all cases his position never moves
* The joint, when grabbing a gun, seems to spin sometimes (the gun will spin in his hand about 4 times before coming to a rest, this throws off the point below)
* The angles of the gun never remain the same

Anyone have any ideas? Am I missing anything?

Joint creation:

Code:
      // Special if it's a gun
      if (Temp->Type == CT_GUN)
      {
         Ragdoll->Grabbing = true;
         b2RevoluteJointDef *jointDef = new b2RevoluteJointDef;
         jointDef->Initialize(LeftBody, RightBody, LeftBody->GetWorldCenter());
         jointDef->lowerAngle = 0;
         jointDef->upperAngle = 0;
         jointDef->enableLimit = true;
         jointDef->enableMotor = false;
         Ragdoll->GrabJointDef = jointDef;
         Ragdoll->GrabJoint = NULL;
         Mix_PlayChannel (-1, grabChunk, 0);
      }


After the callback:

Code:
               BasicBody *RightBody = (BasicBody*)Ragdoll->GrabJointDef->body2->GetUserData();

               if (RightBody->Type == CT_GUN)
               {
                  SetBodyToIgnoreRagdolls (Ragdoll->GrabJointDef->body2);
                  bool check = Ragdoll->GrabJointDef->body2->SetXForm (Ragdoll->GrabJointDef->body1->GetWorldCenter(), Ragdoll->GrabJointDef->body1->GetAngle());
               }
               else if (RightBody->Type == CT_RAGDOLL)
               {
                  RightBody = (BasicBody*)Ragdoll->GrabJointDef->body1->GetUserData();
                  if (RightBody->Type == CT_GUN)
                  {
                     SetBodyToIgnoreRagdolls (Ragdoll->GrabJointDef->body1);
                     bool check = Ragdoll->GrabJointDef->body1->SetXForm (Ragdoll->GrabJointDef->body2->GetWorldCenter(), Ragdoll->GrabJointDef->body2->GetAngle());
                  }
               }
               Ragdoll->GrabJoint = world->CreateJoint(Ragdoll->GrabJointDef);


Utility functions to change the filtermask:

Code:
void SetBodyToIgnoreRagdolls (b2Body *Body)
{
   b2Shape *shape = Body->GetShapeList();
   for (; shape && (shape->GetBody() == Body); shape = shape->GetNext())
   {
      b2FilterData filterData = shape->GetFilterData();
      filterData.maskBits &= ~0x0008;
      shape->SetFilterData (filterData);
   }
}

void SetBodyToAcceptRagdolls (b2Body *Body)
{
   b2Shape *shape = Body->GetShapeList();
   for (; shape && (shape->GetBody() == Body); shape = shape->GetNext())
   {
      b2FilterData filterData = shape->GetFilterData();
      filterData.maskBits |= 0x0008;
      shape->SetFilterData (filterData);
   }
}


Anyone have any ideas why I described above is happening?
What is 'reference angle'?
Did you try setting the collideConnected flag to false (default)? That should handle the filtering.

Paril wrote:
What is 'reference angle'?


Code:
   /// The body2 angle minus body1 angle in the reference state (radians).
   float32 referenceAngle;

In the reference state the joint angle is considered to be zero.

Try to reproduce your problem in the testbed. That way I can help you debug it.
I did end up figuring it out, the revolute joint needed to have some special variables and it was just a matter of playing around with them:

Code:
         Ragdoll->Grabbing = true;
         b2RevoluteJointDef *jointDef = new b2RevoluteJointDef;
         jointDef->Initialize(LeftBody, RightBody, LeftBody->GetWorldCenter());
         jointDef->lowerAngle = LeftBody->GetAngle()+(b2_pi+b2_pi/2);
         jointDef->upperAngle = LeftBody->GetAngle()+(b2_pi+b2_pi/2);
         jointDef->localAnchor1 = b2Vec2(0,0);
         jointDef->localAnchor2 = b2Vec2(0,-.7) + ((GunBody*)Temp)->AttachedGun->AnchorPoint;
         jointDef->referenceAngle = (b2_pi+b2_pi/2);
         jointDef->enableLimit = true;
         jointDef->enableMotor = false;
         Ragdoll->GrabJointDef = jointDef;
         Ragdoll->GrabJoint = NULL;
         Mix_PlayChannel (-1, grabChunk, 0);


-P

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