px3Plugin.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/physx3/px3World.h"
  25. #include "T3D/physics/physx3/px3Plugin.h"
  26. #include "T3D/physics/physx3/px3Collision.h"
  27. #include "T3D/physics/physx3/px3Body.h"
  28. #include "T3D/physics/physx3/px3Player.h"
  29. #include "T3D/physics/physicsShape.h"
  30. #include "T3D/gameBase/gameProcess.h"
  31. #include "core/util/tNamedFactory.h"
  32. AFTER_MODULE_INIT( Sim )
  33. {
  34. NamedFactory<PhysicsPlugin>::add( "PhysX3", &Px3Plugin::create );
  35. #if defined(TORQUE_OS_WIN) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)
  36. NamedFactory<PhysicsPlugin>::add( "default", &Px3Plugin::create );
  37. #endif
  38. }
  39. PhysicsPlugin* Px3Plugin::create()
  40. {
  41. // Only create the plugin if it hasn't been set up AND
  42. // the PhysX world is successfully initialized.
  43. bool success = Px3World::restartSDK( false );
  44. if ( success )
  45. return new Px3Plugin();
  46. return NULL;
  47. }
  48. Px3Plugin::Px3Plugin()
  49. {
  50. }
  51. Px3Plugin::~Px3Plugin()
  52. {
  53. }
  54. void Px3Plugin::destroyPlugin()
  55. {
  56. // Cleanup any worlds that are still kicking.
  57. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.begin();
  58. for ( ; iter != mPhysicsWorldLookup.end(); iter++ )
  59. {
  60. iter->value->destroyWorld();
  61. delete iter->value;
  62. }
  63. mPhysicsWorldLookup.clear();
  64. Px3World::restartSDK( true );
  65. delete this;
  66. }
  67. void Px3Plugin::reset()
  68. {
  69. // First delete all the cleanup objects.
  70. if ( getPhysicsCleanup() )
  71. getPhysicsCleanup()->deleteAllObjects();
  72. getPhysicsResetSignal().trigger( PhysicsResetEvent_Restore );
  73. // Now let each world reset itself.
  74. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.begin();
  75. for ( ; iter != mPhysicsWorldLookup.end(); iter++ )
  76. iter->value->reset();
  77. }
  78. PhysicsCollision* Px3Plugin::createCollision()
  79. {
  80. return new Px3Collision();
  81. }
  82. PhysicsBody* Px3Plugin::createBody()
  83. {
  84. return new Px3Body();
  85. }
  86. PhysicsPlayer* Px3Plugin::createPlayer()
  87. {
  88. return new Px3Player();
  89. }
  90. bool Px3Plugin::isSimulationEnabled() const
  91. {
  92. bool ret = false;
  93. Px3World *world = static_cast<Px3World*>( getWorld( smClientWorldName ) );
  94. if ( world )
  95. {
  96. ret = world->isEnabled();
  97. return ret;
  98. }
  99. world = static_cast<Px3World*>( getWorld( smServerWorldName ) );
  100. if ( world )
  101. {
  102. ret = world->isEnabled();
  103. return ret;
  104. }
  105. return ret;
  106. }
  107. void Px3Plugin::enableSimulation( const String &worldName, bool enable )
  108. {
  109. Px3World *world = static_cast<Px3World*>( getWorld( worldName ) );
  110. if ( world )
  111. world->setEnabled( enable );
  112. }
  113. void Px3Plugin::setTimeScale( const F32 timeScale )
  114. {
  115. // Grab both the client and
  116. // server worlds and set their time
  117. // scales to the passed value.
  118. Px3World *world = static_cast<Px3World*>( getWorld( smClientWorldName ) );
  119. if ( world )
  120. world->setEditorTimeScale( timeScale );
  121. world = static_cast<Px3World*>( getWorld( smServerWorldName ) );
  122. if ( world )
  123. world->setEditorTimeScale( timeScale );
  124. }
  125. const F32 Px3Plugin::getTimeScale() const
  126. {
  127. // Grab both the client and
  128. // server worlds and call
  129. // setEnabled( true ) on them.
  130. Px3World *world = static_cast<Px3World*>( getWorld( smClientWorldName ) );
  131. if ( !world )
  132. {
  133. world = static_cast<Px3World*>( getWorld( smServerWorldName ) );
  134. if ( !world )
  135. return 0.0f;
  136. }
  137. return world->getEditorTimeScale();
  138. }
  139. bool Px3Plugin::createWorld( const String &worldName )
  140. {
  141. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.find( worldName );
  142. PhysicsWorld *world = NULL;
  143. iter != mPhysicsWorldLookup.end() ? world = (*iter).value : world = NULL;
  144. if ( world )
  145. {
  146. Con::errorf( "Px3Plugin::createWorld - %s world already exists!", worldName.c_str() );
  147. return false;
  148. }
  149. world = new Px3World();
  150. if ( worldName.equal( smClientWorldName, String::NoCase ) )
  151. world->initWorld( false, ClientProcessList::get() );
  152. else
  153. world->initWorld( true, ServerProcessList::get() );
  154. mPhysicsWorldLookup.insert( worldName, world );
  155. return world != NULL;
  156. }
  157. void Px3Plugin::destroyWorld( const String &worldName )
  158. {
  159. Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.find( worldName );
  160. if ( iter == mPhysicsWorldLookup.end() )
  161. return;
  162. PhysicsWorld *world = (*iter).value;
  163. world->destroyWorld();
  164. delete world;
  165. mPhysicsWorldLookup.erase( iter );
  166. }
  167. PhysicsWorld* Px3Plugin::getWorld( const String &worldName ) const
  168. {
  169. if ( mPhysicsWorldLookup.isEmpty() )
  170. return NULL;
  171. Map<StringNoCase, PhysicsWorld*>::ConstIterator iter = mPhysicsWorldLookup.find( worldName );
  172. return iter != mPhysicsWorldLookup.end() ? (*iter).value : NULL;
  173. }
  174. PhysicsWorld* Px3Plugin::getWorld() const
  175. {
  176. if ( mPhysicsWorldLookup.size() == 0 )
  177. return NULL;
  178. Map<StringNoCase, PhysicsWorld*>::ConstIterator iter = mPhysicsWorldLookup.begin();
  179. return iter->value;
  180. }
  181. U32 Px3Plugin::getWorldCount() const
  182. {
  183. return mPhysicsWorldLookup.size();
  184. }