PxActor* World::createRigidBody(const PxGeometry& geometry, float mass, const ofVec3f& pos, const ofQuaternion& rot, float density) { assert(inited); PxTransform transform; toPx(pos, transform.p); toPx(rot, transform.q); PxActor *actor; if (mass > 0) { PxRigidDynamic* rigid = PxCreateDynamic(*physics, transform, geometry, *defaultMaterial, density); rigid->setMass(mass); rigid->setLinearDamping(0.25); rigid->setAngularDamping(0.25); actor = rigid; } else { PxRigidStatic *rigid = PxCreateStatic(*physics, transform, geometry, *defaultMaterial); actor = rigid; } scene->addActor(*actor); return actor; }
PxRigidDynamic* createDynamic(const PxTransform& t, const PxGeometry& geometry, const PxVec3& velocity = PxVec3(0)) { PxRigidDynamic* dynamic = PxCreateDynamic(*gPhysics, t, geometry, *gMaterial, 10.0f); dynamic->setAngularDamping(0.5f); dynamic->setLinearVelocity(velocity); gScene->addActor(*dynamic); return dynamic; }
void Physics::Shoot() { float densityS = 100; PxSphereGeometry sphere(1); PxTransform transformS(PxVec3(m_camera.world[3].x, m_camera.world[3].y, m_camera.world[3].z)); PxRigidDynamic* dynamicActorS = PxCreateDynamic(*g_Physics, transformS, sphere, *g_PhysicsMaterial, densityS); //add it to the physx scene g_PhysicsScene->addActor(*dynamicActorS); dynamicActorS->addForce(PxVec3(-m_camera.world[2].x, -m_camera.world[2].y, -m_camera.world[2].z)*muzzleSpeed, PxForceMode::eIMPULSE, true); }
void PhysXPhysics::AddShape(Actor* pActor, PxGeometry* geometry, float density, const std::string& physicsMaterial, bool gravityEnabled, float linearDamping, float angularDamping, const std::string& bodyType) { BE_ASSERT(pActor); ActorId actorId = pActor->GetId(); BE_ASSERTf(m_actorRigidBodyMap.find(actorId) == m_actorRigidBodyMap.end(), "Actor with more than one rigidbody"); Mat4x4 transform = Mat4x4::g_Identity; TransformComponent* pTransformComponent = pActor->GetComponent<TransformComponent>(TransformComponent::g_Name); if (pTransformComponent) { transform = pTransformComponent->GetTransform(); } else { //Doesnt work without transform BE_ERROR("Actor %s PhysicsComponent requires Shape to have Transform Component: %d", actorId); return; } PhysicsMaterialData material(LookupMaterialData(physicsMaterial)); PxMaterial* mat = m_pPhysicsSdk->createMaterial(material.m_friction, material.m_friction, material.m_restitution); Vec3 translation, scale; Quaternion rotation; bool ok = transform.Decompose(translation, rotation, scale); PxQuat pxRot; PxVec3 pxLoc; Vec3ToPxVec(translation, &pxLoc); QuaternionToPxQuat(rotation, &pxRot); PxTransform t(pxLoc, pxRot); if (bodyType == "Dynamic") { PxRigidDynamic* body = PxCreateDynamic(*m_pPhysicsSdk, t, *geometry, *mat, density); body->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, !gravityEnabled); PxRigidBodyExt::updateMassAndInertia(*body, density); body->setLinearDamping(linearDamping); body->setAngularDamping(angularDamping); m_pScene->addActor(*body); m_actorRigidBodyMap[actorId] = body; m_rigidBodyActorMap[body] = actorId; } else { BE_ERROR("[Physics] BodyType not supported: %s", bodyType.c_str()); return; } }
PxRigidActor* createActor( const PxGeometry& geom, double density, PxMaterial* mtl ) { if ( density>0.0 ) { PxRigidDynamic* actor = PxCreateDynamic( *SDK_OBJ, PxTransform::createIdentity(), geom, mtl ? *mtl : *DEF_MTL, density ); return actor; } else { PxRigidStatic* actor = PxCreateStatic( *SDK_OBJ, PxTransform::createIdentity(), geom, mtl ? *mtl : *DEF_MTL ); return actor; } }
void createChain(const PxTransform& t, PxU32 length, const PxGeometry& g, PxReal separation, JointCreateFunction createJoint) { PxVec3 offset(separation / 2, 0, 0); PxTransform localTm(offset); PxRigidDynamic* prev = NULL; for (PxU32 i = 0; i<length; i++) { PxRigidDynamic* current = PxCreateDynamic(*gPhysics, t*localTm, g, *gMaterial, 1.0f); (*createJoint)(prev, prev ? PxTransform(offset) : t, current, PxTransform(-offset)); gScene->addActor(*current); prev = current; localTm.p.x += separation; } }
// add some physics objects into the scene void AddPhyObjects() { PxRigidStatic* groundPlane = PxCreatePlane(*gPhysics, PxPlane(0, 1, 0, 0), *gMaterial); gScene->addActor(*groundPlane); PxShape* shape = gPhysics->createShape(PxBoxGeometry(1.0f, 1.0f, 1.0f), *gMaterial); PxTransform localTm(PxVec3(-3.0f, 5.0f, 0.f)); PxRigidDynamic* body = gPhysics->createRigidDynamic(localTm); body->attachShape(*shape); PxRigidBodyExt::updateMassAndInertia(*body, 10.0f); gScene->addActor(*body); shape->release(); shape = gPhysics->createShape(PxSphereGeometry(1.0f), *gMaterial); PxTransform localTmS(PxVec3(3.0f, 5.0f, 0.f)); body = gPhysics->createRigidDynamic(localTmS); body->attachShape(*shape); PxRigidBodyExt::updateMassAndInertia(*body, 10.0f); gScene->addActor(*body); shape->release(); PxRigidDynamic* dynamic = PxCreateDynamic(*gPhysics, PxTransform(PxVec3(0, 20, 20)), PxSphereGeometry(1), *gMaterial, 10.0f); dynamic->setAngularDamping(0.5f); dynamic->setLinearVelocity(PxVec3(0, -5, -10)); gScene->addActor(*dynamic); // add capsule into the scene shape = gPhysics->createShape(PxCapsuleGeometry(1.0f, 3.0f), *gMaterial); PxTransform localTmC(PxVec3(3.0f, 5.0f, -3.f)); body = gPhysics->createRigidDynamic(localTmC); body->attachShape(*shape); PxRigidBodyExt::updateMassAndInertia(*body, 10.0f); gScene->addActor(*body); // add a static box as the trigger shape = gPhysics->createShape(PxBoxGeometry(1.0f, 1.0f, 1.0f), *gMaterial); PxTransform localTmTrigger(PxVec3(0.0f, 1.0f, -10.f)); body = gPhysics->createRigidDynamic(localTmTrigger); shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false); shape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true); body->attachShape(*shape); body->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, true); gScene->addActor(*body); shape->release(); }
void KinematicController::CreatePhyiscsAgent() { PxTransform transform(PxVec3(m_position.x, m_position.y, m_position.z)); //transform = PxVec3(0, 0, 0); float density = 50; float halfHeight = m_height / 2; PxCapsuleGeometry capsule(m_radius, m_height); m_actor = PxCreateDynamic(*m_physicsObject->m_Physics, transform, capsule, *m_physicsObject->m_PhysicsMaterial, density); m_physicsObject->m_PhysicsScene->addActor(*m_actor); m_physicsObject->m_boxActors.push_back(m_actor); }
void Physx1::MakeBlocks() { PxTransform m_transform(PxVec3(0, 20, 0)); PxBoxGeometry box(2, 2, 2); float density = 50; m_dynamicActor = PxCreateDynamic(*m_Physics, m_transform, box, *m_PhysicsMaterial, density); m_boxActors.push_back(m_dynamicActor); int size = m_boxActors.size(); m_PhysicsScene->addActor(*m_boxActors[size - 1]); m_boxCount++; std::cout << "Boxes in Scene: " << m_boxCount << "\n"; }
xmodel::xmodel(PxPhysics * mPhysics,PxScene* mScene,double mass): pos(0,0,0) { PxMaterial* aMaterial; aMaterial = mPhysics->createMaterial(0.01f, 0.01f, 0.1); //static friction, dynamic friction, restitution if(!aMaterial) printf("createMaterial failed!"); PxTransform pt(PxVec3(0,0,0.3),PxQuat(0,0,0,1)); actor = PxCreateDynamic(*mPhysics, pt, PxBoxGeometry(0.5,0.5,0.128),*aMaterial, mass*4); actor->setLinearVelocity(PxVec3(0,0,0)); mScene->addActor(*actor); physx::PxVec3 p=actor->getGlobalPose().p; pos.x=p.x; pos.y=p.y; pos.z=p.z; }
PxRigidDynamic* PxCreateDynamic(PxPhysics& sdk, const PxTransform& transform, const PxGeometry& geometry, PxMaterial& material, PxReal density, const PxTransform& shapeOffset) { PX_CHECK_AND_RETURN_NULL(transform.isValid(), "PxCreateDynamic: transform is not valid."); PX_CHECK_AND_RETURN_NULL(shapeOffset.isValid(), "PxCreateDynamic: shapeOffset is not valid."); if(!isDynamicGeometry(geometry.getType()) || density <= 0.0f) return NULL; PxShape* shape = sdk.createShape(geometry, material, true); if(!shape) return NULL; shape->setLocalPose(shapeOffset); PxRigidDynamic* body = shape ? PxCreateDynamic(sdk, transform, *shape, density) : NULL; shape->release(); return body; }
void FBXActor::createCollisionShapes(PhysicsDemoScene *a_app) { float density = 300; //pole PxBoxGeometry box = PxBoxGeometry(0.1f,4,0.1f); PxTransform transform(*((PxMat44*)(&m_world))); //cast from glm to PhysX matrices PxRigidDynamic* dynamicActor = PxCreateDynamic(*a_app->g_Physics, transform, box, *a_app->g_PhysicsMaterial, density); dynamicActor->userData = this; //set the user data to point at this FBXActor class //offset int nShapes = dynamicActor->getNbShapes(); PxShape* shapes; dynamicActor->getShapes(&shapes, nShapes); PxTransform relativePose = PxTransform(PxVec3(0.0f,4.0f,0.0f)); shapes->setLocalPose(relativePose); //head box = PxBoxGeometry(0.8f,0.5f,0.3f); relativePose = PxTransform(PxVec3(0.0f,2.0f,0.0f)); PxShape* shape = dynamicActor->createShape(box, *a_app->g_PhysicsMaterial); if (shape) { shape->setLocalPose(relativePose); } PxRigidBodyExt::updateMassAndInertia(*dynamicActor, (PxReal)density); //add to scene a_app->g_PhysicsScene->addActor(*dynamicActor); a_app->g_PhysXActors.push_back(dynamicActor); }
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(); } } } }
/** * Method is used to add new dynamic actor to physics scene. * @param entity is pointer to scene entity which will be added to physics simulation. * @param type is enumeration of shape type. * @param filterGroup is actor own id. * @param filterMask is mask to filter pairs that trigger a contact callback. */ void PhysicsManager::addDynamicActor(SceneEntity* entity, ShapeType type, PxU32 filterGroup, PxU32 filterMask) { PxRigidDynamic* actor = nullptr; PxVec3 position = PxVec3(entity->entityState.position[0],entity->entityState.position[1],entity->entityState.position[2]); PxQuat orientation = PxQuat(entity->entityState.orientation[0],entity->entityState.orientation[1],entity->entityState.orientation[2],entity->entityState.orientation[3]); PxReal density = 1.0f; PxTransform transformation = PxTransform(position,orientation); if(type == BOX) { float x = (entity->entityGeometry.geometryBox->max.x() - entity->entityGeometry.geometryBox->min.x())*entity->entityState.scale.x()*0.5f; float y = (entity->entityGeometry.geometryBox->max.y() - entity->entityGeometry.geometryBox->min.y())*entity->entityState.scale.y()*0.5f; float z = (entity->entityGeometry.geometryBox->max.z() - entity->entityGeometry.geometryBox->min.z())*entity->entityState.scale.z()*0.5f; PxVec3 dimensions(x,y,z); actor = PxCreateDynamic(*physicsSDK,transformation,PxBoxGeometry(dimensions),*materials[0].second,density); PxRigidBodyExt::updateMassAndInertia(*actor, density); } else if(type == SPHERE) { float radius = entity->entityGeometry.geometrySphere->sphereRadius; actor = PxCreateDynamic(*physicsSDK,transformation,PxSphereGeometry(radius),*materials[0].second,density); } else if(type == CAPSULE) { float radius = entity->entityGeometry.geometrySphere->sphereRadius; actor = PxCreateDynamic(*physicsSDK,transformation,PxCapsuleGeometry(radius/2, radius),*materials[0].second,density); } else if(type == CONVEX) { /*int vertsCount = entity->entityGeometry.geometryMesh->getVerticesAmount(); AyumiUtils::Vertex<>* verts = entity->entityGeometry.geometryMesh->getVertices(); PxVec3* convexVerts = new PxVec3[vertsCount]; for(int i = 0; i < vertsCount; ++i) convexVerts[i] = PxVec3(verts[i].x,verts[i].y,verts[i].z); PxConvexMeshDesc convexDesc; convexDesc.points.count = entity->entityGeometry.geometryMesh->getVerticesAmount(); convexDesc.points.stride = sizeof(PxVec3); convexDesc.points.data = convexVerts; convexDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX; MemoryWriteBuffer buf; if(cooking->cookConvexMesh(convexDesc, buf)) { PxConvexMesh* convexMesh = physicsSDK->createConvexMesh(MemoryReadBuffer(buf.data)); actor = PxCreateDynamic(*physicsSDK,transformation,PxConvexMeshGeometry(convexMesh,PxMeshScale()),*materials[0].second,density); } else { Logger::getInstance()->saveLog(Log<string>("Convex Mesh creation error occurred!")); return; }*/ //delete[] convexVerts; } else { Logger::getInstance()->saveLog(Log<string>("Dynamic Actor shape creation error occurred!")); return; } if(!actor) Logger::getInstance()->saveLog(Log<string>("Static Actor creation error occurred!")); PxRigidBodyExt::updateMassAndInertia(*actor, density); actor->setAngularDamping(0.75); actor->setLinearVelocity(PxVec3(0,0,0)); actor->setName(entity->entityName.c_str()); setupFiltering(actor,filterGroup,filterMask); scene->addActor(*actor); DynamicActor* d = new DynamicActor(); d->entityLogic = entity; d->entityPhysics = actor; d->shapesAmount = actor->getNbShapes(); d->shapes = new PxShape*[d->shapesAmount]; dynamicActors.push_back(d); }
void createActors() { // Create Material physx::PxMaterial* cubeMaterial = gPhysicsSDK->createMaterial(0.5f, 0.5f, 0.5f); physx::PxMaterial* sphereMaterial = gPhysicsSDK->createMaterial(0.6f, 0.1f, 0.6f); physx::PxMaterial* planeMaterial = gPhysicsSDK->createMaterial(0.5f, 0.5f, 0.5f); // Create Floor physx::PxReal d = 0.0f; physx::PxTransform pose = physx::PxTransform( physx::PxVec3( 0.0f, 0, 0.0f ), physx::PxQuat( physx::PxHalfPi, physx::PxVec3( 0.0f, 0.0f, 1.0f ))); physx::PxRigidStatic* plane = gPhysicsSDK->createRigidStatic(pose); if (!plane) std::cerr << "create plane failed!" << std::endl; physx::PxShape* shape = plane->createShape(physx::PxPlaneGeometry(), *planeMaterial); if (!shape) std::cerr << "create shape failed!" << std::endl; gScene->addActor(*plane); float gap_x = box_size.x * 2.0f; float gap_y = box_size.y * 2.0f; // Create boxes if(total_boxes > 0) { physx::PxReal density = 1.0f; physx::PxTransform boxTransform(physx::PxVec3(0.0f, 0.0f, 0.0f)); physx::PxBoxGeometry boxGeometry(box_size); for(float i = -box_grid_width/2.0f; i < box_grid_width/2.0f; ++i) { for(unsigned int j = 0; j < box_grid_height; ++j) { boxTransform.p = physx::PxVec3((i * gap_x) + 1.0f, (j * gap_y) + 1.0f, 0.0f); physx::PxRigidDynamic *boxActor = PxCreateDynamic(*gPhysicsSDK, boxTransform, boxGeometry, *cubeMaterial, density); if (!boxActor) std::cerr << "create actor failed!" << std::endl; boxActor->setAngularDamping(0.75f); boxActor->setLinearDamping(0.01f); boxActor->setMass(10.0f); gScene->addActor(*boxActor); boxes_actors.push_back(boxActor); } } } // Create spheres if(total_spheres > 0) { physx::PxReal density = 2.0f; physx::PxTransform sphereTransform(physx::PxVec3(0.0f, 0.0f, 0.0f)); physx::PxSphereGeometry sphereGeometry(sphere_radius); for(unsigned int i = 0; i < total_spheres; ++i) { sphereTransform.p = physx::PxVec3(0.0f, 0.0f, -30.0f); physx::PxRigidDynamic *sphereActor = PxCreateDynamic(*gPhysicsSDK, sphereTransform, sphereGeometry, *sphereMaterial, density); if (!sphereActor) std::cerr << "create actor failed!" << std::endl; sphereActor->setAngularDamping(0.2f); sphereActor->setLinearDamping(0.1f); sphereActor->setMass(5.0f); sphereActor->setLinearVelocity(physx::PxVec3(1.3f, box_grid_height * 2, 60.0f)); gScene->addActor(*sphereActor); spheres_actors.push_back(sphereActor); } } }