physicsPlugin.cpp 7.5 KB

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