Advertisement
Silverlan

PhysX Actor Creation

Mar 3rd, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. physx::PxRigidDynamic *Game::CreatePhysXActor(physx::PxGeometry &geometry,physx::PxMaterial &material,physx::PxTransform &transform)
  2. {
  3.     physx::PxPhysics *physics = m_stateNetwork->GetPhysics();
  4.     if(physics == NULL)
  5.         return NULL;
  6.     physx::PxRigidDynamic *actor = physics->createRigidDynamic(transform);
  7.     actor->createShape(geometry,material);
  8.     m_pxScene->addActor(*actor);
  9.     actor->putToSleep();
  10.     return actor;
  11. }
  12. physx::PxRigidDynamic *Game::CreatePhysXActor(physx::PxGeometry &geometry,physx::PxMaterial &material,glm::vec3 &pos) {return CreatePhysXActor(geometry,material,physx::PxTransform(physx::PxVec3(pos.x,pos.y,pos.z)));}
  13.  
  14. PhysObj *BaseEntity::InitializeModelPhysics()
  15. {
  16.     Model *mdl = GetModel();
  17.     if(mdl == NULL)
  18.         return NULL;
  19.     std::vector<CollisionMesh*> *collisionMeshes;
  20.     mdl->GetCollisionMeshes(&collisionMeshes);
  21.     if(collisionMeshes->empty())
  22.         return NULL;
  23.     Skeleton *skeleton = mdl->GetSkeleton();
  24.     physx::PxPhysics *physics = engine->GetPhysics();
  25.     physx::PxCooking *cooking = engine->GetCookingLibrary();
  26.     NetworkState *state = GetNetworkState();
  27.     Game *game = state->GetGameState();
  28.     physx::PxMaterial *material = physics->createMaterial(0.5f,0.5f,0.1f);
  29.     physx::PxConvexMeshDesc meshDesc;
  30.     meshDesc.points.stride = sizeof(glm::vec3);
  31.     meshDesc.flags = physx::PxConvexFlag::eCOMPUTE_CONVEX;
  32.     meshDesc.vertexLimit = 256;
  33.     std::vector<physx::PxRigidActor*> actors;
  34.     std::map<int,unsigned int> boneActorIDs;
  35.     bool bPhys = false;
  36.     for(unsigned int i=0;i<collisionMeshes->size();i++)
  37.     {
  38.         CollisionMesh *mesh = (*collisionMeshes)[i];
  39.         std::vector<glm::vec3> &verts = mesh->vertices;
  40.         if(!verts.empty())
  41.         {
  42.             meshDesc.points.count = verts.size();
  43.             meshDesc.points.data = &verts[0];
  44.             physx::MemoryOutputStream buf;
  45.             if(cooking->cookConvexMesh(meshDesc,buf))
  46.             {
  47.                 physx::MemoryInputData input(buf.getData(),buf.getSize());
  48.                 physx::PxConvexMesh *convexMesh = physics->createConvexMesh(input);
  49.                 int boneID = mesh->GetBoneParent();
  50.                 std::map<int,unsigned int>::iterator it = boneActorIDs.find(boneID);
  51.                 if(it == boneActorIDs.end())
  52.                 {
  53.                     physx::PxRigidDynamic *actor = game->CreatePhysXActor(physx::PxConvexMeshGeometry(convexMesh),*material,*GetPosition());
  54.                     actor->setRigidBodyFlag(physx::PxRigidBodyFlag::Enum::eENABLE_CCD,true);
  55.                     actor->setRigidBodyFlag(physx::PxRigidBodyFlag::Enum::eENABLE_CCD_FRICTION,true);
  56.                     float mass = 50.f;
  57.                     physx::PxRigidBodyExt::setMassAndUpdateInertia(*actor,mass);
  58.  
  59.                     if(bPhys == false)
  60.                     {
  61.                         bPhys = true;
  62.                         if(m_physObject != NULL)
  63.                             DestroyPhysicsObject();
  64.                         m_physObject = new DynamicPhysObj(this,actor,boneID);
  65.                     }
  66.                     else
  67.                         static_cast<DynamicPhysObj*>(m_physObject)->AddActor(actor,boneID);
  68.                     actors.push_back(actor);
  69.                     boneActorIDs.insert(std::map<int,unsigned int>::value_type(boneID,actors.size() -1));
  70.                 }
  71.                 else
  72.                     actors[it->second]->createShape(physx::PxConvexMeshGeometry(convexMesh),*material);
  73.             }
  74.         }
  75.     }
  76.     if(bPhys == false)
  77.         return NULL;
  78.     return m_physObject;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement