//-----------------------------------------------------------------------------
//  Constructor
//-----------------------------------------------------------------------------
CPhysicEngine::CPhysicEngine (void)
{
    // Creacion e inicializacion del objeto principal de PhysX
    int nVersion = NX_PHYSICS_SDK_VERSION;
    NxSDKCreateError errorCode;
    m_pPhysicsSDK = NxCreatePhysicsSDK (NX_PHYSICS_SDK_VERSION, Globals::g_pUserAllocator, this, NxPhysicsSDKDesc(), &errorCode);
    if (!m_pPhysicsSDK)  return;

#ifdef _DEBUG
    m_pPhysicsSDK->getFoundationSDK().getRemoteDebugger()->connect ("localhost", 5425);
#endif

    // Set the physics parameters
    m_pPhysicsSDK->setParameter (NX_SKIN_WIDTH, 0.001f);

    // Set the debug visualization parameters
    m_pPhysicsSDK->setParameter (NX_VISUALIZATION_SCALE, 1);
    m_pPhysicsSDK->setParameter (NX_VISUALIZE_COLLISION_SHAPES, 1);
    m_pPhysicsSDK->setParameter (NX_VISUALIZE_ACTOR_AXES, 1);

    // Inicializacion del motor de cooking para procesamiento de triangleMeshes
    NxInitCooking();

    // Creacion del gestor de controllers
    m_pControllerMgr = NxCreateControllerManager ( Globals::g_pUserAllocator );
}
Beispiel #2
0
//-------------------------------------------------------------------------------------------------
bool sdPhysicsSystem::CreateEmptyScene(uint uiSize)
{
	NIASSERT(!m_pkScene);
	NIASSERT(!m_pkControllerManager);

	float fSize = (float)uiSize;
	float fWalkHeight = 4000.f;
	NxBounds3 nxBound;
	nxBound.setCenterExtents(NxVec3(fSize * 0.5f, fSize * 0.5f, 0.f), NxVec3(fSize * 0.5f, fSize * 0.5f, fWalkHeight *0.5f));

	NxSceneDesc nxSceneDesc;
	nxSceneDesc.gravity = NxVec3(0.f, 0.f, -9.8f);
	nxSceneDesc.simType = NX_SIMULATION_SW;
	nxSceneDesc.maxBounds = &nxBound;
	nxSceneDesc.upAxis = 2;							///< Z up
	nxSceneDesc.bpType = NX_BP_TYPE_SAP_MULTI;
	nxSceneDesc.nbGridCellsX = 8u;
	nxSceneDesc.nbGridCellsY = 8u;
	nxSceneDesc.subdivisionLevel = 5;

	m_pkScene = m_pkPhysicsSDK->createScene(nxSceneDesc);
	NIASSERT(m_pkScene);

	m_pkControllerManager = NxCreateControllerManager(m_pkAllocator);
	NIASSERT(m_pkControllerManager);

	return true;
}
//初始化这个服务
efd::AsyncResult PhysxCharacterMovementService::OnInit()
{
	efd::MessageService* pMessageService =
		m_pServiceManager->GetSystemServiceAs<efd::MessageService>();

	EE_ASSERT(pMessageService);
//要接收PhysX 消息
	pMessageService->Subscribe(this, egfPhysX::kCAT_egfPhysXMessage);

	EE_ASSERT(efdPhysX::PhysXSDKManager::GetManager());

	EE_ASSERT(m_pAllocator);
	m_pkNxControllerManager = NxCreateControllerManager(m_pAllocator);

	EE_ASSERT(m_pkNxControllerManager);
	return efd::AsyncResult_Complete;

}
//----------------------------------------------------------------------------
// Init data
//----------------------------------------------------------------------------
bool CPhysicsManager::Init ()
{

	m_bIsOk = true;

	m_pMyAllocator = new CPhysicUserAllocator;
	assert(m_pMyAllocator);
	m_bIsOk = (m_pMyAllocator != NULL);
	if (m_bIsOk)
	{
		LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: Inicializando la libreria PhysX");
		// Initialize PhysicsSDK
		NxPhysicsSDKDesc desc;
		NxSDKCreateError errorCode = NXCE_NO_ERROR;
		m_pPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, m_pMyAllocator, NULL, desc, &errorCode);


		/*Precompilation Directives*/
		#if defined( _DEBUG )
		#define USE_DEBUGGER
		#ifdef USE_DEBUGGER
		m_pPhysicsSDK->getFoundationSDK().getRemoteDebugger()->connect("127.0.0.1");
		#endif
		#endif
		

		m_bIsOk = (m_pPhysicsSDK != NULL);
		if (m_bIsOk)
		{
			LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: Creado el PhysXSDK");
			LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: -------PhsX Settings---");
			LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: El valor del SkinWidth es: %f", 
				m_InitParams.m_fSkinWidth);


			//TODO por XML m_pPhysicsSDK->setParameter(NX_SKIN_WIDTH, m_InitParams.m_fSkinWidth);
			m_pPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.1f);

			//CODI PER PRINTAR INFO DELS JOINTS
			m_pPhysicsSDK->setParameter(NX_VISUALIZE_ACTOR_AXES, 1);
			m_pPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_LIMITS, 1);
			m_pPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_LOCAL_AXES, 1);
			m_pPhysicsSDK->setParameter(NX_CONTINUOUS_CD, 1);

			// Create a scene
			LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: El valor de la gravedad es: %f",
				m_InitParams.m_fGravity);
			NxSceneDesc sceneDesc;
			//TODO por XML sceneDesc.gravity = NxVec3(0.0f, m_InitParams.m_fGravity, 0.0f);
			sceneDesc.gravity = NxVec3(0.0f, -9.8f, 0.0f);
			m_pScene = m_pPhysicsSDK->createScene(sceneDesc);
			
			m_bIsOk = (m_pScene != NULL);
			if (m_bIsOk)
			{
				LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: Solo hay un material, con los siguientes params");
				LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: DefaultMaterial->Restitution %f:",
					m_InitParams.m_Restitution_DefMat);
				LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: DefaultMaterial->StaticFriction %f:",
					m_InitParams.m_StaticFriction_DefMat);
				LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: DefaultMaterial->DynamicFriction %f:",
					m_InitParams.m_DynamicFriction_DefMat);
				LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: ----END PhsX Settings---");
				// Set default material
				NxMaterial* defaultMaterial = m_pScene->getMaterialFromIndex(0);
				defaultMaterial->setRestitution(m_InitParams.m_Restitution_DefMat);
				defaultMaterial->setStaticFriction(m_InitParams.m_StaticFriction_DefMat);
				defaultMaterial->setDynamicFriction(m_InitParams.m_DynamicFriction_DefMat);

				//Collision Flags
				//m_pScene->setGroupCollisionFlag(ECG_ARMA,ECG_ESCENARI,true);


				 //Create a controllerManager
				m_pControllerManager = NxCreateControllerManager(m_pMyAllocator);
				m_bIsOk = (m_pControllerManager != NULL);
				if (m_bIsOk)
				{
				LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: Creado el controlador de caracteres");
				m_pCookingMesh = new CPhysicCookingMesh();
				assert(m_pCookingMesh);
				m_bIsOk = m_pCookingMesh->Init(m_pPhysicsSDK, m_pMyAllocator);
				if (m_bIsOk) {
				LOGGER->AddNewLog(ELL_INFORMATION, "PhysicsManager:: Creado el CookingMesh");
				}

				} //m_bIsOk m_pControllerManager?*/

			}//isOk m_pScene?

		}//isOk m_pPhysicsSDK ?

	}//isOk m_pMyAllocator ?

	if (!m_bIsOk)
	{
		std::string msg_error = "PhysicsManager::Init-> Error en la inicializacion de PhysX";
		LOGGER->AddNewLog(ELL_ERROR, msg_error.c_str());
		Release();
		throw CException(__FILE__, __LINE__, msg_error);
	}

	return m_bIsOk;
}
Beispiel #5
0
/***********************************************************
	init function
***********************************************************/
void PhysXEngine::Init()
{
	// Create the physics SDK
    gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
    if (!gPhysicsSDK)  
		return;

	if (gPhysicsSDK->getFoundationSDK().getRemoteDebugger())
		gPhysicsSDK->getFoundationSDK().getRemoteDebugger()->connect("localhost", 5425);


	// Set the physics parameters
	gPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.01f);

	// Set the debug visualization parameters
	gPhysicsSDK->setParameter(NX_VISUALIZATION_SCALE, 1);
	gPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);
	gPhysicsSDK->setParameter(NX_VISUALIZE_ACTOR_AXES, 1);

    // Create the scene
    NxSceneDesc sceneDesc;
 	sceneDesc.simType				= NX_SIMULATION_SW;
    sceneDesc.gravity               = NxVec3(0,-9.8f,0);
    gScene = gPhysicsSDK->createScene(sceneDesc);	
	if(!gScene)
	{ 
		sceneDesc.simType			= NX_SIMULATION_SW; 
		gScene = gPhysicsSDK->createScene(sceneDesc);  
		if(!gScene) 
			return;
	}


	// Create the default material
	NxMaterial* defaultMaterial = gScene->getMaterialFromIndex(0); 
	defaultMaterial->setRestitution(0.5);
	defaultMaterial->setStaticFriction(0.5);
	defaultMaterial->setDynamicFriction(0.5);


	// add a cube and a plane
	//CreatePlane(NxVec3(0, 0, 0), NxVec3(0, 1, 0));

	CreateBox(NxVec3(0, 4, 0), 3.0f, 3.0f, 3.0f, 10, false);
	CreateBox(NxVec3(7, 10, 7), 2.0f, 3.0f, 2.0f, 10, true);
	CreateBox(NxVec3(20, 1, 20), 30.0f, 1.0f, 30.0f, 10, false);

	// init time
	_lasttime = SynchronizedTimeHandler::getInstance()->GetCurrentTimeDouble();


	// init character controllers
	gManager = NxCreateControllerManager(gAllocator);


	//float TimeStep = 1.0f / 60.0f;
	gScene->setTiming();


	// Start the first frame of the simulation
	if (gScene)  
		StartPhysics();

}
ControllerManager::ControllerManager()
{
 mAllocator = new ControllerAllocator();
 mManager = NxCreateControllerManager(mAllocator);
}
void CreateControllerManager(NxUserAllocator* a)
{
	gManager = NxCreateControllerManager(a);
}