physicsPlugin.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/physicsPlugin.h"
  24. #include "console/console.h"
  25. #include "console/engineAPI.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/simSet.h"
  28. #include "core/strings/stringFunctions.h"
  29. #include "scene/sceneObject.h"
  30. #include "scene/sceneManager.h"
  31. #include "scene/sceneRenderState.h"
  32. #include "T3D/physics/physicsObject.h"
  33. #include "T3D/physics/physicsWorld.h"
  34. #include "core/util/tNamedFactory.h"
  35. PhysicsPlugin* PhysicsPlugin::smSingleton = NULL;
  36. PhysicsResetSignal PhysicsPlugin::smPhysicsResetSignal;
  37. bool PhysicsPlugin::smSinglePlayer = false;
  38. U32 PhysicsPlugin::smThreadCount = 2;
  39. String PhysicsPlugin::smServerWorldName( "server" );
  40. String PhysicsPlugin::smClientWorldName( "client" );
  41. AFTER_MODULE_INIT( Sim )
  42. {
  43. Con::addVariable( "$Physics::isSinglePlayer", TypeBool, &PhysicsPlugin::smSinglePlayer,
  44. "@brief Informs the physics simulation if only a single player exists.\n\n"
  45. "If true, optimizations will be implemented to better cater to a single player environmnent.\n\n"
  46. "@ingroup Physics\n");
  47. Con::addVariable( "$pref::Physics::threadCount", TypeS32, &PhysicsPlugin::smThreadCount,
  48. "@brief Number of threads to use in a single pass of the physics engine.\n\n"
  49. "Defaults to 2 if not set.\n\n"
  50. "@ingroup Physics\n");
  51. }
  52. bool PhysicsPlugin::activate( const char *library )
  53. {
  54. // Cleanup any previous plugin.
  55. if ( smSingleton )
  56. {
  57. smSingleton->destroyPlugin();
  58. AssertFatal( smSingleton == NULL,
  59. "PhysicsPlugin::activate - destroyPlugin didn't delete the plugin!" );
  60. }
  61. // Create it thru the factory.
  62. PhysicsPlugin *plugin = NamedFactory<PhysicsPlugin>::create( library );
  63. if ( !plugin )
  64. {
  65. // One last try... try the first available one.
  66. plugin = NamedFactory<PhysicsPlugin>::create();
  67. if ( !plugin )
  68. return false;
  69. }
  70. smSingleton = plugin;
  71. return true;
  72. }
  73. PhysicsPlugin::PhysicsPlugin()
  74. {
  75. mPhysicsCleanup = new SimSet();
  76. mPhysicsCleanup->assignName( "PhysicsCleanupSet" );
  77. mPhysicsCleanup->registerObject();
  78. Sim::getRootGroup()->addObject( mPhysicsCleanup );
  79. }
  80. PhysicsPlugin::~PhysicsPlugin()
  81. {
  82. AssertFatal( smSingleton == this, "PhysicsPlugin::~PhysicsPlugin() - Wrong active plugin!" );
  83. if ( mPhysicsCleanup )
  84. mPhysicsCleanup->deleteObject();
  85. smSingleton = NULL;
  86. }
  87. void PhysicsPlugin::enableDebugDraw( bool enabled )
  88. {
  89. if ( enabled )
  90. SceneManager::getPostRenderSignal().notify( &PhysicsPlugin::_debugDraw );
  91. else
  92. SceneManager::getPostRenderSignal().remove( &PhysicsPlugin::_debugDraw );
  93. _onDebugDrawEnabled( enabled );
  94. }
  95. void PhysicsPlugin::_debugDraw( SceneManager *graph, const SceneRenderState *state )
  96. {
  97. // We only debug draw in the diffuse pass if we have a physics object.
  98. if ( !PHYSICSMGR || !state->isDiffusePass() )
  99. return;
  100. // Render the server by default... else the client.
  101. PhysicsWorld *world = PHYSICSMGR->getWorld( smServerWorldName );
  102. if ( !world )
  103. world = PHYSICSMGR->getWorld( smClientWorldName );
  104. if ( world )
  105. world->onDebugDraw( state );
  106. }
  107. DefineConsoleFunction( physicsPluginPresent, bool, (), , "physicsPluginPresent()"
  108. "@brief Returns true if a physics plugin exists and is initialized.\n\n"
  109. "@ingroup Physics" )
  110. {
  111. return PHYSICSMGR != NULL;
  112. }
  113. DefineConsoleFunction( physicsInit, bool, (const char * library), ("default"), "physicsInit( [string library] )")
  114. {
  115. return PhysicsPlugin::activate( library );
  116. }
  117. DefineConsoleFunction( physicsDestroy, void, (), , "physicsDestroy()")
  118. {
  119. if ( PHYSICSMGR )
  120. PHYSICSMGR->destroyPlugin();
  121. }
  122. DefineConsoleFunction( physicsInitWorld, bool, (const char * worldName), , "physicsInitWorld( String worldName )")
  123. {
  124. bool res = PHYSICSMGR && PHYSICSMGR->createWorld( String( worldName ) );
  125. return res;
  126. }
  127. DefineConsoleFunction( physicsDestroyWorld, void, (const char * worldName), , "physicsDestroyWorld( String worldName )")
  128. {
  129. if ( PHYSICSMGR )
  130. PHYSICSMGR->destroyWorld( worldName );
  131. }
  132. // Control/query of the stop/started state
  133. // of the currently running simulation.
  134. DefineConsoleFunction( physicsStartSimulation, void, (const char * worldName), , "physicsStartSimulation( String worldName )")
  135. {
  136. if ( PHYSICSMGR )
  137. PHYSICSMGR->enableSimulation( String( worldName ), true );
  138. }
  139. DefineConsoleFunction( physicsStopSimulation, void, (const char * worldName), , "physicsStopSimulation( String worldName )")
  140. {
  141. if ( PHYSICSMGR )
  142. PHYSICSMGR->enableSimulation( String( worldName ), false );
  143. }
  144. DefineConsoleFunction( physicsSimulationEnabled, bool, (), , "physicsStopSimulation( String worldName )")
  145. {
  146. return PHYSICSMGR && PHYSICSMGR->isSimulationEnabled();
  147. }
  148. // Used for slowing down time on the
  149. // physics simulation, and for pausing/restarting
  150. // the simulation.
  151. DefineConsoleFunction( physicsSetTimeScale, void, (F32 scale), , "physicsSetTimeScale( F32 scale )")
  152. {
  153. if ( PHYSICSMGR )
  154. PHYSICSMGR->setTimeScale( scale );
  155. }
  156. // Get the currently set time scale.
  157. DefineConsoleFunction( physicsGetTimeScale, F32, (), , "physicsGetTimeScale()")
  158. {
  159. return PHYSICSMGR && PHYSICSMGR->getTimeScale();
  160. }
  161. // Used to send a signal to objects in the
  162. // physics simulation that they should store
  163. // their current state for later restoration,
  164. // such as when the editor is closed.
  165. DefineConsoleFunction( physicsStoreState, void, (), , "physicsStoreState()")
  166. {
  167. PhysicsPlugin::getPhysicsResetSignal().trigger( PhysicsResetEvent_Store );
  168. }
  169. // Used to send a signal to objects in the
  170. // physics simulation that they should restore
  171. // their saved state, such as when the editor is opened.
  172. DefineConsoleFunction( physicsRestoreState, void, (), , "physicsRestoreState()")
  173. {
  174. if ( PHYSICSMGR )
  175. PHYSICSMGR->reset();
  176. }
  177. DefineConsoleFunction( physicsDebugDraw, void, (bool enable), , "physicsDebugDraw( bool enable )")
  178. {
  179. if ( PHYSICSMGR )
  180. PHYSICSMGR->enableDebugDraw( enable );
  181. }