pxPlugin.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/consoleTypes.h"
  24. #include "T3D/physics/physX/pxPlugin.h"
  25. #include "T3D/physics/physicsShape.h"
  26. #include "T3D/physics/physX/pxWorld.h"
  27. #include "T3D/physics/physX/pxBody.h"
  28. #include "T3D/physics/physX/pxPlayer.h"
  29. #include "T3D/physics/physX/pxCollision.h"
  30. #include "T3D/gameBase/gameProcess.h"
  31. #include "core/util/tNamedFactory.h"
  32. extern bool gPhysXLogWarnings;
  33. AFTER_MODULE_INIT( Sim )
  34. {
  35. NamedFactory<PhysicsPlugin>::add( "PhysX", &PxPlugin::create );
  36. #if defined(TORQUE_OS_WIN) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)
  37. NamedFactory<PhysicsPlugin>::add( "default", &PxPlugin::create );
  38. #endif
  39. Con::addVariable( "$PhysXLogWarnings", TypeBool, &gPhysXLogWarnings,
  40. "@brief Output PhysX warnings to the console.\n\n"
  41. "@ingroup Physics\n");
  42. }
  43. PhysicsPlugin* PxPlugin::create()
  44. {
  45. // Only create the plugin if it hasn't been set up AND
  46. // the PhysX world is successfully initialized.
  47. bool success = PxWorld::restartSDK( false );
  48. if ( success )
  49. return new PxPlugin();
  50. return NULL;
  51. }
  52. PxPlugin::PxPlugin()
  53. {
  54. }
  55. PxPlugin::~PxPlugin()
  56. {
  57. }
  58. void PxPlugin::destroyPlugin()
  59. {
  60. // Cleanup any worlds that are still kicking.
  61. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.begin();
  62. for ( ; iter != mPhysicsWorldLookup.end(); iter++ )
  63. {
  64. iter->value->destroyWorld();
  65. delete iter->value;
  66. }
  67. mPhysicsWorldLookup.clear();
  68. PxWorld::restartSDK( true );
  69. delete this;
  70. }
  71. void PxPlugin::reset()
  72. {
  73. // First delete all the cleanup objects.
  74. if ( getPhysicsCleanup() )
  75. getPhysicsCleanup()->deleteAllObjects();
  76. getPhysicsResetSignal().trigger( PhysicsResetEvent_Restore );
  77. // Now let each world reset itself.
  78. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.begin();
  79. for ( ; iter != mPhysicsWorldLookup.end(); iter++ )
  80. iter->value->reset();
  81. }
  82. PhysicsCollision* PxPlugin::createCollision()
  83. {
  84. return new PxCollision();
  85. }
  86. PhysicsBody* PxPlugin::createBody()
  87. {
  88. return new PxBody();
  89. }
  90. PhysicsPlayer* PxPlugin::createPlayer()
  91. {
  92. return new PxPlayer();
  93. }
  94. bool PxPlugin::isSimulationEnabled() const
  95. {
  96. bool ret = false;
  97. PxWorld *world = static_cast<PxWorld*>( getWorld( smClientWorldName ) );
  98. if ( world )
  99. {
  100. ret = world->getEnabled();
  101. return ret;
  102. }
  103. world = static_cast<PxWorld*>( getWorld( smServerWorldName ) );
  104. if ( world )
  105. {
  106. ret = world->getEnabled();
  107. return ret;
  108. }
  109. return ret;
  110. }
  111. void PxPlugin::enableSimulation( const String &worldName, bool enable )
  112. {
  113. PxWorld *world = static_cast<PxWorld*>( getWorld( worldName ) );
  114. if ( world )
  115. world->setEnabled( enable );
  116. }
  117. void PxPlugin::setTimeScale( const F32 timeScale )
  118. {
  119. // Grab both the client and
  120. // server worlds and set their time
  121. // scales to the passed value.
  122. PxWorld *world = static_cast<PxWorld*>( getWorld( smClientWorldName ) );
  123. if ( world )
  124. world->setEditorTimeScale( timeScale );
  125. world = static_cast<PxWorld*>( getWorld( smServerWorldName ) );
  126. if ( world )
  127. world->setEditorTimeScale( timeScale );
  128. }
  129. const F32 PxPlugin::getTimeScale() const
  130. {
  131. // Grab both the client and
  132. // server worlds and call
  133. // setEnabled( true ) on them.
  134. PxWorld *world = static_cast<PxWorld*>( getWorld( smClientWorldName ) );
  135. if ( !world )
  136. {
  137. world = static_cast<PxWorld*>( getWorld( smServerWorldName ) );
  138. if ( !world )
  139. return 0.0f;
  140. }
  141. return world->getEditorTimeScale();
  142. }
  143. bool PxPlugin::createWorld( const String &worldName )
  144. {
  145. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.find( worldName );
  146. PhysicsWorld *world = NULL;
  147. iter != mPhysicsWorldLookup.end() ? world = (*iter).value : world = NULL;
  148. if ( world )
  149. {
  150. Con::errorf( "PxPlugin::createWorld - %s world already exists!", worldName.c_str() );
  151. return false;
  152. }
  153. world = new PxWorld();
  154. if ( worldName.equal( smClientWorldName, String::NoCase ) )
  155. world->initWorld( false, ClientProcessList::get() );
  156. else
  157. world->initWorld( true, ServerProcessList::get() );
  158. mPhysicsWorldLookup.insert( worldName, world );
  159. return world != NULL;
  160. }
  161. void PxPlugin::destroyWorld( const String &worldName )
  162. {
  163. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.find( worldName );
  164. if ( iter == mPhysicsWorldLookup.end() )
  165. return;
  166. PhysicsWorld *world = (*iter).value;
  167. world->destroyWorld();
  168. delete world;
  169. mPhysicsWorldLookup.erase( iter );
  170. }
  171. PhysicsWorld* PxPlugin::getWorld( const String &worldName ) const
  172. {
  173. if ( mPhysicsWorldLookup.isEmpty() )
  174. return NULL;
  175. Map<StringNoCase, PhysicsWorld*>::ConstIterator iter = mPhysicsWorldLookup.find( worldName );
  176. return iter != mPhysicsWorldLookup.end() ? (*iter).value : NULL;
  177. }
  178. PhysicsWorld* PxPlugin::getWorld() const
  179. {
  180. if ( mPhysicsWorldLookup.size() == 0 )
  181. return NULL;
  182. Map<StringNoCase, PhysicsWorld*>::ConstIterator iter = mPhysicsWorldLookup.begin();
  183. return iter->value;
  184. }
  185. U32 PxPlugin::getWorldCount() const
  186. {
  187. return mPhysicsWorldLookup.size();
  188. }
  189. void PxPlugin::_onDebugDrawEnabled( bool enabled )
  190. {
  191. if ( !enabled )
  192. gPhysicsSDK->setParameter( NX_VISUALIZATION_SCALE, 0.0f );
  193. }
  194. ConsoleFunction( physXRemoteDebuggerConnect, bool, 1, 3, "" )
  195. {
  196. if ( !gPhysicsSDK )
  197. {
  198. Con::errorf( "PhysX SDK not initialized!" );
  199. return false;
  200. }
  201. NxRemoteDebugger *debugger = gPhysicsSDK->getFoundationSDK().getRemoteDebugger();
  202. if ( debugger->isConnected() )
  203. {
  204. Con::errorf( "RemoteDebugger already connected... call disconnect first!" );
  205. return false;
  206. }
  207. const UTF8 *host = "localhost";
  208. U32 port = 5425;
  209. if ( argc >= 2 )
  210. host = argv[1];
  211. if ( argc >= 3 )
  212. port = dAtoi( argv[2] );
  213. // Before we connect we need to have write access
  214. // to both the client and server worlds.
  215. PxWorld::releaseWriteLocks();
  216. // Connect!
  217. debugger->connect( host, port );
  218. if ( !debugger->isConnected() )
  219. {
  220. Con::errorf( "RemoteDebugger failed to connect!" );
  221. return false;
  222. }
  223. Con::printf( "RemoteDebugger connected to %s at port %u!", host, port );
  224. return true;
  225. }
  226. ConsoleFunction( physXRemoteDebuggerDisconnect, void, 1, 1, "" )
  227. {
  228. if ( !gPhysicsSDK )
  229. {
  230. Con::errorf( "PhysX SDK not initialized!" );
  231. return;
  232. }
  233. NxRemoteDebugger *debugger = gPhysicsSDK->getFoundationSDK().getRemoteDebugger();
  234. if ( debugger->isConnected() )
  235. {
  236. debugger->flush();
  237. debugger->disconnect();
  238. Con::printf( "RemoteDebugger disconnected!" );
  239. }
  240. }