PxRigidStatic* PxCreateStatic(PxPhysics& sdk, const PxTransform& transform, const PxGeometry& geometry, PxMaterial& material, const PxTransform& shapeOffset) { PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateStatic: transform is not valid."); PX_CHECK_AND_RETURN_NULL(shapeOffset.isValid(), "PxCreateStatic: shapeOffset is not valid."); PxShape* shape = sdk.createShape(geometry, material, true); if(!shape) return NULL; shape->setLocalPose(shapeOffset); PxRigidStatic* s = PxCreateStatic(sdk, transform, *shape); shape->release(); return s; }
void InitializePhysX(vector<PhysXObject*>* &cubeList) { allActors = new vector<PhysXObject*>; PxFoundation* foundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback); gPhysicsSDK = PxCreatePhysics(PX_PHYSICS_VERSION, *foundation, PxTolerancesScale()); if(gPhysicsSDK == NULL) { exit(1); } PxInitExtensions(*gPhysicsSDK); PxSceneDesc sceneDesc(gPhysicsSDK->getTolerancesScale()); sceneDesc.gravity=PxVec3(0.0f, -9.8f, 0.0f); if(!sceneDesc.cpuDispatcher) { PxDefaultCpuDispatcher* mCpuDispatcher = PxDefaultCpuDispatcherCreate(3); sceneDesc.cpuDispatcher = mCpuDispatcher; } if(!sceneDesc.filterShader) sceneDesc.filterShader = gDefaultFilterShader; gScene = gPhysicsSDK->createScene(sceneDesc); gScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0); gScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f); //1) Create Planes PxMaterial* mMaterial = gPhysicsSDK->createMaterial(0.5, 0.5, 0.5); for(int i = 0; i < 1; i++) { PhysXObject* plane = new PhysXObject; plane->actor = gPhysicsSDK->createRigidStatic(planePoses[i]); PxShape* shape = plane->actor->createShape(PxPlaneGeometry(), *mMaterial); gScene->addActor(*(plane->actor)); allActors->push_back(plane); planes.push_back(plane); } //2) Create Planets PxReal planetDensity = 1.0f; PxVec3 planetDimensions(2,2,2); PxBoxGeometry planetGeom(planetDimensions); PxTransform planetTransform; for(int i = 0; i < PLANET_NUM; i++) { planetTransform = planetTransforms[i]; PhysXObject* planet = new PhysXObject; planet->actor = PxCreateStatic(*gPhysicsSDK, planetTransform, planetGeom, *mMaterial); EnableGravity(planet->actor); gScene->addActor(*(planet->actor)); allActors->push_back(planet); planets.push_back(planet); //HACK: /* Create the joint handlers for distance limiting /* We need to do this because a distance joint attached to an actor /* seems to void collisions between those two actors (i.e. "phases through") /* So we make another actor in the same position to hold the position PhysXObject* newHandle = new PhysXObject; newHandle->actor = PxCreateStatic(*gPhysicsSDK, tran, boxgeom, *mMaterial); gScene->addActor(*(newHandle->actor)); planetJointHandles.push_back(newHandle); //We also don't need to worry about drawing the joints, for obvious reasons */ } //3) Create Cubes PxReal density = 1.0f; PxTransform transform(PxVec3(0.0f, 0.0f, 0.0f), PxQuat::createIdentity()); PxVec3 dimensions(0.5, 0.5, 0.5); PxBoxGeometry geometry(dimensions); for(int i = 0; i < BLOCK_NUM; i++) { srand((time(NULL) * i) + time(NULL)); transform.p = PxVec3((float)((rand() % (2 * PLANET_HEIGHT)) - PLANET_HEIGHT), (float)((rand() % (2 * PLANET_HEIGHT)) - PLANET_HEIGHT), (float)((rand() % (2 * PLANET_HEIGHT)) - PLANET_HEIGHT)); PhysXObject* cube = new PhysXObject; cube->actor = PxCreateDynamic(*gPhysicsSDK, transform, geometry, *mMaterial, density); //Create Distance Joints between planets here //Not included for run time optimizations //End creating distance joints //Create D6 Joints between planets here //Not included for run time optimizations //End creating distance joints cube->actor->isRigidDynamic()->setAngularDamping(0.75); cube->actor->isRigidDynamic()->setLinearVelocity(PxVec3(0,0,0)); gScene->addActor(*(cube->actor)); allActors->push_back(cube); boxes.push_back(cube); } cubeList = allActors; }
void Physics::SetUpTutorial1() { //Add a plane PxTransform pose = PxTransform(PxVec3(0.0f, -50, 0.0f), PxQuat(PxHalfPi*1.0f, PxVec3(0.0f, 0.0f, 1.0f))); PxRigidStatic* plane = PxCreateStatic(*g_Physics, pose, PxPlaneGeometry(), *g_PhysicsMaterial); //Add it to the physx scene g_PhysicsScene->addActor(*plane); PxArticulation* ragDollArticulation; ragDollArticulation = makeRagdoll(g_Physics, ragdollData, PxTransform(PxVec3(0, 0, 0)), 0.1f, g_PhysicsMaterial); g_PhysicsScene->addArticulation(*ragDollArticulation); PxBoxGeometry box(1, 1, 10); for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3((j*-2) - 4, (j*2)-9, 8)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, box, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3((j*-2) - 3, (j * 2) - 10, 8)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, box, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } PxBoxGeometry boxS(10, 1, 1); for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3(8, (j * 2) - 9, (j*-2) - 4)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, boxS, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } for (int j = -25; j < 0; j++) { PxTransform transform(PxVec3(8, (j * 2) - 10, (j*-2) - 3)); PxRigidStatic* staticActor = PxCreateStatic(*g_Physics, transform, boxS, *g_PhysicsMaterial); //add it to the physx scene g_PhysicsScene->addActor(*staticActor); } float density = 50; PxBoxGeometry boxes(1, 1, 1); for (int i = 0; i < 2; i++) { for (int j = 30; j < 50; j++) { for (int k = 0; k < 2; k++) { PxTransform transform(PxVec3(2*i, 2*j+1, 2*k)); PxRigidDynamic* dynamicActor = PxCreateDynamic(*g_Physics, transform, boxes, *g_PhysicsMaterial, density); //add it to the physx scene g_PhysicsScene->addActor(*dynamicActor); //dynamicActor->putToSleep(); } } } }
void CurrentApp::LoadContent() { // setup gpass framebuffer m_gPassTarget = new RenderTarget(); m_gPassTarget->SetSize(1280, 720); m_gPassTarget->Initialise(); m_gPassTarget->AttachColourBuffer(0, GL_RGB8); //albedo m_gPassTarget->AttachColourBuffer(1, GL_RGB32F); //position m_gPassTarget->AttachColourBuffer(2, GL_RGB32F); //normal m_gPassTarget->AttachDepthBuffer(); m_gPassTarget->SetDrawBuffers(); // setup light framebuffer m_lightPassTarget = new RenderTarget(); m_lightPassTarget->SetSize(1280, 720); m_lightPassTarget->Initialise(); m_lightPassTarget->AttachColourBuffer(0, GL_RGB8); m_lightPassTarget->AttachDepthBuffer(); m_lightPassTarget->SetDrawBuffers(); CreateFullscreenQuad(); //Setting up Physx m_gravity = -9.8f; SetupPhysx(); SetupVisualDebugger(); //Set up a Player g_ControllerManager = PxCreateControllerManager(*g_PhysicsScene); PxCapsuleControllerDesc desc; desc.contactOffset = 0.05f; desc.height = 3.0f; desc.material = g_PhysicsMaterial; desc.nonWalkableMode = PxControllerNonWalkableMode::ePREVENT_CLIMBING_AND_FORCE_SLIDING; desc.climbingMode = PxCapsuleClimbingMode::eCONSTRAINED; desc.position = PxExtendedVec3(100, 200, 150); desc.radius = 2.0f; desc.stepOffset = 0.1f; g_PlayerCollisions = new PlayerCollisions(); desc.reportCallback = g_PlayerCollisions; g_PlayerController = g_ControllerManager->createController(desc); //Create a New Camera m_camera = new FlyCamera(); m_camera->SetupProjection(glm::pi<float>() * 0.25f, 16 / 9.f, 0.1f, 10000.f); m_camera->LookAt(vec3(100, 10, 10), vec3(0), vec3(0, 1, 0)); m_assetManager = new AssetManager(); m_renderer = new Renderer(m_assetManager); //Terrain Shit here m_terrain = m_renderer->CreateTerrain(); m_terrain->SetSize(30, 30); m_terrain->GenerateFromPerlin(); m_nodeMap = new NodeMap(); m_nodeMap->GenerateFromTerrain(m_terrain); ////Snow Particle m_snowEmitter = m_renderer->CreateParticle(); m_snowEmitter->ReadFile("./Content/Particles/Rain"); //m_snowEmitter->UseTexture("./Content/Particles/ROBOTUNICORN.png"); m_snowEmitter->SetPosition(glm::vec3(150, 400, 150)); m_snowEmitter->initalise(""); m_fairyEmitter = m_renderer->CreateParticle(); m_fairyEmitter->ReadFile("./Content/Particles/fairy"); m_fairyEmitter->SetPosition(glm::vec3(100, 130, 100)); m_fairyEmitter->initalise(""); //Load Renderables int cubeModel = m_assetManager->LoadModel("./Content/Renderables/cube.fbx"); int sphereModel = m_assetManager->LoadModel("./Content/Renderables/cube.fbx"); int bunnyModel = m_assetManager->LoadModel("./Content/Renderables/bunny.fbx"); int treeModel = m_assetManager->LoadModel("./Content/Renderables/Tree/AlanTree.fbx"); //m_assetManager->RecalculateNormals(treeModel); //LoadSkybox m_skybox = new Skybox(); std::vector<std::string> faces; faces.push_back("./Content/Textures/skyRight.png"); faces.push_back("./Content/Textures/skyLeft.png"); faces.push_back("./Content/Textures/skyTop.png"); faces.push_back("./Content/Textures/skyBottom.png"); faces.push_back("./Content/Textures/skyFront.png"); faces.push_back("./Content/Textures/skyBack.png"); m_skybox->Initialize(faces); //Load AI m_world = new World(m_nodeMap); //Load Lights m_light = m_renderer->CreateLight(); m_light->SetColour(glm::vec4(200, 255, 255, 1)); m_light->SetFallOff(200000); m_light->SetPosition(glm::vec3(150, 30, 150)); m_sunLight = m_renderer->CreateLight(); m_sunLight->SetColour(glm::vec4(50, 50, 80, 1)); m_sunLight->SetFallOff(1000000); m_sunLight->SetPosition(glm::vec3(0, 200, 0)); //create our particle system PxParticleFluid* pf; // create particle system in PhysX SDK // set immutable properties. PxU32 maxParticles = 200; bool perParticleRestOffset = false; pf = g_Physics->createParticleFluid(maxParticles, perParticleRestOffset); pf->setRestParticleDistance(3.0f); pf->setDynamicFriction(0); pf->setStaticFriction(0); pf->setDamping(0); pf->setParticleMass(30); pf->setRestitution(0); //pf->setParticleReadDataFlag(PxParticleReadDataFlag::eDENSITY_BUFFER, // true); pf->setParticleBaseFlag(PxParticleBaseFlag::eCOLLISION_TWOWAY, true); pf->setStiffness(300); if (pf) { float multiplier = 10.0f; g_PhysicsScene->addActor(*pf); m_particleEmitter = new ParticleFluidEmitter(maxParticles, PxVec3(150, 120, 150), pf, .05f); m_particleEmitter->setStartVelocityRange(-0.001f, -6000.0f * multiplier, -0.001f, 0.001f, -6000.0f * multiplier, 0.001f); } for (int i = 0; i < 5; ++i) { m_AI[i] = new UtilityAI(m_assetManager, m_world, m_nodeMap); Renderable* renderBox = m_renderer->CreateRenderable(); renderBox->SetModel(bunnyModel); renderBox->SetScale(glm::vec3(0.01f, 0.01f, 0.01f)); renderBox->SetTexture(LoadTexture("./Content/Renderables/white.png")); m_AI[i]->SetRenderable(renderBox); } for (int i = 0; i < 50; ++i) { m_trees[i] = m_renderer->CreateRenderable(); m_trees[i]->SetModel(treeModel); glm::vec3 position = m_nodeMap->GetClosestNode(glm::vec3(40 + rand() % 220, 40 + rand() % 220, 40 + rand() % 220))->GetPos(); position.y -= 1; m_trees[i]->SetPosition(position); m_trees[i]->Rotate(glm::vec3(1, 0, 0), -(3.14159265f / 2.0f)); m_trees[i]->Rotate(glm::vec3(0, 0, 1), rand() % 200 / 100.0f); float randVal = rand() % 120 / 120.f; m_trees[i]->SetScale(glm::vec3(0.2f + randVal, 0.2f + randVal, 0.2f + randVal)); } //Define a Plane PxTransform pose = PxTransform(PxVec3(0.0f, 5, 0.0f), PxQuat(PxHalfPi*1.0f, PxVec3(0.0f, 0.0f, 1.0f))); PxRigidStatic* plane = PxCreateStatic(*g_Physics, pose, PxPlaneGeometry(), *g_PhysicsMaterial); //Add the plane to the Physx Scene g_PhysicsScene->addActor(*plane); m_terrain->AddPhysicsShape(g_PhysicsScene, g_Physics); //Disable the mouse SetCursorPos(0, 0); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); }