Esempio n. 1
0
void InnerWalls::Create()
{
	PxRigidStatic* rgd = PHYSICS->createRigidStatic(m_pose);
	
	Board* b = const_cast<Board*>(P::board);

	// Determine Poses
	Transform p_pLaneWall, p_pLaneWallTp, p_pLaneWallExt, p_fWallLft, p_fWallRgt;

	p_pLaneWall		= Transform(b->Right()	+ Vec3(b->PlungerLaneWidth() + (m_width*4), 0, b->Bottom().z+m_geometrys[IW_ID_PLUNGE_LN_WALL].box().halfExtents.z));
	p_pLaneWallTp = Transform(b->Top() - Vec3(m_width * 2, 0, (b->PlungerLaneWidth() * 3) + (m_width * 6)));

	// Create Shapes
	PxShape* pL		   = rgd->createShape(m_geometrys[IW_ID_PLUNGE_LN_WALL].box(), *m_material); // Plunger Lane Wall
	PxShape* PlT	   = rgd->createShape(m_geometrys[IW_ID_PLUNGE_LN_WALL_TP].box(), *m_material); // Plunger Lane Wall Top

	// Set Local Poses
	pL->setLocalPose(p_pLaneWall);
	PlT->setLocalPose(p_pLaneWallTp);

	// Set Global Pose
	rgd->setGlobalPose(P::board->Pose() * Transform(0, m_height*2, 0));

	m_actor.staticActor = rgd;

	m_actor.staticActor->userData = &m_color;
}
Esempio n. 2
0
void SampleParticles::createDrain() 
{
	float lakeHeight = 5.5f;
	
	//Creates a drain plane for the particles. This is good practice to avoid unnecessary 
	//spreading of particles, which is bad for performance. The drain represents a lake in this case.
	{
		PxRigidStatic* actor = getPhysics().createRigidStatic(PxTransform(PxVec3(0.0f, lakeHeight - 1.0f, 0.0f), PxQuat(PxHalfPi, PxVec3(0,0,1))));
		runtimeAssert(actor, "PxPhysics::createRigidStatic returned NULL\n");
		PxShape* shape = actor->createShape(PxPlaneGeometry(), getDefaultMaterial());
		runtimeAssert(shape, "PxRigidStatic::createShape returned NULL\n");
		shape->setSimulationFilterData(collisionGroupDrain);
		shape->setFlags(PxShapeFlag::ePARTICLE_DRAIN | PxShapeFlag::eSIMULATION_SHAPE);
		getActiveScene().addActor(*actor);
		mPhysicsActors.push_back(actor);
	}
	
	//Creates the surface of the lake (the particles actually just collide with the underlaying drain).
	{
		PxBoxGeometry bg;
		bg.halfExtents = PxVec3(130.0f, lakeHeight + 1.0f, 130.0f);
		PxRigidStatic* actor = getPhysics().createRigidStatic(PxTransform(PxVec3(0.0f, 0.0f, 0.0f), PxQuat::createIdentity()));
		runtimeAssert(actor, "PxPhysics::createRigidStatic returned NULL\n");
		PxShape* shape = actor->createShape(bg, getDefaultMaterial());
		runtimeAssert(shape, "PxRigidStatic::createShape returned NULL\n");
		shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
		shape->setFlag(PxShapeFlag::eSCENE_QUERY_SHAPE, false);
		getActiveScene().addActor(*actor);
		mPhysicsActors.push_back(actor);
		createRenderObjectsFromActor(actor, getMaterial(MATERIAL_LAKE));
	}
}
Esempio n. 3
0
		/**
		 * Method is used to add heightfield actor to physics scene.
		 * @param	entity is actor logic object.
		 * @param	heightData is terrain height map data.
		 * @param	params is terrain parameters: heightmap size, row/column scale, max. height.
		 * @param	filterGroup is actor own id.
		 * @param	filterMask is mask to filter pairs that trigger a contact callback.
		 */
		void PhysicsManager::addHeightFieldActor(SceneEntity* entity, unsigned char* heightData, const Vector4D& params, PxU32 filterGroup, PxU32 filterMask)
		{
			int terrainSize = static_cast<int>(params.x());

			PxHeightFieldSample* samples = new PxHeightFieldSample[terrainSize*terrainSize];
			PxHeightFieldDesc descriptor;
			descriptor.nbColumns = static_cast<int>(terrainSize);
			descriptor.nbRows = static_cast<int>(terrainSize);
	
			for(int i = 0; i < terrainSize*terrainSize; ++i)
				samples[i].height = static_cast<PxI16>(heightData[i]);

			descriptor.samples.data = samples;
			descriptor.samples.stride = sizeof(PxHeightFieldSample);

			PxHeightField* heightField = physicsSDK->createHeightField(descriptor);
			PxHeightFieldGeometry geometry(heightField, PxMeshGeometryFlags(),params.w()*0.00390625f,params.y(),params.z());
			PxTransform transformation = PxTransform(PxVec3(-terrainSize*0.5f*params.y(),0.0f,-terrainSize*0.5f*params.z()),PxQuat::createIdentity());

			PxRigidStatic* heightFieldActor = physicsSDK->createRigidStatic(transformation);
			PxShape* aHeightFieldShape = heightFieldActor->createShape(geometry,*materials[0].second);
			heightFieldActor->setName(entity->entityName.c_str());
			setupFiltering(heightFieldActor,filterGroup,filterMask);
			scene->addActor(*heightFieldActor);
			
			StaticActor* s = new StaticActor();
			s->entityLogic = entity;
			s->entityPhysics = heightFieldActor;
			staticActors.push_back(s);
		}
