//============================================================================= // PRIVATE FUNCTIONS //============================================================================= static physx::unique_ptr<PxConvexMesh> GenerateConvexFromDXMesh(PxPhysics &iPhysics, ID3DXMesh *iMesh) { //Used to retrieve information from X file struct Mesh_FVF { D3DXVECTOR3 VertexPos; D3DXVECTOR3 Normal; D3DXVECTOR2 TexCoord; }; int aNumVerticies = iMesh->GetNumVertices(); DWORD FVFSize = D3DXGetFVFVertexSize(iMesh->GetFVF()); //Create pointer for vertices PxVec3* verts = new PxVec3[aNumVerticies]; char *DXMeshPtr; iMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&DXMeshPtr); for(int i = 0; i < aNumVerticies; i++) { Mesh_FVF *DXMeshFVF = (Mesh_FVF*)DXMeshPtr; verts[i] = PxVec3(DXMeshFVF->VertexPos.x, DXMeshFVF->VertexPos.y, DXMeshFVF->VertexPos.z); DXMeshPtr += FVFSize; } iMesh->UnlockVertexBuffer(); // Create descriptor for convex mesh PxConvexMeshDesc convexDesc; convexDesc.points.count = aNumVerticies; convexDesc.points.stride = sizeof(PxVec3); convexDesc.points.data = verts; convexDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX; PxTolerancesScale toleranceScale; toleranceScale.length = 1.0f; toleranceScale.mass = 1000.0f; toleranceScale.speed = 9.8f; assert(toleranceScale.isValid()); physx::unique_ptr<PxCooking> cooker = physx::unique_ptr<PxCooking>( PxCreateCooking(PX_PHYSICS_VERSION, iPhysics.getFoundation(), PxCookingParams(toleranceScale)) ); // Cooking from memory MemoryStream buf; physx::unique_ptr<PxConvexMesh> convexMesh; if(cooker->cookConvexMesh(convexDesc, buf)) { convexMesh = physx::unique_ptr<PxConvexMesh>(iPhysics.createConvexMesh(buf)); } delete[] verts; return convexMesh; }
void FPhysXMesh::initialize() { if (mCookedData != nullptr && mCookedDataSize > 0) { PxPhysics* physx = gPhysX().getPhysX(); PxDefaultMemoryInputData input(mCookedData, mCookedDataSize); if (mType == PhysicsMeshType::Convex) mConvexMesh = physx->createConvexMesh(input); else mTriangleMesh = physx->createTriangleMesh(input); } }
void PhysXRigidManager::addDynamicBody(const std::string & scene, physx::PxScene * world, physx::PxCooking * mCooking, nau::physics::IPhysics::BoundingVolume shape, physx::PxMaterial * material) { PxPhysics *gPhysics = &(world->getPhysics()); PxRigidDynamic * dynamic; rigidBodies[scene].scalingFactor = getScalingFactor(rigidBodies[scene].info.extInfo.transform); PxMeshScale scale = PxMeshScale(rigidBodies[scene].scalingFactor); PxMat44 aux(rigidBodies[scene].info.extInfo.transform); PxVec3 pos(aux.getPosition()); aux *= (1.0f / rigidBodies[scene].scalingFactor); PxMat44 mat( PxVec3(aux.column0.x, aux.column0.y, aux.column0.z), PxVec3(aux.column1.x, aux.column1.y, aux.column1.z), PxVec3(aux.column2.x, aux.column2.y, aux.column2.z), pos ); //PxTransform trans = PxTransform(PxMat44(rigidBodies[scene].extInfo.transform)); PxTransform trans = PxTransform(mat); switch (shape.sceneShape) { case nau::physics::IPhysics::BOX: { dynamic = gPhysics->createRigidDynamic(trans); dynamic->createShape( PxBoxGeometry( shape.max[0] * rigidBodies[scene].scalingFactor, shape.max[1] * rigidBodies[scene].scalingFactor, shape.max[2] * rigidBodies[scene].scalingFactor), *material); } break; case nau::physics::IPhysics::SPHERE: { dynamic = gPhysics->createRigidDynamic(trans); dynamic->createShape(PxSphereGeometry(shape.max[0] * rigidBodies[scene].scalingFactor), *material); } break; case nau::physics::IPhysics::CAPSULE: { dynamic = gPhysics->createRigidDynamic(trans); dynamic->createShape( PxCapsuleGeometry( shape.max[0] * rigidBodies[scene].scalingFactor, shape.max[1] * rigidBodies[scene].scalingFactor), *material); } break; default: { dynamic = gPhysics->createRigidDynamic(trans); PxConvexMesh * convexMesh = gPhysics->createConvexMesh(*getTriangleMeshGeo(world, mCooking, rigidBodies[scene].info.extInfo, false)); dynamic->createShape(PxConvexMeshGeometry(convexMesh, scale), *material); } break; } dynamic->userData = static_cast<void*> (new std::string(scene)); rigidBodies[scene].rollingFriction = -1.0f; rigidBodies[scene].rollingFrictionTimeStamp = 1; world->addActor(*dynamic); rigidBodies[scene].info.actor = dynamic; }
void PhsXWorld::_addRigid(float mass, float friction, float restitution, std::shared_ptr<nau::scene::IScene> &aScene, std::string name, nau::math::vec3 aVec) { PxPhysics *gPhysics = &(m_pDynamicsWorld->getPhysics()); if (mass == 0.0f) { PxRigidStatic* staticActor; if (name.compare("plane") == 0) { staticActor = PxCreatePlane(*gPhysics, PxPlane(0.0f, 1.0f, 0.0f, 0.0f), *(gPhysics->createMaterial(friction, friction, restitution)) ); } else { /*if (name.compare("box") == 0) { staticActor = PxCreateStatic(*gPhysics, PxTransform(PxMat44(const_cast<float*> (aScene->getTransform().getMatrix()))), PxBoxGeometry(1.0f,1.0f,1.0f), *(gPhysics->createMaterial(1.0f, 1.0f, 0.6f)) ); } else {*/ staticActor = gPhysics->createRigidStatic(PxTransform(PxMat44(const_cast<float*> (aScene->getTransform().getMatrix())))); PxTriangleMeshGeometry triGeom; triGeom.triangleMesh = gPhysics->createTriangleMesh(getTriangleMeshGeo(m_pDynamicsWorld, aScene)); staticActor->createShape(triGeom, *(gPhysics->createMaterial(friction, friction, restitution))); //} } staticActor->userData = aScene.get(); m_pDynamicsWorld->addActor(*staticActor); } else { PxRigidDynamic* dynamic; //if (name.compare("ball") == 0) { // dynamic = PxCreateDynamic(*gPhysics, // PxTransform(PxMat44(const_cast<float*> (aScene->getTransform().getMatrix()))), // PxSphereGeometry(1), // *(gPhysics->createMaterial(0.5f, 0.5f, 0.6f)), // 10.0f // ); // //dynamic->setLinearVelocity(PxVec3(0, -50, -100)); //} //else { PxTransform trans = PxTransform(PxMat44(const_cast<float*> (aScene->getTransform().getMatrix()))); dynamic = gPhysics->createRigidDynamic(trans); PxConvexMesh * convexMesh = gPhysics->createConvexMesh(getTriangleMeshGeo(m_pDynamicsWorld, aScene, false)); PxConvexMeshGeometry convGeom(convexMesh); //PxConvexMeshGeometry convGeom(convexMesh, PxMeshScale(0.5f)); //convGeom.convexMesh = gPhysics->createConvexMesh(getTriangleMeshGeo(m_pDynamicsWorld, aScene, false)); //PxShape *shape = dynamic->createShape(convGeom, *(gPhysics->createMaterial(0.5f, 0.5f, 0.6f)), PxShapeFlag::eSIMULATION_SHAPE | PxShapeFlag::eVISUALIZATION | PxShapeFlag::eSCENE_QUERY_SHAPE); PxShape *shape = dynamic->createShape(convGeom, *(gPhysics->createMaterial(friction, friction, restitution))); //shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, true); //dynamic->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, false); //} dynamic->userData = aScene.get(); //dynamic->setAngularDamping(0.5f); //dynamic->setLinearVelocity(velocity); m_pDynamicsWorld->addActor(*dynamic); } }