[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" |
---|
[4183] | 21 | #include "list.h" |
---|
[4728] | 22 | #include "tinyxml.h" |
---|
[4730] | 23 | #include "factory.h" |
---|
[4733] | 24 | #include "load_param.h" |
---|
[3954] | 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
[4836] | 30 | * standard constructor |
---|
[3954] | 31 | */ |
---|
[4746] | 32 | PhysicsEngine::PhysicsEngine() |
---|
[3954] | 33 | { |
---|
[4597] | 34 | this->setClassID(CL_PHYSICS_ENGINE, "PhysicsEngine"); |
---|
| 35 | this->setName("PhysicsEngine"); |
---|
[5217] | 36 | this->interfaces = NULL; |
---|
[3954] | 37 | } |
---|
| 38 | |
---|
[4183] | 39 | /** |
---|
[4836] | 40 | * the singleton reference to this class |
---|
[4183] | 41 | */ |
---|
| 42 | PhysicsEngine* PhysicsEngine::singletonRef = NULL; |
---|
[3954] | 43 | |
---|
| 44 | /** |
---|
[4836] | 45 | * standard deconstructor |
---|
[3954] | 46 | |
---|
| 47 | */ |
---|
[4746] | 48 | PhysicsEngine::~PhysicsEngine() |
---|
[3954] | 49 | { |
---|
[4749] | 50 | // delete all PhysicsConnections that are still in existence |
---|
[5776] | 51 | list<PhysicsConnection*>::iterator pc; |
---|
| 52 | for (pc = this->connections.begin(); pc != this->connections.end(); pc++) |
---|
| 53 | delete (*pc); |
---|
[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 | { |
---|
[4731] | 102 | Factory::getFirst()->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 | { |
---|
[4730] | 118 | Factory::getFirst()->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 | */ |
---|
| 128 | PhysicsInterface* PhysicsEngine::getPhysicsInterfaceByName(const char* physicsInterfaceName) const |
---|
| 129 | { |
---|
[5217] | 130 | tIterator<BaseObject>* tmpIt = ClassList::getList(CL_PHYSICS_INTERFACE)->getIterator(); |
---|
| 131 | BaseObject* tmpInt = tmpIt->firstElement(); |
---|
[4728] | 132 | while(tmpInt) |
---|
| 133 | { |
---|
| 134 | if (!strcmp(physicsInterfaceName, tmpInt->getName())) |
---|
| 135 | { |
---|
| 136 | delete tmpIt; |
---|
[5217] | 137 | return dynamic_cast<PhysicsInterface*>(tmpInt); |
---|
[4728] | 138 | } |
---|
| 139 | tmpInt = tmpIt->nextElement(); |
---|
| 140 | } |
---|
| 141 | delete tmpIt; |
---|
| 142 | return NULL; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
[4836] | 146 | * adds a Field to the list of handeled fields |
---|
| 147 | * @param field the field to add |
---|
[4394] | 148 | |
---|
| 149 | this is normally done in the constructor of any Field |
---|
| 150 | */ |
---|
| 151 | void PhysicsEngine::addField(Field* field) |
---|
| 152 | { |
---|
[5776] | 153 | this->fields.push_back(field); |
---|
[4394] | 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
[4836] | 157 | * removes a Field from the list of handeled fields |
---|
| 158 | * @param field the field to remove |
---|
[4394] | 159 | |
---|
| 160 | this is normally done in the destructor of any Field |
---|
| 161 | */ |
---|
| 162 | void PhysicsEngine::removeField(Field* field) |
---|
| 163 | { |
---|
[5776] | 164 | this->fields.remove(field); |
---|
[4394] | 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
[4836] | 168 | * @param FieldName the Name of the PhysicsInterface to search for |
---|
| 169 | @returns the Field if found, or NULL if not |
---|
[4728] | 170 | */ |
---|
| 171 | Field* PhysicsEngine::getFieldByName(const char* FieldName) const |
---|
| 172 | { |
---|
[5776] | 173 | list<Field*>::const_iterator field; |
---|
| 174 | for (field = this->fields.begin(); field != this->fields.end(); field++) |
---|
| 175 | if (!strcmp(FieldName, (*field)->getName())) |
---|
| 176 | return (*field); |
---|
[4728] | 177 | return NULL; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | /** |
---|
[4836] | 183 | * adds A Physical Connection to the List of Connections |
---|
| 184 | * @param connection the Connection to add |
---|
[4558] | 185 | |
---|
[4183] | 186 | Usually this is done through the constructor of PhysicshConnections |
---|
| 187 | */ |
---|
| 188 | void PhysicsEngine::addConnection(PhysicsConnection* connection) |
---|
| 189 | { |
---|
[5776] | 190 | this->connections.push_back(connection); |
---|
[4183] | 191 | } |
---|
[3954] | 192 | |
---|
[4183] | 193 | /** |
---|
[4836] | 194 | * removes A Physical Connection from the List of Connections |
---|
| 195 | * @param connection the Connection to remove |
---|
[4558] | 196 | |
---|
[4183] | 197 | Usually this is done through the destructor of PhysicsConnections |
---|
[3954] | 198 | */ |
---|
[4183] | 199 | void PhysicsEngine::removeConnection(PhysicsConnection* connection) |
---|
| 200 | { |
---|
[5776] | 201 | this->connections.remove(connection); |
---|
[4183] | 202 | } |
---|
| 203 | |
---|
[4728] | 204 | /** |
---|
[4836] | 205 | * @param physicsConnectionName the Name of the PhysicsInterface to search for |
---|
| 206 | @returns the PhysicsConnection if found, or NULL if not |
---|
[4728] | 207 | */ |
---|
| 208 | PhysicsConnection* PhysicsEngine::getPhysicsConnectionByName(const char* physicsConnectionName) const |
---|
| 209 | { |
---|
[5776] | 210 | list<PhysicsConnection*>::const_iterator pc; |
---|
| 211 | for (pc = this->connections.begin(); pc != this->connections.end(); pc++) |
---|
| 212 | if (!strcmp(physicsConnectionName, (*pc)->getName())) |
---|
| 213 | delete (*pc); |
---|
[4728] | 214 | return NULL; |
---|
| 215 | } |
---|
[4183] | 216 | |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | /** |
---|
[4836] | 220 | * Steps through all the Connections and Ticks them |
---|
| 221 | * @param dt The time Passed in Seconds |
---|
[4183] | 222 | |
---|
| 223 | This function brings a flow into the whole animation |
---|
| 224 | */ |
---|
| 225 | void PhysicsEngine::tick(float dt) |
---|
| 226 | { |
---|
[4558] | 227 | /* go through all the PhysicsInterface(s) and tick them, |
---|
| 228 | meaning let the fields work */ |
---|
[5776] | 229 | list<PhysicsConnection*>::iterator pc; |
---|
| 230 | for (pc = this->connections.begin(); pc != this->connections.end(); pc++) |
---|
| 231 | (*pc)->apply(); |
---|
[4558] | 232 | |
---|
| 233 | /* actually tick all the PhysicsInterfaces. Move the objects around */ |
---|
[5217] | 234 | if (this->interfaces != NULL || (this->interfaces = ClassList::getList(CL_PHYSICS_INTERFACE)) != NULL) |
---|
| 235 | { |
---|
| 236 | tIterator<BaseObject>* itPI = this->interfaces->getIterator(); |
---|
| 237 | PhysicsInterface* enumPI = dynamic_cast<PhysicsInterface*>(itPI->firstElement()); |
---|
| 238 | while (enumPI) |
---|
[4396] | 239 | { |
---|
[5217] | 240 | enumPI->tickPhys(dt); |
---|
[4558] | 241 | |
---|
[5217] | 242 | enumPI = dynamic_cast<PhysicsInterface*>(itPI->nextElement()); |
---|
[4396] | 243 | } |
---|
[5217] | 244 | delete itPI; |
---|
| 245 | } |
---|
[4183] | 246 | } |
---|
[4378] | 247 | |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | /** |
---|
[4836] | 251 | * print out interesting debug information of this class |
---|
[4378] | 252 | */ |
---|
[4746] | 253 | void PhysicsEngine::debug() const |
---|
[4378] | 254 | { |
---|
| 255 | PRINT(0)("====================================\n"); |
---|
| 256 | PRINT(0)("= Physics-Engine debug information =\n"); |
---|
| 257 | PRINT(0)("====================================\n"); |
---|
| 258 | PRINT(0)(" reference: %p\n", this); |
---|
[5217] | 259 | if (this->interfaces != NULL) |
---|
| 260 | PRINT(0)(" number of Interfaces: %d\n", this->interfaces->getSize()); |
---|
[5776] | 261 | PRINT(0)(" number of Fields: %d\n", this->fields.size()); |
---|
| 262 | PRINT(0)(" number of Connections: %d\n", this->connections.size()); |
---|
[4378] | 263 | |
---|
| 264 | PRINT(0)("==============================PHYS==\n"); |
---|
| 265 | } |
---|