physicsPlugin.cpp 6.9 KB

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