Esempio n. 4
0
// Experiment specific functions called from ARSS.cpp
void CreateExperiment()
{
	// This is how you turn on the grid display. (There is also an XY Grid.)
	gDebug.bXZGridOn=true;

	// This is how you create a ground plane (and save its address in gExp.VIPs).
	CreateGroundPlane();

	// This is how you create a compound, multi-shape, static actor.
	PxRigidStatic* basket = gPhysX.mPhysics->createRigidStatic(PxTransform(PxVec3(0)));
	if (!basket)
		ncc__error("basket fail!");
	PxMaterial* defmat=gPhysX.mDefaultMaterial;
	PxBoxGeometry base(0.5,0.1,0.5);
	PxBoxGeometry pole(0.05,1,0.05);
	PxBoxGeometry board(0.5,0.5,0.01);
	PxBoxGeometry hoopel(0.01,0.01,0.15);
	basket->createShape(base,*defmat,PxTransform(PxVec3(0,0.1,0)));
	basket->createShape(pole,*defmat,PxTransform(PxVec3(0,1,0)));
	PxShape* sboard = basket->createShape(board,*defmat,PxTransform(PxVec3(0,2,0.05)));
	PxShape* shoopel1 = basket->createShape(hoopel,*defmat,PxTransform(PxVec3(-0.15,2,0.15+0.06)));
	PxShape* shoopel2 = basket->createShape(hoopel,*defmat,PxTransform(PxVec3(+0.15,2,0.15+0.06)));
	PxShape* shoopel3 = basket->createShape(hoopel,*defmat,PxTransform(PxVec3(+0.00,2,0.30+0.05),PxQuat(PxPi/2,PxVec3(0,1,0))));

	gPhysX.mScene->addActor(*basket);
	
	// We saved the pointers to the shapes we wish to color separately, with a call to the convenience function...
	ColorShape(sboard, ncc::rgb::yLightYellow);

	// ... or manually (in case we wish to be efficient with duplicate colors).
	gColors.colorBucket.push_back(vector<GLubyte>(3));
	gColors.colorBucket.back()[0]=ncc::rgb::grBlack[0];
	gColors.colorBucket.back()[1]=ncc::rgb::grBlack[1];
	gColors.colorBucket.back()[2]=ncc::rgb::grBlack[2];
	shoopel1->userData=&(gColors.colorBucket.back()[0]);
	shoopel2->userData=&(gColors.colorBucket.back()[0]);
	shoopel3->userData=&(gColors.colorBucket.back()[0]);

	// We signal that the experiment is ready for simulation by setting this flag.
	gSim.isRunning = true;
}
Esempio n. 5
0
void InitPhysX()
{
	//Creating foundation for PhysX
	gFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);

	//Creating instance of PhysX SDK
	gPhysicsSDK = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale());

	if (gPhysicsSDK == NULL)
	{
		cerr << "Error creating PhysX3 device, Exiting..." << endl;
		exit(1);
	}


	//Creating scene
	PxSceneDesc sceneDesc(gPhysicsSDK->getTolerancesScale());	//Descriptor class for scenes 

	sceneDesc.gravity = PxVec3(0.0f, -9.8f, 0.0f);		//Setting gravity
	sceneDesc.cpuDispatcher = PxDefaultCpuDispatcherCreate(1);	//Creates default CPU dispatcher for the scene
	sceneDesc.filterShader = PxDefaultSimulationFilterShader;	//Creates default collision filter shader for the scene

	gScene = gPhysicsSDK->createScene(sceneDesc);				//Creates a scene 


	//Creating PhysX material
	PxMaterial* material = gPhysicsSDK->createMaterial(0.5, 0.5, 0.5); //Creating a PhysX material



	//---------Creating actors-----------]

	//1-Creating static plane	 
	PxTransform planePos = PxTransform(PxVec3(0.0f), PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f)));	//Position and orientation(transform) for plane actor  
	PxRigidStatic* plane = gPhysicsSDK->createRigidStatic(planePos);								//Creating rigid static actor	
	plane->createShape(PxPlaneGeometry(), *material);												//Defining geometry for plane actor
	gScene->addActor(*plane);																		//Adding plane actor to PhysX scene


	//2-Creating dynamic cube																		 
	PxTransform		boxPos(PxVec3(0.0f, 10.0f, 0.0f));												//Position and orientation(transform) for box actor 
	PxBoxGeometry	boxGeometry(PxVec3(2, 2, 2));											//Defining geometry for box actor
	gBox = PxCreateDynamic(*gPhysicsSDK, boxPos, boxGeometry, *material, 1.0f);		//Creating rigid static actor
	gScene->addActor(*gBox);														//Adding box actor to PhysX scene

	//3-Creating dynamic cloth
	CreateCloth();

	gCloth->addCollisionPlane(PxClothCollisionPlane(PxVec3(0, 1, 0), 0.0f));
	gCloth->addCollisionConvex(1 << 0); // Convex references the first plane

}
Esempio n. 6
0
void SampleSubmarine::onInit()
{
#if defined(PX_PS3)
	mNbThreads = 1;
#elif defined(PX_X360)
	mNbThreads = 2;
#else
	mNbThreads = 3;
#endif

	PhysXSample::onInit();

	mSubmarineCameraController = SAMPLE_NEW(SubmarineCameraController)();
	setCameraController(mSubmarineCameraController);

	mApplication.setMouseCursorHiding(true);
	mApplication.setMouseCursorRecentering(true);
	mSubmarineCameraController->init(PxTransform::createIdentity());
	mSubmarineCameraController->setMouseSensitivity(0.5f);
	mSubmarineCameraController->setMouseLookOnMouseButton(false);

	getRenderer()->setAmbientColor(RendererColor(60, 60, 60));

	PxMaterial& material = getDefaultMaterial();
	material.setRestitution(0);
	material.setDynamicFriction(50.0f);
	material.setStaticFriction(50.0f);

	// set gravity
	getActiveScene().setGravity(PxVec3(0, -10.0f, 0));

	createMaterials();

	getRenderer()->setFog(SampleRenderer::RendererColor(16,16,40), 125.0f);

	PxRigidActor* heightField = loadTerrain("submarine_heightmap.bmp", 0.4f, 3.0f, 3.0f);
	if (!heightField) fatalError("Sample can not load file submarine_heightmap.bmp\n");

	setupFiltering(heightField, FilterGroup::eHEIGHTFIELD, FilterGroup::eCRAB);

	// create ceiling plane
	PxReal d = 60.0f;
	PxTransform pose = PxTransform(PxVec3(0.0f, d, 0.0f), PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, -1.0f)));
	PxRigidStatic* plane = getPhysics().createRigidStatic(pose);
	if(!plane) fatalError("createRigidStatic failed!");
	PxShape* shape = plane->createShape(PxPlaneGeometry(), material);
	if(!shape) fatalError("createShape failed!");
	getActiveScene().addActor(*plane);

	resetScene();
}
Esempio n. 7
0
void PhysicalTile::createPxObjects(PxRigidStatic & pxActor)
{
    const unsigned int numSamples = samplesPerAxis * samplesPerAxis;

    // create the list of material references
    PxHeightFieldSample * hfSamples = new PxHeightFieldSample[numSamples];
    PxMaterial ** materials = new PxMaterial*[m_elementNames.size()];
    for (uint8_t i = 0; i < m_elementNames.size(); ++i)
        materials[i] = Elements::pxMaterial(m_elementNames.at(i));

    // scale height so that we use the full range of PxI16=short
    PxReal heightScaleToWorld = m_terrain.settings.maxHeight / std::numeric_limits<PxI16>::max();
    assert(heightScaleToWorld >= PX_MIN_HEIGHTFIELD_Y_SCALE);
    float heightScaleToPx = std::numeric_limits<PxI16>::max() / m_terrain.settings.maxHeight;

    // copy the material and height data into the physx height field
    for (unsigned int row = 0; row < samplesPerAxis; ++row) {
        const unsigned int rowOffset = row * samplesPerAxis;
        for (unsigned int column = 0; column < samplesPerAxis; ++column) {
            const unsigned int index = column + rowOffset;
            hfSamples[index].materialIndex0 = hfSamples[index].materialIndex1 = elementIndexAt(index);
            hfSamples[index].height = static_cast<PxI16>(m_values.at(index) * heightScaleToPx);
        }
    }

    PxHeightFieldDesc hfDesc;
    hfDesc.format = PxHeightFieldFormat::eS16_TM;
    hfDesc.nbRows = samplesPerAxis;
    hfDesc.nbColumns = samplesPerAxis;
    hfDesc.samples.data = hfSamples;
    hfDesc.samples.stride = sizeof(PxHeightFieldSample);

    PxHeightField * pxHeightField = PxGetPhysics().createHeightField(hfDesc);

    assert(sampleInterval >= PX_MIN_HEIGHTFIELD_XZ_SCALE);
    // create height field geometry and set scale
    PxHeightFieldGeometry pxHfGeometry(pxHeightField, PxMeshGeometryFlags(),
        heightScaleToWorld, sampleInterval, sampleInterval);
    m_pxShape = pxActor.createShape(pxHfGeometry, materials, 1);

    assert(m_pxShape);

#ifdef PX_WINDOWS
    if (PhysicsWrapper::physxGpuAvailable())
        PxParticleGpu::createHeightFieldMirror(*pxHeightField, *PhysicsWrapper::getInstance()->cudaContextManager());
#endif

    delete[] hfSamples;
    delete[] materials;
}
Esempio n. 8
0
void Border::Create()
{
	PxRigidStatic* rgd = PHYSICS->createRigidStatic(m_pose);
	
	Board* b = const_cast<Board*>(P::board);

	// Determine Poses
	Transform p_top, p_bottom_lft, p_bottom_rgt, p_left, p_right;
	p_top =			Transform(b->Top()	  - Vec3(0,0,m_width));
	p_bottom_lft =  Transform(Vec3(b->Left().x - m_geometrys[BDR_ID_BTM_LFT].box().halfExtents.x, 0, b->Bottom().z));
	p_bottom_rgt =  Transform(Vec3(b->Right().x + m_geometrys[BDR_ID_BTM_RGT].box().halfExtents.x + b->PlungerLaneWidth() + m_width*3, 0, b->Bottom().z));
	p_left	 =		Transform(b->Left()	  - Vec3(m_width, 0, 0));
	p_right  =		Transform(b->Right()  + Vec3(m_width, 0, -m_width));

	// Create Shapes
	PxShape* sT = rgd->createShape(m_geometrys[BDR_ID_TOP].box(), *m_material);
	PxShape* sBl = rgd->createShape(m_geometrys[BDR_ID_BTM_LFT].box(), *m_material);
	PxShape* sBr = rgd->createShape(m_geometrys[BDR_ID_BTM_RGT].box(), *m_material);
	PxShape* sL = rgd->createShape(m_geometrys[BDR_ID_LFT].box(), *m_material);
	PxShape* sR = rgd->createShape(m_geometrys[BDR_ID_RGT].box(), *m_material);

	// Set Local Poses
	sT->setLocalPose(p_top);
	sBl->setLocalPose(p_bottom_lft);
	sBr->setLocalPose(p_bottom_rgt);
	sL->setLocalPose(p_left);
	sR->setLocalPose(p_right);
	
	// Set Global Pose
	Transform t = const_cast<Board*>(P::board)->Get().staticActor->getGlobalPose();
	rgd->setGlobalPose(P::board->Pose() * Transform(0, m_height*2, 0));

	m_actor.staticActor = rgd;

	m_actor.staticActor->userData = &m_color;
}
Esempio n. 9
0
		/**
		 * Method is used to add new static actor to physics scene. Now only plane shape can be used.
		 * @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::addStaticActor(SceneEntity* entity, ShapeType type, PxU32 filterGroup, PxU32 filterMask)
		{
			PxVec3 position;
			PxQuat orientation;
			PxTransform transformation;

			if(entity == nullptr)
			{
				 transformation = PxTransform(PxVec3(0.0f,1.0f,0.0f),PxQuat(PxHalfPi,PxVec3(0.0f, 0.0f, 1.0f)));
			}
			else
			{
				position = PxVec3(entity->entityState.position[0],entity->entityState.position[1],entity->entityState.position[2]);
				orientation = PxQuat(entity->entityState.orientation[0],entity->entityState.orientation[1],entity->entityState.orientation[2],entity->entityState.orientation[3]);
				transformation = PxTransform(position,orientation); 
			}

			PxRigidStatic* actor = nullptr;
			actor = physicsSDK->createRigidStatic(transformation);
			
			if(!actor)
				Logger::getInstance()->saveLog(Log<string>("Static Actor creation error occurred!"));
			
			PxShape* shape = nullptr;

			if(type == PLANE)
				shape = actor->createShape(PxPlaneGeometry(),*materials[0].second);
			else
				return;

			if(!shape)
				Logger::getInstance()->saveLog(Log<string>("Static Actor shape creation error occurred!"));
			
			actor->setName("World Plane");
			setupFiltering(actor,filterGroup,filterMask);
			scene->addActor(*actor);

			StaticActor* s = new StaticActor();
			s->entityLogic = entity;
			s->entityPhysics = actor;
			staticActors.push_back(s);
		}
Esempio n. 10
0
void PhysXInterface::createWorld( const osg::Plane& plane, const osg::Vec3& gravity )
{
    PxSceneDesc sceneDesc( _physicsSDK->getTolerancesScale() );
    sceneDesc.gravity = PxVec3(gravity[0], gravity[1], gravity[2]);
    sceneDesc.filterShader = &PxDefaultSimulationFilterShader;
    if ( !sceneDesc.cpuDispatcher )
    {
        PxDefaultCpuDispatcher* mCpuDispatcher = PxDefaultCpuDispatcherCreate(1);
        if ( !mCpuDispatcher )
           OSG_WARN << "Failed to create default Cpu dispatcher." << std::endl;
        sceneDesc.cpuDispatcher = mCpuDispatcher;
    }
    
    _scene = _physicsSDK->createScene( sceneDesc );
    if ( !_scene )
    {
        OSG_WARN <<"Failed to create the physics world." << std::endl;
        return;
    }
    _scene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0f );
    _scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f );
    _material = _physicsSDK->createMaterial( 0.5, 0.5, 0.5 );
    
    // Create the ground
    osg::Quat q; q.makeRotate( osg::Vec3f(1.0f, 0.0f, 0.0f), osg::Vec3f(plane[0], plane[1], plane[2]) );
    PxTransform pose( PxVec3(0.0f, 0.0f, plane[3]), PxQuat(q[0], q[1], q[2], q[3]) );
    PxRigidStatic* actor = _physicsSDK->createRigidStatic( pose );
    if ( !actor )
    {
        OSG_WARN <<"Failed to create the static plane." << std::endl;
        return;
    }
    
    PxShape* shape = actor->createShape( PxPlaneGeometry(), *_material );
    if ( !shape )
    {
        OSG_WARN <<"Failed to create the plane shape." << std::endl;
        return;
    }
    _scene->addActor( *actor );
}
Esempio n. 11
0
		void InitPhysX() {
		        gFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);
		        gPhysicsSDK = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale() );
		        if(gPhysicsSDK == NULL) {
		                cerr<<"Error create PhysX."<<endl;
		        }
		        PxSceneDesc sceneDesc(gPhysicsSDK->getTolerancesScale());
		        sceneDesc.gravity               = PxVec3(0.0f, -9.8f, 0.0f);
		        sceneDesc.cpuDispatcher = PxDefaultCpuDispatcherCreate(1);
		        sceneDesc.filterShader  = PxDefaultSimulationFilterShader;
		        gScene = gPhysicsSDK->createScene(sceneDesc);
		        PxMaterial* material = gPhysicsSDK->createMaterial(0.5,0.5,0.5);
		        PxTransform planePos =  PxTransform(PxVec3(0.0f),PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f)));
		        PxRigidStatic* plane =  gPhysicsSDK->createRigidStatic(planePos);
		        plane->createShape(PxPlaneGeometry(), *material);
		        gScene->addActor(*plane);
		        PxTransform             boxPos(PxVec3(0.0f, 10.0f, 0.0f));
		        PxBoxGeometry   boxGeometry(PxVec3(2,2,2));
		        gBox = PxCreateDynamic(*gPhysicsSDK, boxPos, boxGeometry, *material, 1.0f);
		        gScene->addActor(*gBox);
		}
Esempio n. 12
0
void CPhysics::CreateGround (int samplesX, int samplesY, float hScale, float wScale, const float* heightmap)
{
	PxHeightFieldDesc hfDesc;

	PxHeightFieldSample* Samples = new PxHeightFieldSample[sizeof(PxHeightFieldSample)*(samplesX*samplesY)];
	for (int x = 0; x < samplesX; ++x)
	{
		for (int y = 0; y < samplesY; ++ y)
		{
			Samples[x*samplesY + y].materialIndex0 = 0;
			Samples[x*samplesY + y].materialIndex1 = 0;

			short Height = heightmap[x*samplesY + y] * 32000;
			Samples[x*samplesY + y].height = Height;
		}
	}

	hfDesc.format           = PxHeightFieldFormat::eS16_TM;
	hfDesc.nbColumns        = samplesX;
	hfDesc.nbRows           = samplesY;
	hfDesc.samples.data     = Samples;
	hfDesc.samples.stride   = sizeof(PxHeightFieldSample);

	PxHeightField* aHeightField = m_SDK->createHeightField(hfDesc);
	if (!aHeightField)
		_CrtDbgBreak();

	PxHeightFieldGeometry hfGeom(aHeightField, PxMeshGeometryFlags(), hScale / 32000.f, wScale, wScale);

	PxTransform Pose = PxTransform(PxVec3(0.f, 0.0f, 0.0f));

	PxRigidStatic* aHeightFieldActor = m_SDK->createRigidStatic(Pose);
	if (!aHeightFieldActor)
		_CrtDbgBreak();

	PxShape* aHeightFieldShape = aHeightFieldActor->createShape(hfGeom, *m_Material);
	m_Scene->addActor(*aHeightFieldActor);

	delete Samples;
}
Esempio n. 13
0
void Apex::LoadTriangleMesh(int numVerts, PxVec3* verts, ObjectInfo info)
{
	PxRigidStatic* meshActor = mPhysics->createRigidStatic(PxTransform::createIdentity());
	PxShape* meshShape;
	if(meshActor)
	{
		
		PxTriangleMeshDesc meshDesc;
		meshDesc.points.count           = numVerts;
		meshDesc.points.stride          = sizeof(PxVec3);
		meshDesc.points.data            = verts;

		//meshDesc.triangles.count        = numInds/3.;
		//meshDesc.triangles.stride       = 3*sizeof(int);
		//meshDesc.triangles.data         = inds;

		PxToolkit::MemoryOutputStream writeBuffer;
		bool status = mCooking->cookTriangleMesh(meshDesc, writeBuffer);
		if(!status)
			return;

		PxToolkit::MemoryInputData readBuffer(writeBuffer.getData(), writeBuffer.getSize());

		PxTriangleMeshGeometry triGeom;
		triGeom.triangleMesh = mPhysics->createTriangleMesh(readBuffer);
		triGeom.scale = PxMeshScale(PxVec3(info.sx,info.sy,info.sz),physx::PxQuat::createIdentity());
		
		meshShape = meshActor->createShape(triGeom, *defaultMaterial);
		meshShape->setLocalPose(PxTransform(PxVec3(info.x,info.y,info.z), PxQuat(info.ry, PxVec3(0.0f,1.0f,0.0f))));
		meshShape->setFlag(PxShapeFlag::eUSE_SWEPT_BOUNDS, true);


		meshShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, true);
		

		mScene[mCurrentScene]->addActor(*meshActor);
	}
}
Esempio n. 14
0
void InitializePhysX() {

	gFoundation = PxCreateFoundation(
		PX_PHYSICS_VERSION,
		gDefaultAllocatorCallback,
		gDefaultErrorCallback);

	// Creating instance of PhysX SDK
	gPhysicsSDK = PxCreatePhysics(
		PX_PHYSICS_VERSION,
		*gFoundation,
		PxTolerancesScale());

	if(gPhysicsSDK==NULL) {
		cerr<<"Error creating PhysX3 device."<<endl;
		cerr<<"Exiting..."<<endl;
		exit(1);
	}

	if(!PxInitExtensions(*gPhysicsSDK))
		cerr<<"PxInitExtensions failed!"<<endl;

	//PxExtensionVisualDebugger::connect(gPhysicsSDK->getPvdConnectionManager(),"localhost",5425, 10000, true);


	//Create the scene
	PxSceneDesc sceneDesc(gPhysicsSDK->getTolerancesScale());
	sceneDesc.gravity = PxVec3(0.0f, -9.8f, 0.0f);

	if(!sceneDesc.cpuDispatcher) {
		PxDefaultCpuDispatcher* mCpuDispatcher = PxDefaultCpuDispatcherCreate(1);
		if(!mCpuDispatcher)
			cerr<<"PxDefaultCpuDispatcherCreate failed!"<<endl;
		sceneDesc.cpuDispatcher = mCpuDispatcher;
	}
	if(!sceneDesc.filterShader)
		sceneDesc.filterShader = gDefaultFilterShader;


	gScene = gPhysicsSDK->createScene(sceneDesc);
	if(!gScene)
		cerr<<"createScene failed!"<<endl;

	gScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0);
	gScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f);


	PxMaterial* mMaterial = gPhysicsSDK->createMaterial(0.5, 0.5, 0.5);

	//Create actors 
	//1) Create ground plane
	PxReal d = 0.0f;
	PxTransform pose = PxTransform(PxVec3(0.0f, 0, 0.0f), PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f)));

	PxRigidStatic* plane = gPhysicsSDK->createRigidStatic(pose);
	if(!plane)
		cerr<<"create plane failed!"<<endl;

	PxShape* shape = plane->createShape(PxPlaneGeometry(), *mMaterial);
	if(!shape)
		cerr<<"create shape failed!"<<endl;
	gScene->addActor(*plane);


	//2) Create dynamic cube	 
	PxReal density = 1.0f;
	PxTransform transform(PxVec3(0.0f, 3.0, 0.0f), PxQuat::createIdentity());
	PxVec3 dimensions(0.5, 0.5, 0.5);
	PxBoxGeometry geometry(dimensions);
	PxRigidDynamic *dyn_box = PxCreateDynamic(*gPhysicsSDK, transform, geometry, *mMaterial, density);
	if(!dyn_box)
		cerr<<"create actor failed!"<<endl;
	dyn_box->setAngularDamping(0.75);
	dyn_box->setLinearVelocity(PxVec3(0, 0, 0));
	gScene->addActor(*dyn_box);
	boxes.push_back(dyn_box);

	//create static cube
	transform.p = PxVec3(0, 5, 0);
	PxRigidStatic *static_box = PxCreateStatic(*gPhysicsSDK, transform, geometry, *mMaterial);
	if(!static_box)
		cerr<<"create actor failed!"<<endl;

	gScene->addActor(*static_box);
	boxes.push_back(static_box);

	//create a joint 	
	PxDistanceJoint* j = PxDistanceJointCreate(*gPhysicsSDK, dyn_box, PxTransform::createIdentity(), static_box, PxTransform::createIdentity());
	j->setDamping(1);
	//j->setSpring(200);
	j->setMinDistance(1);
	j->setMaxDistance(2);
	j->setConstraintFlag(PxConstraintFlag::eCOLLISION_ENABLED, true);
	j->setDistanceJointFlag(PxDistanceJointFlag::eMIN_DISTANCE_ENABLED, true);
	j->setDistanceJointFlag(PxDistanceJointFlag::eMAX_DISTANCE_ENABLED, true);
	j->setDistanceJointFlag(PxDistanceJointFlag::eSPRING_ENABLED, true);
}
Esempio n. 15
0
void CPhysics::Init()
{
	bool recordMemoryAllocations = true;

	static PxDefaultErrorCallback gDefaultErrorCallback;
	static PxDefaultAllocator gDefaultAllocatorCallback;

	
	m_SDK = PxCreatePhysics(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback, physx::PxTolerancesScale(), recordMemoryAllocations );
	if(!m_SDK)
		_CrtDbgBreak();

	if (!PxInitExtensions(*m_SDK))
        _CrtDbgBreak();

	m_Cooking = PxCreateCooking(PX_PHYSICS_VERSION, &m_SDK->getFoundation(), PxCookingParams());
	if (!m_Cooking)
		_CrtDbgBreak();


	PxSceneDesc sceneDesc(m_SDK->getTolerancesScale());
	sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
	//customizeSceneDesc(sceneDesc);

	if(!sceneDesc.cpuDispatcher)
	{
		unsigned int Threads = 1;
		m_CpuDispatcher = PxDefaultCpuDispatcherCreate(Threads);
		if(!m_CpuDispatcher)
			_CrtDbgBreak();
		sceneDesc.cpuDispatcher = static_cast<physx::pxtask::CpuDispatcher*>(m_CpuDispatcher);
	}
	if(!sceneDesc.filterShader)
		//sceneDesc.filterShader  = &PxDefaultSimulationFilterShader;
		sceneDesc.filterShader = &ButterflyFilterShader;

/*	#ifdef PX_WINDOWS
	if (true)
	{
		pxtask::CudaContextManagerDesc cudaContextManagerDesc;
		mCudaContextManager = pxtask::createCudaContextManager(cudaContextManagerDesc, &mSDK->getProfileZoneManager());
	}
	if(!sceneDesc.gpuDispatcher && mCudaContextManager)
	{
		sceneDesc.gpuDispatcher = mCudaContextManager->getGpuDispatcher();
	}
	#endif*/

	m_Scene = m_SDK->createScene(sceneDesc);
	if (!m_Scene)
		_CrtDbgBreak();

	// Basicowy material
	m_Material = m_SDK->createMaterial(0.5f, 0.5f, 0.1f);     //static friction, dynamic friction, restitution
	if(!m_Material)
		_CrtDbgBreak();

	m_Scene->setSimulationEventCallback(new CCollisionCallback);

	// PVD
	if (m_SDK->getPvdConnectionManager() == NULL)
		_CrtDbgBreak();
	//The connection flags state overall what data is to be sent to PVD.  Currently
	//the Debug connection flag requires support from the implementation (don't send
	//the data when debug isn't set) but the other two flags, profile and memory
	//are taken care of by the PVD SDK.
	//PVD::TConnectionFlagsType theConnectionFlags( PVD::PvdConnectionType::Debug | PVD::PvdConnectionType::Profile | PVD::PvdConnectionType::Memory );

	//Use these flags for a clean profile trace with minimal overhead
	PVD::TConnectionFlagsType theConnectionFlags( PVD::PvdConnectionType::Profile | PVD::PvdConnectionType::Debug);

	//Create a pvd connection that writes data straight to the filesystem.  This is
	//the fastest connection on windows for various reasons.  First, the transport is quite fast as
	//pvd writes data in blocks and filesystems work well with that abstraction.
	//Second, you don't have the PVD application parsing data and using CPU and memory bandwidth
	//while your application is running.
	//return mSDK->getPvdConnectionManager()->connect("c:\\temp.pxd2", true, theConnectionFlags);
	
	//The normal way to connect to pvd.  PVD needs to be running at the time this function is called.
	//We don't worry about the return value because we are already registered as a listener for connections
	//and thus our onPvdConnected call will take care of setting up our basic connection state.
	PVD::PvdConnection* Conn = PxExtensionVisualDebugger::connect(m_SDK->getPvdConnectionManager(), "127.0.0.1", 5425, 10, true, PxDebuggerConnectionFlags( (PxU32)theConnectionFlags) );

	// Test plane
	PxReal d = 0.0f;
	PxU32 axis = 1;
	PxTransform pose;
	
	#pragma region Testowe obiekty
	if(axis == 0)
			pose = PxTransform(PxVec3(d, 0.0f, 0.0f));
	else if(axis == 1)
			pose = PxTransform(PxVec3(0.0f, d, 0.0f),PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f)));
	else if(axis == 2)
			pose = PxTransform(PxVec3(0.0f, 0.0f, d), PxQuat(-PxHalfPi, PxVec3(0.0f, 1.0f, 0.0f)));

	PxRigidStatic* plane = m_SDK->createRigidStatic(pose);
	if (!plane)
		_CrtDbgBreak();
	PxShape* shape = plane->createShape(PxPlaneGeometry(), *m_Material);
	if (!shape)
		_CrtDbgBreak();
	m_Scene->addActor(*plane);

	PxReal density = 1.0f;
	PxTransform transform(PxVec3(50.0f, 50.0f, 50.0f), PxQuat::createIdentity());
	PxVec3 dimensions(1.0f, 1.0f, 1.0f);
	PxBoxGeometry geometry(dimensions);

	PxRigidDynamic *actor = PxCreateDynamic(*m_SDK, transform, geometry, *m_Material, density);
	if (!actor)
		_CrtDbgBreak();
	m_Scene->addActor(*actor);

	//physx::repx::RepXCollection* collection = physx::repx::createCollection("file.xml");
	//physx::repx::addObjectsToScene(collection, m_SDK, m_Cooking, m_Scene, 0);

	#pragma endregion
}
Esempio n. 16
0
PhysicsSystemPhysX::PhysicsSystemPhysX()
: PhysicsSystem()
{
	Log("HELLO PHYSX");

	static PxDefaultErrorCallback gDefaultErrorCallback;
	static PxDefaultAllocator gDefaultAllocatorCallback;

	PxFoundation* mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);
	if (!mFoundation)
		Log("PxCreateFoundation failed!");

	PxProfileZoneManager* mProfileZoneManager = &PxProfileZoneManager::createProfileZoneManager(mFoundation);
	if (!mProfileZoneManager)
		Log("PxProfileZoneManager::createProfileZoneManager failed!");

	bool recordMemoryAllocations = true;
	mPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation,
		PxTolerancesScale(), recordMemoryAllocations, mProfileZoneManager);
	if (!mPhysics)
		Log("PxCreatePhysics failed!");

	if (!PxInitExtensions(*mPhysics))
		Log("PxInitExtensions failed!");

	//	static PxDefaultSimulationFilterShader gDefaultFilterShader;
	//static PxFilterFlags gDefaultFilterShader = 

	PxSceneDesc sceneDesc(mPhysics->getTolerancesScale());
	sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
	sceneDesc.gravity = PxVec3(0.0f, -19.81f, 0.0f);
	//sceneDesc.
	//customizeSceneDesc(sceneDesc);

	if (!sceneDesc.cpuDispatcher)
	{
		PxDefaultCpuDispatcher*  mCpuDispatcher = PxDefaultCpuDispatcherCreate(1);
		if (!mCpuDispatcher)
			Log("PxDefaultCpuDispatcherCreate failed!");
		sceneDesc.cpuDispatcher = mCpuDispatcher;
	}
	if (!sceneDesc.filterShader)
		sceneDesc.filterShader = PxDefaultSimulationFilterShader;

	/*
	#ifdef PX_WINDOWS
	if(!sceneDesc.gpuDispatcher && mCudaContextManager)
	{
	sceneDesc.gpuDispatcher = mCudaContextManager->getGpuDispatcher();
	}
	#endif*/

	mScene = mPhysics->createScene(sceneDesc);
	if (!mScene)
		Log("createScene failed!");


	PxMaterial* mMaterial;

	mMaterial = mPhysics->createMaterial(0.5f, 0.5f, 0.1f);    //static friction, dynamic friction, restitution
	if (!mMaterial)
		Log("createMaterial failed!");

	aSphereActor = mPhysics->createRigidDynamic(PxTransform(0, 30, 0));
	PxShape* aSphereShape = aSphereActor->createShape(PxSphereGeometry(1), *mMaterial);
	PxRigidBodyExt::updateMassAndInertia(*aSphereActor, 2);

	mScene->addActor(*aSphereActor);
	aSphereActor->setLinearVelocity(PxVec3(1, 1, 1));

	PxRigidStatic* groundPlaneActor = mPhysics->createRigidStatic(PxTransform(0.f, -10.f, 0.f));
	PxShape* groundShape = groundPlaneActor->createShape(PxBoxGeometry(150.f, 5.f, 150.f), *mMaterial);
	mScene->addActor(*groundPlaneActor);
	 
	manager = PxCreateControllerManager(*mScene);
}
Esempio n. 17
0
void 
PhysXRigidManager::addStaticBody(const std::string & scene, physx::PxScene * world, physx::PxCooking * mCooking, nau::physics::IPhysics::BoundingVolume shape, physx::PxMaterial * material) {
	PxPhysics *gPhysics = &(world->getPhysics());
	PxRigidStatic * staticActor;
	PxTransform trans = PxTransform(PxMat44(rigidBodies[scene].info.extInfo.transform));
	switch (shape.sceneShape)
	{
	case nau::physics::IPhysics::BOX:
	{
		staticActor = PxCreateStatic(
			world->getPhysics(),
			trans,
			PxBoxGeometry(shape.max[0], shape.max[1], shape.max[2]),
			*material
		);
	}
	break;
	case nau::physics::IPhysics::SPHERE:
	{
		staticActor = PxCreateStatic(
			world->getPhysics(),
			trans,
			PxSphereGeometry(shape.max[0]),
			*material
		);
	}
	break;
	case nau::physics::IPhysics::CAPSULE:
	{
		staticActor = PxCreateStatic(
			world->getPhysics(),
			trans,
			PxCapsuleGeometry(
				shape.max[0],
				shape.max[1]
			),
			*material
		);
	}
	break;
	default:
	{
		if (scene.compare("plane") == 0) {
			staticActor = PxCreatePlane(
				world->getPhysics(),
				PxPlane(0.0f, 1.0f, 0.0f, 0.0f),
				*material
			);
		}
		else {
			staticActor = gPhysics->createRigidStatic(trans);
			PxTriangleMeshGeometry triGeom(gPhysics->createTriangleMesh(*getTriangleMeshGeo(world, mCooking, rigidBodies[scene].info.extInfo, true)));
			//triGeom.triangleMesh = gPhysics->createTriangleMesh(*getTriangleMeshGeo(world, mCooking, rigidBodies[scene].extInfo, true));
			staticActor->createShape(triGeom, *material);
		}
	}
	break;
	}
	staticActor->userData = static_cast<void*> (new std::string(scene));
	world->addActor(*staticActor);
	rigidBodies[scene].info.actor = staticActor;
}
Esempio n. 18
0
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);
    }
}