コード例 #1
0
ファイル: scene.cpp プロジェクト: crucifix35/base-pro-edition
void Scene::initializePhysics(void)
{
    // database record for scene location
    database::LocationInfo* locationInfo = database::LocationInfo::getRecord( _location->getDatabaseId() );

    // find collision geometry
    engine::IClump* collisionClump = NULL;
    callback::ClumpL clumpL;
    _extrasAsset->forAllClumps( callback::enumerateClumps, &clumpL );
    for( callback::ClumpI clumpI=clumpL.begin(); clumpI!=clumpL.end(); clumpI++ )
    {
        if( strcmp( (*clumpI)->getName(), "CollisionGeometry" ) == 0 )
        {
            collisionClump = (*clumpI);
            break;
        }
    }
    assert( collisionClump != NULL );
    collisionClump->getFrame()->translate( Vector3f( 0,0,0 ) );
    collisionClump->getFrame()->getLTM();

    // determine physics scene bounds by collision geometry
    callback::AtomicL atomicL;
    collisionClump->forAllAtomics( callback::enumerateAtomics, &atomicL );
    assert( atomicL.size() == 1 );
    _collisionGeometry = *atomicL.begin();
    Vector3f sceneInf = _collisionGeometry->getAABBInf();
    Vector3f sceneSup = _collisionGeometry->getAABBSup();
    _phSceneBounds = PxBounds3 (PxVec3(sceneInf[0], sceneInf[1], sceneInf[2]),
                                PxVec3(sceneSup[0], sceneSup[1], sceneSup[2]));

    // determine limits of scene
    _phSceneLimits.maxNbActors = locationInfo->physicsLimits.numActors;
    _phSceneLimits.maxNbBodies = locationInfo->physicsLimits.numBodies;
    //_phSceneLimits.maxNbJoints = locationInfo->physicsLimits.numJoints;
    _phSceneLimits.maxNbDynamicShapes = locationInfo->physicsLimits.numDynamicShapes;
    _phSceneLimits.maxNbStaticShapes  = locationInfo->physicsLimits.numStaticShapes;

    // initialize physics scene
    PxSceneDesc _phSceneDesc(PxGetPhysics().getTolerancesScale());
    _phSceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
    _phSceneDesc.broadPhaseType = PxBroadPhaseType::eSAP;

    //PHYSX3 fix collision filters
    //_phSceneDesc.userNotify = NULL;
    //_phSceneDesc.userTriggerReport = this;
    //_phSceneDesc.userContactReport = this;
    //_phSceneDesc.maxTimestep = simulationStepTime;
    //_phSceneDesc.maxIter = 1;
    //_phSceneDesc.timeStepMethod = NX_TIMESTEP_VARIABLE;
    //_phSceneDesc.groundPlane = false;
    //_phSceneDesc.boundsPlanes = false;
    //_phSceneDesc.collisionDetection = true;
    _phSceneDesc.userData = this;

    // create scene
    //mNbThreads = PxMax(PxI32(shdfnd::Thread::getNbPhysicalCores())-1, 0);
    //#ifdef PX_PS3
    //	mNbThreads = 1; // known issue, 0 worker threads and SPU batched query can deadlock.
    //#endif
    //PHYSX3
    mNbThreads = 1;
    if(!_phSceneDesc.cpuDispatcher) {
        mCpuDispatcher = PxDefaultCpuDispatcherCreate(mNbThreads);
        //PHYSX3
        //if(!mCpuDispatcher)
        //	fatalError("PxDefaultCpuDispatcherCreate failed!");
        _phSceneDesc.cpuDispatcher    = mCpuDispatcher;
    }
    if(!_phSceneDesc.filterShader)
        _phSceneDesc.filterShader    = gDefaultFilterShader;

#ifdef PX_WINDOWS
    if(!_phSceneDesc.gpuDispatcher && mCudaContextManager) {
        _phSceneDesc.gpuDispatcher = mCudaContextManager->getGpuDispatcher();
    }
#endif
    _phScene = PxGetPhysics().createScene(_phSceneDesc);
    assert( _phScene );

    // post-creation scene settings
    _phScene->setVisualizationCullingBox(_phSceneBounds);
    _phScene->setLimits(_phSceneLimits);

    // default material
    PxMaterial* defaultMaterial = PxGetPhysics().createMaterial(0.25f, 0.25f, 0.5f);

    // add ground plane
    PxRigidStatic* groundPlane = PxCreatePlane(PxGetPhysics(), PxPlane(0,1,0,0), *defaultMaterial);
    _phScene->addActor(*groundPlane);

    // fixed flesh material
    _phFleshMaterial = PxGetPhysics().createMaterial(0.75f, 0.75f, 0.25f);
    _phFleshMaterial->setFrictionCombineMode(PxCombineMode::eMAX);
    _phFleshMaterial->setRestitutionCombineMode(PxCombineMode::eMULTIPLY);
    assert( _phFleshMaterial );

    // moving flesh material
    _phMovingFleshMaterial = PxGetPhysics().createMaterial(0.25f, 0.25f, 0.125f);
    _phMovingFleshMaterial->setFrictionCombineMode(PxCombineMode::eMAX);
    _phMovingFleshMaterial->setRestitutionCombineMode(PxCombineMode::eMULTIPLY);
    assert( _phMovingFleshMaterial );

    // cloth material
    _phClothMaterial = PxGetPhysics().createMaterial(0.995f, 0.995f, 0.05f);
    _phClothMaterial->setFrictionCombineMode(PxCombineMode::eMAX);
    _phClothMaterial->setRestitutionCombineMode(PxCombineMode::eMULTIPLY);
    assert( _phClothMaterial );

    // build terrain mesh data
    Matrix4f collisionGeometryLTM = _collisionGeometry->getFrame()->getLTM();
    Vector3f worldVertex;
    engine::Mesh* mesh = _collisionGeometry->getGeometry()->createMesh();
    _phTerrainVerts = new PxVec3[mesh->numVertices];
    _phTerrainTriangles = new PxU32[3*mesh->numTriangles];
    //PHYSX3
    //_phTerrainMaterials = new NxMaterialIndex[mesh->numTriangles];
    unsigned int i;
    for( i=0; i<mesh->numVertices; i++ )
    {
        worldVertex = Gameplay::iEngine->transformCoord( mesh->vertices[i], collisionGeometryLTM );
        _phTerrainVerts[i] = wrap( worldVertex );
    }
    for( i=0; i<mesh->numTriangles; i++ )
    {
        _phTerrainTriangles[i*3+0] = mesh->triangles[i].vertexId[0];
        _phTerrainTriangles[i*3+1] = mesh->triangles[i].vertexId[1];
        _phTerrainTriangles[i*3+2] = mesh->triangles[i].vertexId[2];
        //PHYSX3
        //_phTerrainMaterials[i] = 0;
    }

    // initialize terrain descriptor
    _phTerrainDesc.points.count = mesh->numVertices;
    _phTerrainDesc.points.data = _phTerrainVerts;
    _phTerrainDesc.points.stride = sizeof(PxVec3);
    _phTerrainDesc.triangles.count = mesh->numTriangles;
    _phTerrainDesc.triangles.data = _phTerrainTriangles;
    _phTerrainDesc.triangles.stride = 3 * sizeof(PxU32);

    //PHYSX3
    //_phTerrainDesc.materialIndexStride = sizeof( PxMaterialIndex );
    //_phTerrainDesc.materialIndices = _phTerrainMaterials;
    //_phTerrainDesc.heightFieldVerticalAxis = NX_NOT_HEIGHTFIELD;
    //_phTerrainDesc.heightFieldVerticalExtent = -1000;

    // cook terrain

    _phTerrainMesh = Gameplay::pxCooking->createTriangleMesh(_phTerrainDesc, PxGetPhysics().getPhysicsInsertionCallback());

    assert (_phTerrainMesh);

    _phTerrain = PxGetPhysics().createRigidStatic(PxTransform(PxVec3(0.0f, 1.0f, 0.0f), PxQuat(PxHalfPi / 60.0f, PxVec3(0.0f, 1.0f, 0.0f))));

    assert (_phTerrain);

    PxTriangleMeshGeometry triGeom(_phTerrainMesh);
    PxShape* triangleMeshShape = _phTerrain->createShape(triGeom, *defaultMaterial);
    assert (triangleMeshShape);

    _phScene->addActor(*_phTerrain);

    //PHYSX3
    // retrieve terrain shape
    _phTerrain->getShapes(&_phTerrainShape, 1);
    assert( _phTerrainShape );

    Gameplay::iEngine->releaseMesh( mesh );
}
コード例 #2
0
PxBounds3 Gu::TriangleMesh::refitBVH()
{
	Ps::getFoundation().error(PxErrorCode::eINVALID_OPERATION, __FILE__, __LINE__, "PxTriangleMesh::refitBVH() is only supported for meshes with PxMeshMidPhase::eBVHDynamic.");

	return PxBounds3(mAABB.getMin(), mAABB.getMax());
}
コード例 #3
0
PxBounds3 NpSpatialIndex::getBounds(PxSpatialIndexItemId /*id*/) const
{
	return PxBounds3();
}