DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Joints With PhysX

05.23.2008
| 3561 views |
  • submit to reddit
        
class Ragdoll
{
public:
   Ragdoll(const NxVec3& pos)
   {
      NxQuat qRotLeft, qRotRight, qRotAround;
      qRotLeft.fromAngleAxis(90, NxVec3(0,0,1));
      qRotRight.fromAngleAxis(-90, NxVec3(0,0,1));
      qRotAround.fromAngleAxis(180, NxVec3(0,0,1));

      NxMat33 mRotLeft, mRotRight, mRotAround;
      mRotLeft.fromQuat(qRotLeft);
      mRotRight.fromQuat(qRotRight);
      mRotAround.fromQuat(qRotAround);

      // Create body parts
      head = CreateSphere(NxVec3(0,8.8,0), 0.5, 10);
      torso = CreateSphere(NxVec3(0,7,0), 0.95, 10);
      pelvis = CreateSphere(NxVec3(0,5.8,0), 0.7, 10);

      leftUpperArm = CreateCapsule(NxVec3(0.5,8.5,0), 1, 0.4, 10);
      leftUpperArm->setGlobalOrientationQuat(qRotRight);
      leftForeArm = CreateCapsule(NxVec3(2,8.5,0), 1, 0.3, 10);
      leftForeArm->setGlobalOrientationQuat(qRotRight);
      leftHand = CreateBox(NxVec3(3.5,8.5,0), NxVec3(0.3,0.3,0.1), 10);
      leftHand->setGlobalOrientationQuat(qRotRight);

      rightUpperArm = CreateCapsule(NxVec3(-0.5,8.5,0), 1, 0.4, 10);
      rightUpperArm->setGlobalOrientationQuat(qRotLeft);
      rightForeArm = CreateCapsule(NxVec3(-2,8.5,0), 1, 0.3, 10);
      rightForeArm->setGlobalOrientationQuat(qRotLeft);
      rightHand = CreateBox(NxVec3(-3.5,8.5,0), NxVec3(0.3,0.3,0.1), 10);
      rightHand->setGlobalOrientationQuat(qRotLeft);

      leftThigh = CreateCapsule(NxVec3(0.6,6,0), 1.5, 0.5, 10);
      leftThigh->setGlobalOrientationQuat(qRotAround);
      leftCalf = CreateCapsule(NxVec3(0.6,3.5,0), 1.5, 0.35, 10);
      leftCalf->setGlobalOrientationQuat(qRotAround);
      leftFoot = CreateBox(NxVec3(0.6,1.5,0.2), NxVec3(0.4,0.2,0.75), 10);
      leftFoot->setGlobalOrientationQuat(qRotAround);

      rightThigh = CreateCapsule(NxVec3(-0.6,6,0), 1.5, 0.5, 10);
      rightThigh->setGlobalOrientationQuat(qRotAround);
      rightCalf = CreateCapsule(NxVec3(-0.6,3.5,0), 1.5, 0.35, 10);
      rightCalf->setGlobalOrientationQuat(qRotAround);
      rightFoot = CreateBox(NxVec3(-0.6,1.5,0.2), NxVec3(0.4,0.2,0.75), 10);
      rightFoot->setGlobalOrientationQuat(qRotAround);

      // Joint body parts together
      neck = CreateBodySphericalJoint(head,torso,NxVec3(0,8.8,0),NxVec3(0,1,0));
      leftShoulder = CreateBodySphericalJoint(leftUpperArm,torso,NxVec3(0.5,8.5,0),NxVec3(1,0,0));
      rightShoulder = CreateBodySphericalJoint(rightUpperArm,torso,NxVec3(-0.5,8.5,0),NxVec3(-1,0,0));
      spine = CreateBodySphericalJoint(torso,pelvis,NxVec3(0,7,0),NxVec3(0,-1,0));
      leftHip = CreateBodySphericalJoint(leftThigh,pelvis,NxVec3(0.6,6,0),NxVec3(0,-1,0));
      rightHip = CreateBodySphericalJoint(rightThigh,pelvis,NxVec3(-0.6,6,0),NxVec3(0,-1,0));

      leftElbow = CreateRevoluteJoint(leftForeArm,leftUpperArm,NxVec3(2,8.5,0),NxVec3(0,0,-1));
      rightElbow = CreateRevoluteJoint(rightForeArm,rightUpperArm,NxVec3(-2,8.5,0),NxVec3(0,0,-1));

      leftWrist = CreateRevoluteJoint2(leftHand,leftForeArm,NxVec3(0,-0.15,0),NxVec3(0,1.3,0),NxVec3(0,1,0),NxVec3(0,1,0));
      rightWrist = CreateRevoluteJoint2(rightHand,rightForeArm,NxVec3(0,-0.15,0),NxVec3(0,1.3,0),NxVec3(0,1,0),NxVec3(0,1,0));

      leftKnee = CreateRevoluteJoint(leftCalf,leftThigh,NxVec3(0.6,3.5,0),NxVec3(1,0,0));
      rightKnee = CreateRevoluteJoint(rightCalf,rightThigh,NxVec3(-0.6,3.5,0),NxVec3(-1,0,0));

      leftAnkle = CreateRevoluteJoint(leftFoot,leftCalf,NxVec3(0.6,1.3,0),NxVec3(1,0,0));
      rightAnkle = CreateRevoluteJoint(rightFoot,rightCalf,NxVec3(-0.6,1.3,0),NxVec3(-1,0,0));
   };

   NxActor* head;
   NxActor* torso;
   NxActor* pelvis;
   NxActor* leftUpperArm;
   NxActor* rightUpperArm;
   NxActor* leftForeArm;
   NxActor* rightForeArm;
   NxActor* leftHand;
   NxActor* rightHand;
   NxActor* leftThigh;
   NxActor* rightThigh;
   NxActor* leftCalf;
   NxActor* rightCalf;
   NxActor* leftFoot;
   NxActor* rightFoot;

   NxSphericalJoint* neck;
   NxSphericalJoint* leftShoulder;
   NxSphericalJoint* rightShoulder;
   NxSphericalJoint* spine;
   NxSphericalJoint* leftHip;
   NxSphericalJoint* rightHip;

   NxRevoluteJoint* leftElbow;
   NxRevoluteJoint* rightElbow;
   NxRevoluteJoint* leftWrist;
   NxRevoluteJoint* rightWrist;
   NxRevoluteJoint* leftKnee;
   NxRevoluteJoint* rightKnee;
   NxRevoluteJoint* leftAnkle;
   NxRevoluteJoint* rightAnkle;
};