示例#1
0
NxMaterialDesc*pFactory::createMaterialFromXML(const char* nodeName/* = */,const TiXmlDocument * doc /* = NULL */)
{

	
	
	NxMaterialDesc *result  = new NxMaterialDesc();
	result->setToDefault();

	if (doc ==NULL)
		return result;

	if (!getFirstDocElement(doc->RootElement()))
		return result;


	/************************************************************************/
	/* try to load settings from xml :                                                                      */
	/************************************************************************/
	if ( strlen(nodeName) && doc)
	{
		const TiXmlElement *root = getFirstDocElement(doc->RootElement());
		for (const TiXmlNode *child = root->FirstChild(); child; child = child->NextSibling() )
		{
			if ( (child->Type() == TiXmlNode::ELEMENT))
			{
				XString name = child->Value();

				if (!strcmp(child->Value(), "material" ) )
				{
					const TiXmlElement *element = (const TiXmlElement*)child;
					if (element->Type()  == TiXmlNode::ELEMENT )
					{	
						if(!strcmp( element->Attribute("name"),nodeName ) )
						{
							for (const TiXmlNode *node = element->FirstChild(); node; node = node->NextSibling() )
							{
								if ((node->Type() ==  TiXmlNode::ELEMENT) && (!stricmp(node->Value(),"settings")))
								{
									const TiXmlElement *sube = (const TiXmlElement*)node;
									double v;

									//////////////////////////////////////////////////////////////////////////
									int res = sube->QueryDoubleAttribute("DynamicFriction",&v);
									if (res == TIXML_SUCCESS)
									{
										if(v >=0.0f)
										{
											result->dynamicFriction = (float)v;
											continue;
										}
									}
									//////////////////////////////////////////////////////////////////////////
									res = sube->QueryDoubleAttribute("StaticFriction",&v);
									if (res == TIXML_SUCCESS)
									{
										if(v >=0.0f){
											result->staticFriction= (float)v;
											continue;
										}
									}
									//////////////////////////////////////////////////////////////////////////
									res = sube->QueryDoubleAttribute("Restitution",&v);
									if (res == TIXML_SUCCESS)
									{
										if(v >=0.0f){
											result->restitution= (float)v;
											continue;
										}
									}
									//////////////////////////////////////////////////////////////////////////
									res = sube->QueryDoubleAttribute("DynamicFrictionV",&v);
									if (res == TIXML_SUCCESS)
									{
										if(v >=0.0f){
											result->dynamicFrictionV= (float)v;
											continue;
										}
									}
									//////////////////////////////////////////////////////////////////////////
									res = sube->QueryDoubleAttribute("StaticFrictionV",&v);
									if (res == TIXML_SUCCESS)
									{
										if(v >=0.0f){
											result->staticFrictionV= (float)v;
											continue;
										}
									}
									//////////////////////////////////////////////////////////////////////////
									const char* dirOfAnisotropy  = NULL;
									dirOfAnisotropy  = sube->Attribute("DirOfAnisotropy");
									if (dirOfAnisotropy && strlen(dirOfAnisotropy))
									{
										VxVector vec  = _str2Vec(dirOfAnisotropy);
										if (vec.Magnitude() >0.1f)
										{
											result->flags = NX_MF_ANISOTROPIC;
											result->dirOfAnisotropy = pMath::getFrom(vec);
											continue;
										}else
										{
											result->dirOfAnisotropy = pMath::getFrom(VxVector(0,0,0));
										}

										//result->setGravity(vec);

									}

									//////////////////////////////////////////////////////////////////////////
									const char* FrictionCombineMode  = NULL;
									FrictionCombineMode = sube->Attribute("FrictionCombineMode");
									if (FrictionCombineMode && strlen(FrictionCombineMode))
									{
										int fMode  = _str2CombineMode(FrictionCombineMode);
										result->frictionCombineMode = (NxCombineMode)fMode;
										continue;
									}

									//////////////////////////////////////////////////////////////////////////
									const char* RestitutionCombineMode  = NULL;
									RestitutionCombineMode = sube->Attribute("RestitutionCombineMode");
									if (RestitutionCombineMode && strlen(RestitutionCombineMode))
									{
										int fMode  = _str2CombineMode(RestitutionCombineMode);
										result->restitutionCombineMode= (NxCombineMode)fMode;
										continue;
									}
								}
							}	
							return result;
						}
					}
				}
			}
		}
	}

	return result;

}
示例#2
0
pWorld*pFactory::createWorld(CK3dEntity* referenceObject, pWorldSettings *worldSettings,pSleepingSettings *sleepSettings)
{


	using namespace vtTools::AttributeTools;
	using namespace vtTools::ParameterTools;

	
	//////////////////////////////////////////////////////////////////////////
	//sanity checks : 
	if (!referenceObject || !GetPMan() )
	{
		return NULL;
	}


	if (!getPhysicSDK())
	{
		//xLogger::xLog(XL_START,ELOGWARNING,E_LI_MANAGER,"No physic sdk loaded");
		return NULL;
	}

	int worldAtt = GetPMan()->att_world_object;
	int surfaceAttribute = GetPMan()->att_surface_props;

	//exists ? Delete it !
	pWorld *w = GetPMan()->getWorld(referenceObject->GetID());
	if (w)
	{
		GetPMan()->deleteWorld(referenceObject->GetID());
	}
		
	//our new world : 
	pWorld *result  = new pWorld(referenceObject);
	GetPMan()->getWorlds()->InsertUnique(referenceObject,result);

	result->initUserReports();



	//////////////////////////////////////////////////////////////////////////

	//there is no world settings attribute  : 
	if (!referenceObject->HasAttribute(worldAtt) )
	{
		referenceObject->SetAttribute(worldAtt);
		using namespace vtTools;
		VxVector grav = worldSettings->getGravity();
		float sWith  = worldSettings->getSkinWith();
		AttributeTools::SetAttributeValue<VxVector>(referenceObject,worldAtt,0,&grav);
		AttributeTools::SetAttributeValue<float>(referenceObject,worldAtt,1,&sWith);
	}


	if (!worldSettings)
	{
		worldSettings = pFactory::Instance()->createWorldSettings(XString("Default"),GetPMan()->getDefaultConfig());
	}
	if (!worldSettings)
	{
		worldSettings = new pWorldSettings();
	}

	//////////////////////////////////////////////////////////////////////////
	//pSDK Scene creation  : 

	// Create a scene
	NxSceneDesc sceneDesc;
	sceneDesc.gravity				= pMath::getFrom(worldSettings->getGravity());
	sceneDesc.upAxis = 1;
	sceneDesc.flags |=NX_SF_ENABLE_ACTIVETRANSFORMS;
	sceneDesc.userNotify =&myNotify;
	sceneDesc.userContactReport = result->contactReport;
    
	NxScene *scene = NULL;
	if (getPhysicSDK())
	{
		scene  = getPhysicSDK()->createScene(sceneDesc);
		if(scene == NULL) 
		{
            xLogger::xLog(XL_START,ELOGERROR,E_LI_MANAGER,"Couldn't create scene!");
			return NULL;
		}
	}

	result->setScene(scene);
	scene->setUserContactReport(result->contactReport);
	scene->setUserTriggerReport(result->triggerReport);

	NxMaterialDesc *materialDescr = NULL;
	NxMaterial *material = NULL;
	if (referenceObject->HasAttribute(surfaceAttribute))
	{
		materialDescr  = createMaterialFromEntity(referenceObject);
		material =  result->getScene()->createMaterial(*materialDescr);
		material->userData = (void*)GetValueFromParameterStruct<int>(referenceObject->GetAttributeParameter(surfaceAttribute) ,E_MS_XML_TYPE);
	}else{
		
		if (getDefaultDocument())
		{
			
			materialDescr = createMaterialFromXML("Default",getDefaultDocument());
		}
		
		if (materialDescr)
		{
			material = result->getScene()->createMaterial(*materialDescr);
		}

		if (!material)
		{
			materialDescr = new NxMaterialDesc();
			materialDescr->setToDefault();
			material = result->getScene()->getMaterialFromIndex(0); 
			material->loadFromDesc(*materialDescr);
		}
	}
	
	int z = (int)material->userData;
	NxMaterial *zeroMaterial  = result->getScene()->getMaterialFromIndex(0);
	zeroMaterial->setDirOfAnisotropy(material->getDirOfAnisotropy());
	zeroMaterial->setStaticFriction(material->getStaticFriction());
	zeroMaterial->setDynamicFriction(material->getDynamicFriction());
	zeroMaterial->setStaticFrictionV(material->getStaticFrictionV());
	zeroMaterial->setDynamicFrictionV(material->getDynamicFrictionV());
	zeroMaterial->setFrictionCombineMode(material->getFrictionCombineMode());
	zeroMaterial->setRestitutionCombineMode(material->getRestitutionCombineMode());
	zeroMaterial->setFlags(material->getFlags());
	zeroMaterial->userData = material->userData;




	if (!material)
	{
		xLogger::xLog(XL_START,ELOGERROR,E_LI_MANAGER,"Couldn't create default material!");
	}
	result->setDefaultMaterial(material);

	scene->userData = result;

	
	//NxConstraintDominance testDom(1.0, 1.0f);
	//result->getScene()->setDominanceGroupPair(1, 2,testDom );	//board - debris



	//////////////////////////////////////////////////////////////////////////
	//there is no world settings attribute  : 
	/*
	if (!referenceObject->HasAttribute(GetPMan()->att_sleep_settings) )
	{
		referenceObject->SetAttribute(GetPMan()->att_sleep_settings);
		using namespace vtTools;
		AttributeTools::SetAttributeValue<int>(referenceObject,GetPMan()->att_sleep_settings,0,&sSettings->m_SleepSteps);
		AttributeTools::SetAttributeValue<float>(referenceObject,GetPMan()->att_sleep_settings,1,&sSettings->m_AngularThresold);
		AttributeTools::SetAttributeValue<float>(referenceObject,GetPMan()->att_sleep_settings,2,&sSettings->m_LinearThresold);
		AttributeTools::SetAttributeValue<int>(referenceObject,GetPMan()->att_sleep_settings,3,&sSettings->m_AutoSleepFlag);
	}	
	*/
	

	/*
	result->SleepingSettings(sSettings);
	//////////////////////////////////////////////////////////////////////////

	result->Reference(referenceObject);
	
	result->Init();
*/


	result->_checkForDominanceConstraints();
	result->_construct();






	return result;
	

	//return NULL;

}
示例#3
0
pRigidBody*pFactory::createBox(CK3dEntity *referenceObject,CK3dEntity *worldReferenceObject,pObjectDescr*descr,int creationFlags)
{

	pWorld *world=getManager()->getWorld(worldReferenceObject,referenceObject); 
	pRigidBody*result = world->getBody(referenceObject);
	if(result)
	{
		result->destroy();
		delete result;
		result = NULL;
	}
	// we create our final body in the given world :
	result  =  createBody(referenceObject,worldReferenceObject);
	if (result)
	{
		result->setWorld(world);

		using namespace vtTools::AttributeTools;

		result->setFlags(descr->flags);
		result->setHullType(descr->hullType);
		result->setDataFlags(0x000);
		result->checkDataFlags();

		if (result->getSkinWidth()==-1.0f)
		{
			result->setSkinWidth(0.01f);
		}

		VxMatrix v_matrix ;
		VxVector position,scale;		
		VxQuaternion quat;	


		v_matrix = referenceObject->GetWorldMatrix();
		Vx3DDecomposeMatrix(v_matrix,quat,position,scale);
		VxVector box_s= BoxGetZero(referenceObject);

		NxVec3 pos = pMath::getFrom(position);
		NxQuat rot = pMath::getFrom(quat);

		float density = result->getDensity();
		float radius = referenceObject->GetRadius();
		if (referenceObject->GetRadius() < 0.001f )
		{
			radius  = 1.0f;
		}


		//////////////////////////////////////////////////////////////////////////
		//create actors description
		NxActorDesc actorDesc;actorDesc.setToDefault();
		NxBodyDesc bodyDesc;bodyDesc.setToDefault();

		//////////////////////////////////////////////////////////////////////////
		NxMaterialDesc *materialDescr = NULL;
		NxMaterial *material = NULL;
		if (isFlagOn(result->getDataFlags(),EDF_MATERIAL_PARAMETER))
		{
			NxMaterialDesc  entMatNull;entMatNull.setToDefault();
			NxMaterialDesc  *entMat  = createMaterialFromEntity(referenceObject);
			material  = world->getScene()->createMaterial(entMatNull);
			material->loadFromDesc(*entMat);
			result->setMaterial(material);
		}else{
			if (world->getDefaultMaterial())
			{
				result->setMaterial(world->getDefaultMaterial());
			}
		}
		//////////////////////////////////////////////////////////////////////////

		NxBoxShapeDesc boxShape;
		if (creationFlags & E_OFC_DIMENSION )
		{
			boxShape.dimensions = pMath::getFrom(box_s)*0.5f;
		}
		boxShape.density  = descr->density;
		boxShape.materialIndex = result->getMaterial()->getMaterialIndex();
		if (result->getSkinWidth()!=-1.0f)
			boxShape.skinWidth = result->getSkinWidth();

		actorDesc.shapes.pushBack(&boxShape);

		//////////////////////////////////////////////////////////////////////////
		//dynamic object  ? 
		if (result->getFlags() & BF_Moving){
			actorDesc.body = &bodyDesc;
		}
		else
			actorDesc.body = NULL;

		//////////////////////////////////////////////////////////////////////////
		//set transformations
		actorDesc.density  = descr->density;

		if (creationFlags & E_OFC_POSITION)
		{
			actorDesc.globalPose.t = pos;	
		}

		if (creationFlags & E_OFC_POSITION)
		{
			actorDesc.globalPose.M = rot;
		}
		
				//////////////////////////////////////////////////////////////////////////
		//create the actor

		int v = actorDesc.isValid();
		NxActor *actor  = world->getScene()->createActor(actorDesc);
		if (!actor)
		{
			xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't create actor");
			delete result;
			return NULL;

		}


		//////////////////////////////////////////////////////////////////////////
		//set additional settings : 
		result->setActor(actor);
		actor->setName(referenceObject->GetName());
		actor->userData= result;

		if (result->getFlags() & BF_Moving)
		{
			VxVector massOffsetOut;referenceObject->Transform(&massOffsetOut,&result->getMassOffset());
			actor->setCMassOffsetGlobalPosition(pMath::getFrom(massOffsetOut));
		}

		if (result->getFlags() & BF_Kinematic)
		{
			actor->raiseBodyFlag(NX_BF_KINEMATIC);
		}

		result->enableCollision((result->getFlags() & BF_Collision));
		if (result->getFlags() & BF_Moving)
		{
			if (!(result->getFlags() & BF_Gravity))
			{
				actor->raiseBodyFlag(NX_BF_DISABLE_GRAVITY);
			}
		}

		NxShape *shape  = result->getShapeByIndex();
		if (shape)
		{
			pSubMeshInfo *sInfo  = new pSubMeshInfo();
			sInfo->entID = referenceObject->GetID();
			sInfo->refObject  = (CKBeObject*)referenceObject;
			shape->userData = (void*)sInfo;
			result->setMainShape(shape);
			shape->setName(referenceObject->GetName());
		}
	}
		
	return result;
}
static bool InitNx()
	{

	if (!gAllocator)
		gAllocator = new UserAllocator;

	// Initialize PhysicsSDK
	NxPhysicsSDKDesc desc;
	NxSDKCreateError errorCode = NXCE_NO_ERROR;
	gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, gAllocator, &gErrorStream, desc, &errorCode);
	if(gPhysicsSDK == NULL) 
		{
		printf("\nSDK create error (%d - %s).\nUnable to initialize the PhysX SDK, exiting the sample.\n\n", errorCode, getNxSDKCreateError(errorCode));
		return false;
		}
