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