Advertisement
Guest User

PhysXBone

a guest
Apr 13th, 2013
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. void PhysxBone::Initiliaze(MeshFilter* pMeshFilter)
  2. {
  3.     //Map the bone
  4.     MapToBone(pMeshFilter);
  5.     //Create actor and set it on the correct position
  6.     CreatePhysxBone(pMeshFilter);
  7. }
  8.  
  9. void PhysxBone::MapToBone(MeshFilter* pMeshFilter)
  10. {
  11.     //Find the bone we want to map to
  12.     for(auto bone : pMeshFilter->GetSkeleton())
  13.     {
  14.         if(m_boneLayout.name == bone.Name)
  15.         {
  16.             m_pBone = &bone;
  17.             break;
  18.         }
  19.     }
  20.     //Check if we found our bone, else there is a mistake with the input
  21.     ASSERT(m_pBone!=nullptr, _T("PhysxBone NAME INCORRECT! PhysxBone can not be mapped to a Bone in the Model!"));
  22.  
  23.     if(m_pBone)
  24.     {
  25.         //Store the index
  26.         m_iBoneIndex = m_pBone->Index;
  27.  
  28.         //Get all it's parent and calculate the total offset, or the position in modelspace
  29.         int currentParentIndex = m_pBone->Parent;
  30.         vector<D3DXMATRIX> vOffsets;
  31.  
  32.         //get all offsets first
  33.         while(currentParentIndex > -1)
  34.         {
  35.             vOffsets.push_back(pMeshFilter->GetSkeleton().at(currentParentIndex).Offset);
  36.             currentParentIndex = pMeshFilter->GetSkeleton().at(currentParentIndex).Parent;
  37.         }
  38.  
  39.         //inverse the vector and calculate the total offset
  40.         std::reverse(vOffsets.begin(), vOffsets.end());
  41.         for(auto offset : vOffsets)
  42.         {
  43.             m_matTotalOffset *= offset;
  44.         }
  45.         //finally adding it's own offset to the calculation to get the final position on modelspace
  46.         m_matTotalOffset *= m_pBone->Offset;
  47.  
  48.         //when we know the transform in modelspace calculate the transform in worldspace
  49.         //FIXME: worldtransform of model is hardcoded atm!!
  50.         D3DXMATRIX world;
  51.         D3DXMatrixIdentity(&world);
  52.  
  53.         m_matWorldSpace = m_matTotalOffset * world;
  54.     }
  55. }
  56.  
  57. void PhysxBone::UpdateLeechMode(D3DXMATRIX matKeyTransform)
  58. {
  59.     //Calculate the final position
  60.     //Orientation * TotalOffset * LocalTransformAnimation
  61.     m_matWorldSpace = m_matTotalOffset * matKeyTransform * world;
  62.  
  63.     //Convert matrix and position the actor
  64.     NxMat34 nPos;
  65.     PhysicsManager::GetInstance()->DMatToNMat(nPos, m_matWorldSpace);
  66.     m_pActor->setGlobalPose(nPos);
  67. }
  68.  
  69. void PhysxBone::CreatePhysxBone(MeshFilter* pMeshFilter)
  70. {
  71.     //Creates the bone using the information we know when we mapped the bone
  72.     //Also taking into account which shape we want
  73.     if(m_boneLayout.shapeType == RagdollShapeType::capsule)
  74.     {
  75.         //Create the capsule shape desc
  76.         NxCapsuleShapeDesc capsuleDesc;
  77.         capsuleDesc.height = m_boneLayout.height;
  78.         capsuleDesc.radius = m_boneLayout.radius;
  79.         capsuleDesc.localPose.t = NxVec3(0, capsuleDesc.radius + 0.5f * capsuleDesc.height, 0);
  80.  
  81.         //Create body so the actor is dynamic
  82.         NxBodyDesc bodyDesc;
  83.         bodyDesc.setToDefault();
  84.         bodyDesc.angularDamping = 0.75f;
  85.         bodyDesc.linearVelocity = NxVec3(0,0,0);
  86.  
  87.         //Create the actor
  88.         NxActorDesc actorDesc;
  89.         actorDesc.shapes.pushBack(&capsuleDesc);
  90.         actorDesc.body = &bodyDesc;
  91.         actorDesc.density = 10.0f;
  92.         //Important to set a density if we have a body to succesfully create an actor with a body
  93.  
  94.         //Position the actor to the correct place
  95.         NxMat34 nPos;
  96.         PhysicsManager::GetInstance()->DMatToNMat(nPos, m_matWorldSpace);
  97.         actorDesc.globalPose = nPos;
  98.  
  99.         //Create the actor
  100.         m_pActor = m_pPhysicsScene->createActor(actorDesc);
  101.        
  102.         if(!m_pActor)
  103.             Logger::Log(_T("Error creating actor"), LogLevel::Error);
  104.  
  105.         //Default set our actor to be kinematic
  106.         m_pActor->raiseBodyFlag(NX_BF_KINEMATIC);
  107.         m_pActor->raiseActorFlag(NX_AF_DISABLE_COLLISION);
  108.     }
  109.     else if(m_boneLayout.shapeType == RagdollShapeType::sphere)
  110.     {  
  111.         //Create the sphere shape desc
  112.         NxSphereShapeDesc sphereDesc;
  113.         sphereDesc.radius = m_boneLayout.radius;
  114.         sphereDesc.localPose.t = NxVec3(0, m_boneLayout.radius, 0);
  115.  
  116.         //Create body so the actor is dynamic
  117.         NxBodyDesc bodyDesc;
  118.         bodyDesc.setToDefault();
  119.         bodyDesc.angularDamping = 0.75f;
  120.         bodyDesc.linearVelocity = NxVec3(0,0,0);
  121.  
  122.         NxActorDesc actorDesc;
  123.         actorDesc.shapes.pushBack(&sphereDesc);
  124.         actorDesc.body = &bodyDesc;
  125.         actorDesc.density = 10.0f;
  126.         //Important to set a density if we have a body to succesfully create an actor with a body
  127.  
  128.         //Position the actor to the correct place
  129.         NxMat34 nPos;
  130.         PhysicsManager::GetInstance()->DMatToNMat(nPos, m_matWorldSpace);
  131.         actorDesc.globalPose = nPos;
  132.  
  133.         //Create the actor
  134.         m_pActor = m_pPhysicsScene->createActor(actorDesc);
  135.  
  136.         if(!m_pActor)
  137.             Logger::Log(_T("Error creating actor"), LogLevel::Error);
  138.  
  139.         //Default set our actor to be kinematic
  140.         m_pActor->raiseBodyFlag(NX_BF_KINEMATIC);
  141.         m_pActor->raiseActorFlag(NX_AF_DISABLE_COLLISION);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement