| [4558] | 1 | /* | 
|---|
| [3954] | 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
|  | 3 |  | 
|---|
|  | 4 | Copyright (C) 2004 orx | 
|---|
|  | 5 |  | 
|---|
|  | 6 | This program is free software; you can redistribute it and/or modify | 
|---|
|  | 7 | it under the terms of the GNU General Public License as published by | 
|---|
|  | 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
|  | 9 | any later version. | 
|---|
|  | 10 |  | 
|---|
|  | 11 | ### File Specific: | 
|---|
|  | 12 | main-programmer: ... | 
|---|
|  | 13 | co-programmer: ... | 
|---|
|  | 14 | */ | 
|---|
|  | 15 |  | 
|---|
| [4381] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PHYSICS | 
|---|
| [3954] | 17 |  | 
|---|
| [4121] | 18 | #include "physics_engine.h" | 
|---|
| [3954] | 19 |  | 
|---|
| [5217] | 20 | #include "class_list.h" | 
|---|
| [5944] | 21 | #include "parser/tinyxml/tinyxml.h" | 
|---|
| [7193] | 22 | #include "util/loading/factory.h" | 
|---|
|  | 23 | #include "util/loading/load_param.h" | 
|---|
| [3954] | 24 |  | 
|---|
|  | 25 | using namespace std; | 
|---|
|  | 26 |  | 
|---|
|  | 27 |  | 
|---|
|  | 28 | /** | 
|---|
| [4836] | 29 | *  standard constructor | 
|---|
| [3954] | 30 | */ | 
|---|
| [4746] | 31 | PhysicsEngine::PhysicsEngine() | 
|---|
| [3954] | 32 | { | 
|---|
| [4597] | 33 | this->setClassID(CL_PHYSICS_ENGINE, "PhysicsEngine"); | 
|---|
|  | 34 | this->setName("PhysicsEngine"); | 
|---|
| [5217] | 35 | this->interfaces = NULL; | 
|---|
| [3954] | 36 | } | 
|---|
|  | 37 |  | 
|---|
| [4183] | 38 | /** | 
|---|
| [4836] | 39 | *  the singleton reference to this class | 
|---|
| [4183] | 40 | */ | 
|---|
|  | 41 | PhysicsEngine* PhysicsEngine::singletonRef = NULL; | 
|---|
| [3954] | 42 |  | 
|---|
|  | 43 | /** | 
|---|
| [4836] | 44 | *  standard deconstructor | 
|---|
| [3954] | 45 |  | 
|---|
|  | 46 | */ | 
|---|
| [4746] | 47 | PhysicsEngine::~PhysicsEngine() | 
|---|
| [3954] | 48 | { | 
|---|
| [4749] | 49 | // delete all PhysicsConnections that are still in existence | 
|---|
| [5778] | 50 | while (this->connections.size() > 0) | 
|---|
|  | 51 | { | 
|---|
|  | 52 | PhysicsConnection* connection = this->connections.front(); | 
|---|
|  | 53 | this->connections.pop_front(); | 
|---|
|  | 54 | delete connection; | 
|---|
|  | 55 | } | 
|---|
| [5115] | 56 | // | 
|---|
|  | 57 | //   // delete all PhysicsInterfaces, still in existence (this could be dangerous) | 
|---|
|  | 58 | //   tIterator<PhysicsInterface>* itPI = this->interfaces->getIterator(); | 
|---|
|  | 59 | //   PhysicsInterface* enumPI = itPI->firstElement(); | 
|---|
|  | 60 | //   while (enumPI) | 
|---|
|  | 61 | //   { | 
|---|
|  | 62 | //     delete enumPI; | 
|---|
|  | 63 | // | 
|---|
|  | 64 | //     enumPI = itPI->nextElement(); | 
|---|
|  | 65 | //   } | 
|---|
|  | 66 | //   delete itPI; | 
|---|
|  | 67 | // | 
|---|
|  | 68 | //   // delete all PhysicsFields, still in existence (this could be dangerous) | 
|---|
|  | 69 | //   tIterator<Field>* itF = this->fields->getIterator(); | 
|---|
|  | 70 | //   Field* enumF = itF->firstElement(); | 
|---|
|  | 71 | //   while (enumF) | 
|---|
|  | 72 | //   { | 
|---|
|  | 73 | //     delete enumF; | 
|---|
|  | 74 | // | 
|---|
|  | 75 | //     enumF = itF->nextElement(); | 
|---|
|  | 76 | //   } | 
|---|
|  | 77 | //   delete itF; | 
|---|
| [4749] | 78 |  | 
|---|
| [4183] | 79 | PhysicsEngine::singletonRef = NULL; | 
|---|
| [3954] | 80 | } | 
|---|
|  | 81 |  | 
|---|
| [4728] | 82 | /** | 
|---|
| [4836] | 83 | * @param root the XML-element to load settings from | 
|---|
| [4728] | 84 | */ | 
|---|
| [4730] | 85 | void PhysicsEngine::loadParams(const TiXmlElement* root) | 
|---|
| [4728] | 86 | { | 
|---|
| [5652] | 87 | LoadParamXML(root, "Fields", this, PhysicsEngine, loadFields) | 
|---|
| [4733] | 88 | .describe("loads a list of fields"); | 
|---|
| [4392] | 89 |  | 
|---|
| [5652] | 90 | LoadParamXML(root, "Connections", this, PhysicsEngine, loadConnections) | 
|---|
| [4733] | 91 | .describe("loads a list of fields"); | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | /** | 
|---|
| [4836] | 95 | * @param root the XML-element to Load the PhysicsField from | 
|---|
| [4733] | 96 | */ | 
|---|
|  | 97 | void PhysicsEngine::loadFields(const TiXmlElement* root) | 
|---|
|  | 98 | { | 
|---|
| [4731] | 99 | PRINTF(4)("Loading Physical Fields\n"); | 
|---|
| [4733] | 100 |  | 
|---|
|  | 101 | const TiXmlElement* element = root->FirstChildElement(); | 
|---|
| [4730] | 102 | while (element != NULL) | 
|---|
| [4728] | 103 | { | 
|---|
| [5982] | 104 | Factory::fabricate(element); | 
|---|
| [4728] | 105 |  | 
|---|
|  | 106 | element = element->NextSiblingElement(); | 
|---|
|  | 107 | } | 
|---|
| [4733] | 108 | } | 
|---|
| [4728] | 109 |  | 
|---|
| [4733] | 110 | /** | 
|---|
| [4836] | 111 | * @param root the XML-element to load the PhysicsConnection from | 
|---|
| [4733] | 112 | */ | 
|---|
|  | 113 | void PhysicsEngine::loadConnections(const TiXmlElement* root) | 
|---|
|  | 114 | { | 
|---|
| [4731] | 115 | PRINTF(4)("Loading Physical Connections\n"); | 
|---|
| [4733] | 116 |  | 
|---|
|  | 117 | const TiXmlElement* element = root->FirstChildElement(); | 
|---|
| [4730] | 118 | while (element != NULL) | 
|---|
| [4728] | 119 | { | 
|---|
| [5982] | 120 | Factory::fabricate(element); | 
|---|
| [4728] | 121 |  | 
|---|
|  | 122 | element = element->NextSiblingElement(); | 
|---|
|  | 123 | } | 
|---|
|  | 124 | } | 
|---|
|  | 125 |  | 
|---|
| [3954] | 126 | /** | 
|---|
| [4836] | 127 | * @param physicsInterfaceName the Name of the PhysicsInterface to search for | 
|---|
|  | 128 | @returns the PhysicsInterface if found, or NULL if not | 
|---|
| [4728] | 129 | */ | 
|---|
| [7221] | 130 | PhysicsInterface* PhysicsEngine::getPhysicsInterfaceByName(const std::string& physicsInterfaceName) const | 
|---|
| [4728] | 131 | { | 
|---|
| [5779] | 132 | BaseObject* interface = ClassList::getObject(physicsInterfaceName, CL_PHYSICS_INTERFACE); | 
|---|
|  | 133 | return (interface != NULL)?  dynamic_cast<PhysicsInterface*>(interface) : NULL; | 
|---|
| [4728] | 134 | } | 
|---|
|  | 135 |  | 
|---|
|  | 136 | /** | 
|---|
| [4836] | 137 | *  adds a Field to the list of handeled fields | 
|---|
|  | 138 | * @param field the field to add | 
|---|
| [4394] | 139 |  | 
|---|
|  | 140 | this is normally done in the constructor of any Field | 
|---|
|  | 141 | */ | 
|---|
|  | 142 | void PhysicsEngine::addField(Field* field) | 
|---|
|  | 143 | { | 
|---|
| [5776] | 144 | this->fields.push_back(field); | 
|---|
| [4394] | 145 | } | 
|---|
|  | 146 |  | 
|---|
|  | 147 | /** | 
|---|
| [4836] | 148 | *  removes a Field from the list of handeled fields | 
|---|
|  | 149 | * @param field the field to remove | 
|---|
| [4394] | 150 |  | 
|---|
|  | 151 | this is normally done in the destructor of any Field | 
|---|
|  | 152 | */ | 
|---|
|  | 153 | void PhysicsEngine::removeField(Field* field) | 
|---|
|  | 154 | { | 
|---|
| [5776] | 155 | this->fields.remove(field); | 
|---|
| [4394] | 156 | } | 
|---|
|  | 157 |  | 
|---|
|  | 158 | /** | 
|---|
| [7221] | 159 | * @param fieldName the Name of the PhysicsInterface to search for | 
|---|
| [4836] | 160 | @returns the Field if found, or NULL if not | 
|---|
| [4728] | 161 | */ | 
|---|
| [7221] | 162 | Field* PhysicsEngine::getFieldByName(const std::string& fieldName) const | 
|---|
| [4728] | 163 | { | 
|---|
| [5776] | 164 | list<Field*>::const_iterator field; | 
|---|
|  | 165 | for (field = this->fields.begin(); field != this->fields.end(); field++) | 
|---|
| [7221] | 166 | if (fieldName == (*field)->getName()) | 
|---|
| [5776] | 167 | return (*field); | 
|---|
| [4728] | 168 | return NULL; | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
|  | 171 |  | 
|---|
|  | 172 |  | 
|---|
|  | 173 | /** | 
|---|
| [4836] | 174 | *  adds A Physical Connection to the List of Connections | 
|---|
|  | 175 | * @param connection the Connection to add | 
|---|
| [4558] | 176 |  | 
|---|
| [4183] | 177 | Usually this is done through the constructor of PhysicshConnections | 
|---|
|  | 178 | */ | 
|---|
|  | 179 | void PhysicsEngine::addConnection(PhysicsConnection* connection) | 
|---|
|  | 180 | { | 
|---|
| [5776] | 181 | this->connections.push_back(connection); | 
|---|
| [4183] | 182 | } | 
|---|
| [3954] | 183 |  | 
|---|
| [4183] | 184 | /** | 
|---|
| [4836] | 185 | *  removes A Physical Connection from the List of Connections | 
|---|
|  | 186 | * @param connection the Connection to remove | 
|---|
| [4558] | 187 |  | 
|---|
| [4183] | 188 | Usually this is done through the destructor of PhysicsConnections | 
|---|
| [3954] | 189 | */ | 
|---|
| [4183] | 190 | void PhysicsEngine::removeConnection(PhysicsConnection* connection) | 
|---|
|  | 191 | { | 
|---|
| [5776] | 192 | this->connections.remove(connection); | 
|---|
| [4183] | 193 | } | 
|---|
|  | 194 |  | 
|---|
| [4728] | 195 | /** | 
|---|
| [4836] | 196 | * @param physicsConnectionName the Name of the PhysicsInterface to search for | 
|---|
|  | 197 | @returns the PhysicsConnection if found, or NULL if not | 
|---|
| [4728] | 198 | */ | 
|---|
| [7221] | 199 | PhysicsConnection* PhysicsEngine::getPhysicsConnectionByName(const std::string& physicsConnectionName) const | 
|---|
| [4728] | 200 | { | 
|---|
| [5776] | 201 | list<PhysicsConnection*>::const_iterator pc; | 
|---|
|  | 202 | for (pc = this->connections.begin(); pc != this->connections.end(); pc++) | 
|---|
| [7221] | 203 | if (physicsConnectionName == (*pc)->getName()) | 
|---|
| [5776] | 204 | delete (*pc); | 
|---|
| [4728] | 205 | return NULL; | 
|---|
|  | 206 | } | 
|---|
| [4183] | 207 |  | 
|---|
|  | 208 |  | 
|---|
|  | 209 |  | 
|---|
|  | 210 | /** | 
|---|
| [4836] | 211 | *  Steps through all the Connections and Ticks them | 
|---|
|  | 212 | * @param dt The time Passed in Seconds | 
|---|
| [4183] | 213 |  | 
|---|
|  | 214 | This function brings a flow into the whole animation | 
|---|
|  | 215 | */ | 
|---|
|  | 216 | void PhysicsEngine::tick(float dt) | 
|---|
|  | 217 | { | 
|---|
| [4558] | 218 | /* go through all the PhysicsInterface(s) and tick them, | 
|---|
|  | 219 | meaning let the fields work */ | 
|---|
| [5776] | 220 | list<PhysicsConnection*>::iterator pc; | 
|---|
|  | 221 | for (pc = this->connections.begin(); pc != this->connections.end(); pc++) | 
|---|
|  | 222 | (*pc)->apply(); | 
|---|
| [4558] | 223 |  | 
|---|
|  | 224 | /* actually tick all the PhysicsInterfaces. Move the objects around */ | 
|---|
| [5217] | 225 | if (this->interfaces != NULL || (this->interfaces = ClassList::getList(CL_PHYSICS_INTERFACE)) != NULL) | 
|---|
|  | 226 | { | 
|---|
| [5779] | 227 | list<BaseObject*>::const_iterator tickPhys; | 
|---|
|  | 228 | for (tickPhys = this->interfaces->begin(); tickPhys != this->interfaces->end(); tickPhys++) | 
|---|
|  | 229 | dynamic_cast<PhysicsInterface*>(*tickPhys)->tickPhys(dt); | 
|---|
| [5217] | 230 | } | 
|---|
| [4183] | 231 | } | 
|---|
| [4378] | 232 |  | 
|---|
|  | 233 |  | 
|---|
|  | 234 |  | 
|---|
|  | 235 | /** | 
|---|
| [4836] | 236 | *  print out interesting debug information of this class | 
|---|
| [4378] | 237 | */ | 
|---|
| [4746] | 238 | void PhysicsEngine::debug() const | 
|---|
| [4378] | 239 | { | 
|---|
|  | 240 | PRINT(0)("====================================\n"); | 
|---|
|  | 241 | PRINT(0)("= Physics-Engine debug information =\n"); | 
|---|
|  | 242 | PRINT(0)("====================================\n"); | 
|---|
|  | 243 | PRINT(0)(" reference: %p\n", this); | 
|---|
| [5217] | 244 | if (this->interfaces != NULL) | 
|---|
| [5779] | 245 | PRINT(0)(" number of Interfaces: %d\n", this->interfaces->size()); | 
|---|
| [5776] | 246 | PRINT(0)(" number of Fields: %d\n", this->fields.size()); | 
|---|
|  | 247 | PRINT(0)(" number of Connections: %d\n", this->connections.size()); | 
|---|
| [4378] | 248 |  | 
|---|
|  | 249 | PRINT(0)("==============================PHYS==\n"); | 
|---|
|  | 250 | } | 
|---|