// 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 GOCCharacterController::Create(Ogre::Vector3 dimensions) { QuitAllNewsgroups(); JoinNewsgroup(GlobalMessageIDs::PHYSICS_SUBSTEP); JoinNewsgroup(GlobalMessageIDs::PHYSICS_END); if (dimensions.x == 0 || dimensions.y == 0 || dimensions.z == 0) dimensions = Ogre::Vector3(1,1,1); mFreezed = false; mSpeedFactor = 1; mDirection = Ogre::Vector3(0,0,0); mDimensions = dimensions; PxMaterial *mat = &OgrePhysX::World::getSingleton().getDefaultMaterial(); mActor = Main::Instance().GetPhysXScene()->createRigidDynamic( PxBoxGeometry(mDimensions.x*0.5f, mDimensions.y*0.5f, mDimensions.z*0.5f), mDensity, *mat, PxTransform(PxVec3(0, mDimensions.y*0.5f, 0))); mActor.getFirstShape()->setSimulationFilterData(PhysXFilterData::Instance().Character); mActor.getFirstShape()->setQueryFilterData(PhysXFilterData::Instance().Character); mActor.getPxActor()->userData = mOwnerGO.lock().get(); mActor.getPxActor()->setMassSpaceInertiaTensor(PxVec3(0,1,0)); mActor.getPxActor()->setSolverIterationCounts(8); }
// Set up PhysX void InitializePhysX() { gFoundation = PxCreateFoundation(PX_FOUNDATION_VERSION, gAllocator, gErrorCallback); gPvd = PxCreatePvd(*gFoundation); PxPvdTransport* transport = PxDefaultPvdSocketTransportCreate("127.0.0.1", 5425, 10); gPvd->connect(*transport, PxPvdInstrumentationFlag::eALL); gPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale(), true, gPvd); PxSceneDesc sceneDesc(gPhysics->getTolerancesScale()); sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f); gDispatcher = PxDefaultCpuDispatcherCreate(2); sceneDesc.cpuDispatcher = gDispatcher; sceneDesc.filterShader = contactReportFilterShader/*PxDefaultSimulationFilterShader*/; sceneDesc.simulationEventCallback = &gContactReportCallback; // contact callback sceneDesc.contactModifyCallback = &gModContactReportCallback; // modification contact callback gScene = gPhysics->createScene(sceneDesc); PxPvdSceneClient* pvdClient = gScene->getScenePvdClient(); if (pvdClient) { pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONSTRAINTS, true); pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONTACTS, true); pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_SCENEQUERIES, true); } gMaterial = gPhysics->createMaterial(0.5f, 0.5f, 0.6f); // add some physics objects AddPhyObjects(); createChain(PxTransform(PxVec3(10.0f, 30.0f, -30.0f)), 5, PxBoxGeometry(2.0f, 0.5f, 0.5f), 4.0f, createLimitedSpherical); createChain(PxTransform(PxVec3(0.0f, 30.0f, -30.0f)), 5, PxBoxGeometry(2.0f, 0.5f, 0.5f), 4.0f, createBreakableFixed); createChain(PxTransform(PxVec3(-10.0f, 30.0f, -30.0f)), 5, PxBoxGeometry(2.0f, 0.5f, 0.5f), 4.0f, createDampedD6); gScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0f); gScene->setVisualizationParameter(PxVisualizationParameter::eACTOR_AXES, 1.0f); gScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 2.0f); gScene->setVisualizationParameter(PxVisualizationParameter::eCONTACT_NORMAL, 2.0f); }
void JF::Component::ColisionBox::SethalfExtents(XMFLOAT3 _vhalfExtents) { // 1) m_Size = _vhalfExtents; // 2) if (m_pRigidBody != nullptr && m_pShape != nullptr) { m_pRigidBody->detachShape(*m_pShape); m_pShape->setGeometry(PxBoxGeometry(m_Size.x, m_Size.y, m_Size.z)); m_pRigidBody->attachShape(*m_pShape); } }
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 BoxCollider::OnInitShape() { PxMaterial* mtl = gPhysics->createMaterial(0.5f, 0.5f, 0.1f); CHECK(mtl); mShape = mActor->createShape(PxBoxGeometry(mSize.x, mSize.y, mSize.z), *mtl); CHECK(mShape); mGizmo = new Gizmo(mTransform); mGizmo->SetMesh(ShapeMesh::CreateBox(mSize)); CHECK(mGizmo); Exit0: ; }
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; }
std::vector<int> RayCastManagerImpl::OverlapBoxMultipleHits(const XMFLOAT3& p_origin, const XMFLOAT3& p_halfExtents) { std::vector<int> returnVec; PxVec3 origin = PxVec3(p_origin.x, p_origin.y, p_origin.z); PxVec3 halfExtents = PxVec3(p_halfExtents.x, p_halfExtents.y, p_halfExtents.z); PxTransform position = PxTransform(origin); PxOverlapHit hitBuffer[256]; // Max 256 hits will be stored? PxOverlapBuffer hit(hitBuffer, 256); // Used to save the hit PxBoxGeometry box = PxBoxGeometry(halfExtents); /// Paramters for the sweep // PxGeometry* geometry bool status = m_utils.m_worldScene->overlap(box, position, hit); size_t numberOfHits = hit.getNbAnyHits(); for(size_t i = 0; i < numberOfHits; i++) { // Start with checking static and dynamic rigid bodies unordered_map<PxRigidActor*, int> idsByRigidBody = m_utils.m_rigidBodyManager->GetIDsByBodies(); if(idsByRigidBody.find(hit.getAnyHit(i).actor) != idsByRigidBody.end()) { returnVec.push_back(idsByRigidBody.find(hit.getAnyHit(i).actor)->second); } else { // Nothing } // Now comes the difficult task of checking vs character controllers unordered_map<PxController*, int> idsByCharacterController = m_utils.m_characterControlManager->GetIdsByControllers(); for(auto pairs : idsByCharacterController) // Loop through every pair in the list { if(pairs.first->getActor() == hit.getAnyHit(i).actor) // The first part contains the actor pointer { returnVec.push_back(pairs.second); // If this is true we found a hit vs character controller, second contains ID } } } return returnVec; }
PxActor* World::addBox(const ofVec3f& size, const ofVec3f& pos, const ofQuaternion& rot, float mass) { assert(inited); return createRigidBody(PxBoxGeometry(toPx(size)), mass, pos, rot); }
void BoundingBox::SetScale(OPvec3 scale) { scale = scale; PxBoxGeometry geo = PxBoxGeometry(scale.x / 2.0, scale.y / 2.0, scale.z / 2.0); shapes->setGeometry(geo); }
/** * 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 JF::Component::ColisionBox::Reset() { // 1) auto* pTransform = GetOwner()->GetComponent<JF::Component::Transform>(); XMFLOAT4X4& rMatrix = pTransform->GetTransformMatrix(); assert(pTransform != nullptr); // 2) m_pShape = m_pPXDevice->GetPhysics()->createShape( PxBoxGeometry(m_Size.x, m_Size.y, m_Size.z), *m_pPXDevice->GetMaterial()); // 3) PxTransform localTm( PxMat44( PxVec4(rMatrix._11, rMatrix._12, rMatrix._13, rMatrix._14), PxVec4(rMatrix._21, rMatrix._22, rMatrix._23, rMatrix._24), PxVec4(rMatrix._31, rMatrix._32, rMatrix._33, rMatrix._34), PxVec4(rMatrix._41, rMatrix._42, rMatrix._43, rMatrix._44) )); // 4) m_pRigidBody = m_pPXDevice->GetPhysics()->createRigidDynamic(localTm); m_pRigidBody->attachShape(*m_pShape); PxRigidBodyExt::updateMassAndInertia(*m_pRigidBody, 10.0f); m_pRigidBody->setLinearVelocity(PxVec3(0, 10, 2.0f)); m_pPXDevice->GetScene()->addActor(*m_pRigidBody); // 5) float cX = 0.0f; float cY = 0.0f; float cZ = 0.0f; float eX = 1.0f; float eY = 1.0f; float eZ = 1.0f; // 정점 JF::Vertex::PosColor vertices[] = { // 1, 1, 1 (0) { XMFLOAT3(cX + eX, cY + eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // 1, 1, -1 (1) { XMFLOAT3(cX + eX, cY + eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, 1, 1 (2) { XMFLOAT3(cX + -eX, cY + eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, 1, -1 (3) { XMFLOAT3(cX + -eX, cY + eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // 1, -1, 1 (4) { XMFLOAT3(cX + eX, cY + -eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // 1, -1, -1 (5) { XMFLOAT3(cX + eX, cY + -eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, -1, 1 (6) { XMFLOAT3(cX + -eX, cY + -eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, // -1, -1, -1 (7) { XMFLOAT3(cX + -eX, cY + -eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) } }; m_VertexSize = ARRAYSIZE(vertices); // 인덱스 UINT indices[] = { // Top 0,1, 1,3, 3,2, 0,2, // Bottom 4,5, 5,7, 7,6, 6,4, // Top - Bottom 0,4, 1,5, 2,6, 3,7 }; m_indexCnt = ARRAYSIZE(indices); // Create gRENDERER->CreateVertexBuffer(&vertices[0], sizeof(JF::Vertex::PosColor) * m_VertexSize, &m_VertBuff); gRENDERER->CreateIndexBuffer(&indices[0], sizeof(UINT) * m_indexCnt, &m_IndexBuff); }
void PhysXPhysics::VAddBox(Vec3 scale, Actor* gameActor, const std::string& densityStr, const std::string& physicsMaterial, bool gravityEnabled, float linearDamping, float angularDamping, Mat4x4 relativeTransform, const std::string& bodyType) { float density = LookupDensity(densityStr); AddShape(gameActor, &PxBoxGeometry(scale.x, scale.y, scale.z), density, physicsMaterial, gravityEnabled, linearDamping, angularDamping, bodyType); }