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