#if SAMPLES_USE_VRD
	// The settings for the VRD host and port are found in SampleCommonCode/SamplesVRDSettings.h
	if (gPhysicsSDK->getFoundationSDK().getRemoteDebugger() && !gPhysicsSDK->getFoundationSDK().getRemoteDebugger()->isConnected())
		gPhysicsSDK->getFoundationSDK().getRemoteDebugger()->connect(SAMPLES_VRD_HOST, SAMPLES_VRD_PORT, SAMPLES_VRD_EVENTMASK);
#endif

	gPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.025f);
	//enable visualisation
	gPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1.0f);
	gPhysicsSDK->setParameter(NX_VISUALIZATION_SCALE, 1.0f);
		
	// Create a scene
	NxSceneDesc sceneDesc;
	sceneDesc.gravity				= gDefaultGravity;
	gScene = gPhysicsSDK->createScene(sceneDesc);
	if(gScene == NULL) 
		{
		printf("\nError: Unable to create a PhysX scene, exiting the sample.\n\n");
		return false;
		}

	//create some materials -- note that we reuse the same NxMaterialDesc every time,
	//as it gets copied to the SDK with every call, not just referenced.
	NxMaterialDesc	material;
	
	//default material
	material.restitution		= 0.0f;
	material.staticFriction		= 0.5f;
	material.dynamicFriction	= 0.5f;
	defaultMaterialIndex = 0;
	gScene->getMaterialFromIndex(defaultMaterialIndex)->loadFromDesc(material);//we redefine the default material in slot 0
	
	
	//somewhat bouncy material
	material.restitution		= 0.75f;
	material.staticFriction		= 0.5f;
	material.dynamicFriction	= 0.5f;
	somewhatBouncyMaterialIndex = gScene->createMaterial(material)->getMaterialIndex();
	
	//very bouncy material
	material.restitution		= 1.0f;
	material.staticFriction		= 0.5f;
	material.dynamicFriction	= 0.5f;
	veryBouncyMaterialIndex = gScene->createMaterial(material)->getMaterialIndex();
	
	//almost frictionless material
	material.restitution		= 0.0f;
	material.staticFriction		= 0.01f;
	material.dynamicFriction	= 0.01f;
	frictionlessMaterialIndex = gScene->createMaterial(material)->getMaterialIndex();
	
	//really high friction material
	material.restitution		= 0.0f;
	material.staticFriction		= 20.0f;//static friction can be higher than 1.
	material.dynamicFriction	= 1.0f;	//dynamic friction should not be higher than 1.
	highFrictionMaterialIndex = gScene->createMaterial(material)->getMaterialIndex();
	
	//anisotropic friction material
	material.restitution		= 0.0f;
	material.staticFriction		= 0.1f;
	material.dynamicFriction	= 0.1f;	
	material.dynamicFrictionV	= 0.8f;
	material.staticFrictionV	= 1.0f;
	material.dirOfAnisotropy.set(1.5,0,1);	//this will be used on one of the ramps so that boxes slide off at an angle
	material.dirOfAnisotropy.normalize();
	material.flags				= NX_MF_ANISOTROPIC;
	anisoMaterialIndex = gScene->createMaterial(material)->getMaterialIndex();
	material.setToDefault();		//turn off aniso on our material object in case we want to use it again.
	

	
	// Create ground plane
	NxPlaneShapeDesc planeDesc;
	planeDesc.materialIndex = frictionlessMaterialIndex;
	NxActorDesc actorDesc;
	actorDesc.shapes.pushBack(&planeDesc);
	gScene->createActor(actorDesc);

	//create some ramps
	NxBoxShapeDesc anisoBoxDesc;
	anisoBoxDesc.dimensions		= NxVec3(float(20), float(2), float(30));
	anisoBoxDesc.materialIndex	= anisoMaterialIndex;
	
	actorDesc.setToDefault();
	actorDesc.shapes.pushBack(&anisoBoxDesc);
	actorDesc.globalPose.t.set(0,20.0f,0);
	float angle = NxMath::degToRad(30.0f);
	actorDesc.globalPose.M(0,0) = 1;		actorDesc.globalPose.M(0,1) = 0;		actorDesc.globalPose.M(0,2) = 0;
	actorDesc.globalPose.M(1,0) = 0;		actorDesc.globalPose.M(1,1) = cos(angle);		actorDesc.globalPose.M(1,2) = -sin(angle);
	actorDesc.globalPose.M(2,0) = 0;		actorDesc.globalPose.M(2,1) = sin(angle);		actorDesc.globalPose.M(2,2) = cos(angle);
	gScene->createActor(actorDesc);

	NxBoxShapeDesc frictlessBoxDesc;
	frictlessBoxDesc.dimensions		= NxVec3(float(20), float(2), float(30));
	frictlessBoxDesc.materialIndex	= frictionlessMaterialIndex;

	actorDesc.setToDefault();
	actorDesc.shapes.clear();
	actorDesc.shapes.pushBack(&frictlessBoxDesc);
	actorDesc.globalPose.t.set(-40,20.0f,0);
	actorDesc.globalPose.M(0,0) = 1;		actorDesc.globalPose.M(0,1) = 0;		actorDesc.globalPose.M(0,2) = 0;
	actorDesc.globalPose.M(1,0) = 0;		actorDesc.globalPose.M(1,1) = cos(angle);		actorDesc.globalPose.M(1,2) = -sin(angle);
	actorDesc.globalPose.M(2,0) = 0;		actorDesc.globalPose.M(2,1) = sin(angle);		actorDesc.globalPose.M(2,2) = cos(angle);
	gScene->createActor(actorDesc);

	NxBoxShapeDesc hiFrictBoxDesc;
	hiFrictBoxDesc.dimensions		= NxVec3(float(20), float(2), float(30));
	hiFrictBoxDesc.materialIndex	= highFrictionMaterialIndex;

	actorDesc.setToDefault();
	actorDesc.shapes.clear();
	actorDesc.shapes.pushBack(&hiFrictBoxDesc);
	actorDesc.globalPose.t.set(40,20.0f,0);
	actorDesc.globalPose.M(0,0) = 1;		actorDesc.globalPose.M(0,1) = 0;		actorDesc.globalPose.M(0,2) = 0;
	actorDesc.globalPose.M(1,0) = 0;		actorDesc.globalPose.M(1,1) = cos(angle);		actorDesc.globalPose.M(1,2) = -sin(angle);
	actorDesc.globalPose.M(2,0) = 0;		actorDesc.globalPose.M(2,1) = sin(angle);		actorDesc.globalPose.M(2,2) = cos(angle);
	gScene->createActor(actorDesc);

	return true;
	}