btPlugin.cpp 5.8 KB

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