Torque3D Documentation / _generateds / physicsPlugin.cpp

physicsPlugin.cpp

Engine/source/T3D/physics/physicsPlugin.cpp

More...

Public Functions

DefineEngineFunction(physicsDebugDraw , void , (bool enable) , "physicsDebugDraw( bool enable )" )
DefineEngineFunction(physicsDestroy , void , () , "physicsDestroy()" )
DefineEngineFunction(physicsDestroyWorld , void , (const char *worldName) , "physicsDestroyWorld( <a href="/coding/class/classstring/">String</a> worldName )" )
DefineEngineFunction(physicsGetTimeScale , F32 , () , "physicsGetTimeScale()" )
DefineEngineFunction(physicsInit , bool , (const char *library) , ("default") , "physicsInit( [string library] )" )
DefineEngineFunction(physicsInitWorld , bool , (const char *worldName) , "physicsInitWorld( <a href="/coding/class/classstring/">String</a> worldName )" )
DefineEngineFunction(physicsPluginPresent , bool , () , "physicsPluginPresent()" "@brief Returns true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> physics plugin exists and is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">initialized.\n\n</a>" "@ingroup Physics" )
DefineEngineFunction(physicsRestoreState , void , () , "physicsRestoreState()" )
DefineEngineFunction(physicsSetTimeScale , void , (F32 scale) , "physicsSetTimeScale( <a href="/coding/file/types_8h/#types_8h_1a841d3674577a1e86afdc2f4845f46c4b">F32</a> scale )" )
DefineEngineFunction(physicsSimulationEnabled , bool , () , "physicsStopSimulation( <a href="/coding/class/classstring/">String</a> worldName )" )
DefineEngineFunction(physicsStartSimulation , void , (const char *worldName) , "physicsStartSimulation( <a href="/coding/class/classstring/">String</a> worldName )" )
DefineEngineFunction(physicsStopSimulation , void , (const char *worldName) , "physicsStopSimulation( <a href="/coding/class/classstring/">String</a> worldName )" )
DefineEngineFunction(physicsStoreState , void , () , "physicsStoreState()" )

Detailed Description

Public Functions

AFTER_MODULE_INIT(Sim )

DefineEngineFunction(physicsDebugDraw , void , (bool enable) , "physicsDebugDraw( bool enable )" )

DefineEngineFunction(physicsDestroy , void , () , "physicsDestroy()" )

DefineEngineFunction(physicsDestroyWorld , void , (const char *worldName) , "physicsDestroyWorld( <a href="/coding/class/classstring/">String</a> worldName )" )

DefineEngineFunction(physicsGetTimeScale , F32 , () , "physicsGetTimeScale()" )

DefineEngineFunction(physicsInit , bool , (const char *library) , ("default") , "physicsInit( [string library] )" )

DefineEngineFunction(physicsInitWorld , bool , (const char *worldName) , "physicsInitWorld( <a href="/coding/class/classstring/">String</a> worldName )" )

DefineEngineFunction(physicsPluginPresent , bool , () , "physicsPluginPresent()" "@brief Returns true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> physics plugin exists and is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">initialized.\n\n</a>" "@ingroup Physics" )

DefineEngineFunction(physicsRestoreState , void , () , "physicsRestoreState()" )

DefineEngineFunction(physicsSetTimeScale , void , (F32 scale) , "physicsSetTimeScale( <a href="/coding/file/types_8h/#types_8h_1a841d3674577a1e86afdc2f4845f46c4b">F32</a> scale )" )

DefineEngineFunction(physicsSimulationEnabled , bool , () , "physicsStopSimulation( <a href="/coding/class/classstring/">String</a> worldName )" )

DefineEngineFunction(physicsStartSimulation , void , (const char *worldName) , "physicsStartSimulation( <a href="/coding/class/classstring/">String</a> worldName )" )

DefineEngineFunction(physicsStopSimulation , void , (const char *worldName) , "physicsStopSimulation( <a href="/coding/class/classstring/">String</a> worldName )" )

DefineEngineFunction(physicsStoreState , void , () , "physicsStoreState()" )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "T3D/physics/physicsPlugin.h"
 26
 27#include "console/console.h"
 28#include "console/engineAPI.h"
 29#include "console/consoleTypes.h"
 30#include "console/simSet.h"
 31#include "core/strings/stringFunctions.h"
 32#include "scene/sceneObject.h"
 33#include "scene/sceneManager.h"
 34#include "scene/sceneRenderState.h"
 35#include "T3D/physics/physicsObject.h"
 36#include "T3D/physics/physicsWorld.h"
 37#include "core/util/tNamedFactory.h"
 38
 39PhysicsPlugin* PhysicsPlugin::smSingleton = NULL;
 40PhysicsResetSignal PhysicsPlugin::smPhysicsResetSignal;
 41bool PhysicsPlugin::smSinglePlayer = false;
 42U32 PhysicsPlugin::smThreadCount = 2;
 43bool PhysicsPlugin::smGpuAccelerationAllowed = false;
 44
 45String PhysicsPlugin::smServerWorldName( "server" );
 46String PhysicsPlugin::smClientWorldName( "client" );
 47
 48AFTER_MODULE_INIT( Sim )
 49{
 50   Con::addVariable( "$Physics::isSinglePlayer", TypeBool, &PhysicsPlugin::smSinglePlayer, 
 51      "@brief Informs the physics simulation if only a single player exists.\n\n"
 52      "If true, optimizations will be implemented to better cater to a single player environmnent.\n\n"
 53      "@ingroup Physics\n");
 54   Con::addVariable("$Physics::gpuAccelerationAllowed", TypeBool, &PhysicsPlugin::smGpuAccelerationAllowed,
 55      "@brief Informs the physics plugin if it is allowed to use gpu acceleration.\n\n"
 56      "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"
 57      "@ingroup Physics\n");
 58   Con::addVariable( "$pref::Physics::threadCount", TypeS32, &PhysicsPlugin::smThreadCount, 
 59      "@brief Number of threads to use in a single pass of the physics engine.\n\n"
 60      "Defaults to 2 if not set.\n\n"
 61      "@ingroup Physics\n");
 62}
 63
 64bool PhysicsPlugin::activate( const char *library )
 65{
 66   // Cleanup any previous plugin.
 67   if ( smSingleton )
 68   {
 69      smSingleton->destroyPlugin();
 70      AssertFatal( smSingleton == NULL, 
 71         "PhysicsPlugin::activate - destroyPlugin didn't delete the plugin!" );
 72   }
 73
 74   // Create it thru the factory.
 75   PhysicsPlugin *plugin = NamedFactory<PhysicsPlugin>::create( library );
 76   if ( !plugin )
 77   {
 78      // One last try... try the first available one.
 79      plugin = NamedFactory<PhysicsPlugin>::create();
 80      if ( !plugin )
 81         return false;
 82   }
 83
 84   smSingleton = plugin;
 85   return true;
 86}
 87
 88PhysicsPlugin::PhysicsPlugin()
 89{
 90   mPhysicsCleanup = new SimSet();
 91   mPhysicsCleanup->assignName( "PhysicsCleanupSet" );
 92   mPhysicsCleanup->registerObject();
 93   Sim::getRootGroup()->addObject( mPhysicsCleanup );   
 94}
 95
 96PhysicsPlugin::~PhysicsPlugin()
 97{
 98   AssertFatal( smSingleton == this, "PhysicsPlugin::~PhysicsPlugin() - Wrong active plugin!" );
 99   
100   if ( mPhysicsCleanup )
101      mPhysicsCleanup->deleteObject();
102
103   smSingleton = NULL;
104}
105
106void PhysicsPlugin::enableDebugDraw( bool enabled )
107{
108   if ( enabled )
109      SceneManager::getPostRenderSignal().notify( &PhysicsPlugin::_debugDraw );
110   else
111      SceneManager::getPostRenderSignal().remove( &PhysicsPlugin::_debugDraw );
112
113   _onDebugDrawEnabled( enabled );
114}
115
116void PhysicsPlugin::_debugDraw( SceneManager *graph, const SceneRenderState *state )
117{
118   // We only debug draw in the diffuse pass if we have a physics object.
119   if ( !PHYSICSMGR || !state->isDiffusePass() )
120      return;
121
122   // Render the server by default... else the client.
123   PhysicsWorld *world = PHYSICSMGR->getWorld( smServerWorldName );
124   if ( !world )
125      world = PHYSICSMGR->getWorld( smClientWorldName );
126
127   if ( world )
128      world->onDebugDraw( state );
129}
130
131DefineEngineFunction( physicsPluginPresent, bool, (), , "physicsPluginPresent()"
132   "@brief Returns true if a physics plugin exists and is initialized.\n\n"
133   "@ingroup Physics" )
134{
135   return PHYSICSMGR != NULL;
136}
137
138DefineEngineFunction( physicsInit, bool, (const char * library), ("default"), "physicsInit( [string library] )")
139{
140   return PhysicsPlugin::activate( library );
141}
142
143DefineEngineFunction( physicsDestroy, void, (), , "physicsDestroy()")
144{
145   if ( PHYSICSMGR )
146      PHYSICSMGR->destroyPlugin();
147}
148
149DefineEngineFunction( physicsInitWorld, bool, (const char * worldName), , "physicsInitWorld( String worldName )")
150{
151    bool res = PHYSICSMGR && PHYSICSMGR->createWorld( String( worldName ) );
152   return res;
153}
154
155DefineEngineFunction( physicsDestroyWorld, void, (const char * worldName), , "physicsDestroyWorld( String worldName )")
156{
157   if ( PHYSICSMGR )
158      PHYSICSMGR->destroyWorld( worldName );
159}
160
161
162// Control/query of the stop/started state
163// of the currently running simulation.
164DefineEngineFunction( physicsStartSimulation, void, (const char * worldName), , "physicsStartSimulation( String worldName )")
165{
166   if ( PHYSICSMGR )
167      PHYSICSMGR->enableSimulation( String( worldName ), true );
168}
169
170DefineEngineFunction( physicsStopSimulation, void, (const char * worldName), , "physicsStopSimulation( String worldName )")
171{
172   if ( PHYSICSMGR )
173      PHYSICSMGR->enableSimulation( String( worldName ), false );
174}
175
176DefineEngineFunction( physicsSimulationEnabled, bool, (), , "physicsStopSimulation( String worldName )")
177{
178   return PHYSICSMGR && PHYSICSMGR->isSimulationEnabled();
179}
180
181// Used for slowing down time on the
182// physics simulation, and for pausing/restarting
183// the simulation.
184DefineEngineFunction( physicsSetTimeScale, void, (F32 scale), , "physicsSetTimeScale( F32 scale )")
185{
186   if ( PHYSICSMGR )
187      PHYSICSMGR->setTimeScale( scale );
188}
189
190// Get the currently set time scale.
191DefineEngineFunction( physicsGetTimeScale, F32, (), , "physicsGetTimeScale()")
192{
193   return PHYSICSMGR && PHYSICSMGR->getTimeScale();
194}
195
196// Used to send a signal to objects in the
197// physics simulation that they should store
198// their current state for later restoration,
199// such as when the editor is closed.
200DefineEngineFunction( physicsStoreState, void, (), , "physicsStoreState()")
201{
202   PhysicsPlugin::getPhysicsResetSignal().trigger( PhysicsResetEvent_Store );
203}
204
205// Used to send a signal to objects in the
206// physics simulation that they should restore
207// their saved state, such as when the editor is opened.
208DefineEngineFunction( physicsRestoreState, void, (), , "physicsRestoreState()")
209{
210   if ( PHYSICSMGR )
211      PHYSICSMGR->reset();
212}
213
214DefineEngineFunction( physicsDebugDraw, void, (bool enable), , "physicsDebugDraw( bool enable )")
215{
216   if ( PHYSICSMGR )
217      PHYSICSMGR->enableDebugDraw( enable );
218}
219