px3Plugin.cpp

Engine/source/T3D/physics/physx3/px3Plugin.cpp

More...

Public Functions

Detailed Description

Public Functions

AFTER_MODULE_INIT(Sim )

  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 "console/consoleTypes.h"
 26#include "T3D/physics/physx3/px3World.h"
 27#include "T3D/physics/physx3/px3Plugin.h"
 28#include "T3D/physics/physx3/px3Collision.h"
 29#include "T3D/physics/physx3/px3Body.h"
 30#include "T3D/physics/physx3/px3Player.h"
 31
 32#include "T3D/physics/physicsShape.h"
 33#include "T3D/gameBase/gameProcess.h"
 34#include "core/util/tNamedFactory.h"
 35
 36
 37AFTER_MODULE_INIT( Sim )
 38{
 39   NamedFactory<PhysicsPlugin>::add( "PhysX3", &Px3Plugin::create );
 40
 41   #if defined(TORQUE_OS_WIN) || defined(TORQUE_OS_XBOX) || defined(TORQUE_OS_XENON)   
 42      NamedFactory<PhysicsPlugin>::add( "default", &Px3Plugin::create );
 43   #endif   
 44}
 45
 46PhysicsPlugin* Px3Plugin::create()
 47{
 48   // Only create the plugin if it hasn't been set up AND
 49   // the PhysX world is successfully initialized.
 50   bool success = Px3World::restartSDK( false );
 51   if ( success )
 52      return new Px3Plugin();
 53   
 54   return NULL;
 55}
 56
 57Px3Plugin::Px3Plugin()
 58{
 59}
 60
 61Px3Plugin::~Px3Plugin()
 62{
 63}
 64
 65void Px3Plugin::destroyPlugin()
 66{
 67   // Cleanup any worlds that are still kicking.
 68   Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.begin();
 69   for ( ; iter != mPhysicsWorldLookup.end(); iter++ )
 70   {
 71      iter->value->destroyWorld();
 72      delete iter->value;
 73   }
 74   mPhysicsWorldLookup.clear();
 75
 76   Px3World::restartSDK( true );
 77
 78   delete this;
 79}
 80
 81void Px3Plugin::reset()
 82{
 83   // First delete all the cleanup objects.
 84   if ( getPhysicsCleanup() )
 85      getPhysicsCleanup()->deleteAllObjects();
 86
 87   getPhysicsResetSignal().trigger( PhysicsResetEvent_Restore );
 88
 89   // Now let each world reset itself.
 90   Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.begin();
 91   for ( ; iter != mPhysicsWorldLookup.end(); iter++ )
 92      iter->value->reset();
 93}
 94
 95PhysicsCollision* Px3Plugin::createCollision()
 96{
 97   return new Px3Collision();
 98}
 99
100PhysicsBody* Px3Plugin::createBody()
101{
102   return new Px3Body();
103}
104
105PhysicsPlayer* Px3Plugin::createPlayer()
106{
107   return new Px3Player();
108}
109
110bool Px3Plugin::isSimulationEnabled() const
111{
112   bool ret = false;
113   Px3World *world = static_cast<Px3World*>( getWorld( smClientWorldName ) );
114   if ( world )
115   {
116      ret = world->isEnabled();
117      return ret;
118   }
119
120   world = static_cast<Px3World*>( getWorld( smServerWorldName ) );
121   if ( world )
122   {
123      ret = world->isEnabled();
124      return ret;
125   }
126
127   return ret;
128}
129
130void Px3Plugin::enableSimulation( const String &worldName, bool enable )
131{
132   Px3World *world = static_cast<Px3World*>( getWorld( worldName ) );
133   if ( world )
134      world->setEnabled( enable );
135}
136
137void Px3Plugin::setTimeScale( const F32 timeScale )
138{
139   // Grab both the client and
140   // server worlds and set their time
141   // scales to the passed value.
142   Px3World *world = static_cast<Px3World*>( getWorld( smClientWorldName ) );
143   if ( world )
144      world->setEditorTimeScale( timeScale );
145
146   world = static_cast<Px3World*>( getWorld( smServerWorldName ) );
147   if ( world )
148      world->setEditorTimeScale( timeScale );
149}
150
151const F32 Px3Plugin::getTimeScale() const
152{
153   // Grab both the client and
154   // server worlds and call 
155   // setEnabled( true ) on them.
156   Px3World *world = static_cast<Px3World*>( getWorld( smClientWorldName ) );
157   if ( !world )
158   {
159      world = static_cast<Px3World*>( getWorld( smServerWorldName ) );
160      if ( !world )
161         return 0.0f;
162   }
163   
164   return world->getEditorTimeScale();
165}
166
167bool Px3Plugin::createWorld( const String &worldName )
168{
169   Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.find( worldName );
170   PhysicsWorld *world = NULL;
171   
172   iter != mPhysicsWorldLookup.end() ? world = (*iter).value : world = NULL; 
173
174   if ( world ) 
175   {
176      Con::errorf( "Px3Plugin::createWorld - %s world already exists!", worldName.c_str() );
177      return false;
178   }
179
180   world = new Px3World();
181   
182   if ( worldName.equal( smClientWorldName, String::NoCase ) )
183      world->initWorld( false, ClientProcessList::get() );
184   else
185      world->initWorld( true, ServerProcessList::get() );
186
187   mPhysicsWorldLookup.insert( worldName, world );
188
189   return world != NULL;
190}
191
192void Px3Plugin::destroyWorld( const String &worldName )
193{
194   Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.find( worldName );
195   if ( iter == mPhysicsWorldLookup.end() )
196      return;
197
198   PhysicsWorld *world = (*iter).value;
199   world->destroyWorld();
200   delete world;
201   
202   mPhysicsWorldLookup.erase( iter );
203}
204
205PhysicsWorld* Px3Plugin::getWorld( const String &worldName ) const
206{
207   if ( mPhysicsWorldLookup.isEmpty() )
208      return NULL;
209
210   Map<StringNoCase, PhysicsWorld*>::ConstIterator iter = mPhysicsWorldLookup.find( worldName );
211
212   return iter != mPhysicsWorldLookup.end() ? (*iter).value : NULL;
213}
214
215PhysicsWorld* Px3Plugin::getWorld() const
216{
217   if ( mPhysicsWorldLookup.size() == 0 )
218      return NULL;
219
220   Map<StringNoCase, PhysicsWorld*>::ConstIterator iter = mPhysicsWorldLookup.begin();
221   return iter->value;
222}
223
224U32 Px3Plugin::getWorldCount() const
225{ 
226   return mPhysicsWorldLookup.size(); 
227}
228