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