[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" |
---|
[5955] | 22 | #include "parser/tinyxml/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 |
---|
[5778] | 51 | while (this->connections.size() > 0) |
---|
| 52 | { |
---|
| 53 | PhysicsConnection* connection = this->connections.front(); |
---|
| 54 | this->connections.pop_front(); |
---|
| 55 | delete connection; |
---|
| 56 | } |
---|
[5115] | 57 | // |
---|
| 58 | // // delete all PhysicsInterfaces, still in existence (this could be dangerous) |
---|
| 59 | // tIterator<PhysicsInterface>* itPI = this->interfaces->getIterator(); |
---|
| 60 | // PhysicsInterface* enumPI = itPI->firstElement(); |
---|
| 61 | // while (enumPI) |
---|
| 62 | // { |
---|
| 63 | // delete enumPI; |
---|
| 64 | // |
---|
| 65 | // enumPI = itPI->nextElement(); |
---|
| 66 | // } |
---|
| 67 | // delete itPI; |
---|
| 68 | // |
---|
| 69 | // // delete all PhysicsFields, still in existence (this could be dangerous) |
---|
| 70 | // tIterator<Field>* itF = this->fields->getIterator(); |
---|
| 71 | // Field* enumF = itF->firstElement(); |
---|
| 72 | // while (enumF) |
---|
| 73 | // { |
---|
| 74 | // delete enumF; |
---|
| 75 | // |
---|
| 76 | // enumF = itF->nextElement(); |
---|
| 77 | // } |
---|
| 78 | // delete itF; |
---|
[4749] | 79 | |
---|
[4183] | 80 | PhysicsEngine::singletonRef = NULL; |
---|
[3954] | 81 | } |
---|
| 82 | |
---|
[4728] | 83 | /** |
---|
[4836] | 84 | * @param root the XML-element to load settings from |
---|
[4728] | 85 | */ |
---|
[4730] | 86 | void PhysicsEngine::loadParams(const TiXmlElement* root) |
---|
[4728] | 87 | { |
---|
[5652] | 88 | LoadParamXML(root, "Fields", this, PhysicsEngine, loadFields) |
---|
[4733] | 89 | .describe("loads a list of fields"); |
---|
[4392] | 90 | |
---|
[5652] | 91 | LoadParamXML(root, "Connections", this, PhysicsEngine, loadConnections) |
---|
[4733] | 92 | .describe("loads a list of fields"); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
[4836] | 96 | * @param root the XML-element to Load the PhysicsField from |
---|
[4733] | 97 | */ |
---|
| 98 | void PhysicsEngine::loadFields(const TiXmlElement* root) |
---|
| 99 | { |
---|
[4731] | 100 | PRINTF(4)("Loading Physical Fields\n"); |
---|
[4733] | 101 | |
---|
| 102 | const TiXmlElement* element = root->FirstChildElement(); |
---|
[4730] | 103 | while (element != NULL) |
---|
[4728] | 104 | { |
---|
[5985] | 105 | Factory::fabricate(element); |
---|
[4728] | 106 | |
---|
| 107 | element = element->NextSiblingElement(); |
---|
| 108 | } |
---|
[4733] | 109 | } |
---|
[4728] | 110 | |
---|
[4733] | 111 | /** |
---|
[4836] | 112 | * @param root the XML-element to load the PhysicsConnection from |
---|
[4733] | 113 | */ |
---|
| 114 | void PhysicsEngine::loadConnections(const TiXmlElement* root) |
---|
| 115 | { |
---|
[4731] | 116 | PRINTF(4)("Loading Physical Connections\n"); |
---|
[4733] | 117 | |
---|
| 118 | const TiXmlElement* element = root->FirstChildElement(); |
---|
[4730] | 119 | while (element != NULL) |
---|
[4728] | 120 | { |
---|
[5985] | 121 | Factory::fabricate(element); |
---|
[4728] | 122 | |
---|
| 123 | element = element->NextSiblingElement(); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
[3954] | 127 | /** |
---|
[4836] | 128 | * @param physicsInterfaceName the Name of the PhysicsInterface to search for |
---|
| 129 | @returns the PhysicsInterface if found, or NULL if not |
---|
[4728] | 130 | */ |
---|
| 131 | PhysicsInterface* PhysicsEngine::getPhysicsInterfaceByName(const char* physicsInterfaceName) const |
---|
| 132 | { |
---|
[5779] | 133 | BaseObject* interface = ClassList::getObject(physicsInterfaceName, CL_PHYSICS_INTERFACE); |
---|
| 134 | return (interface != NULL)? dynamic_cast<PhysicsInterface*>(interface) : NULL; |
---|
[4728] | 135 | } |
---|
| 136 | |
---|
| 137 | /** |
---|
[4836] | 138 | * adds a Field to the list of handeled fields |
---|
| 139 | * @param field the field to add |
---|
[4394] | 140 | |
---|
| 141 | this is normally done in the constructor of any Field |
---|
| 142 | */ |
---|
| 143 | void PhysicsEngine::addField(Field* field) |
---|
| 144 | { |
---|
[5776] | 145 | this->fields.push_back(field); |
---|
[4394] | 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
[4836] | 149 | * removes a Field from the list of handeled fields |
---|
| 150 | * @param field the field to remove |
---|
[4394] | 151 | |
---|
| 152 | this is normally done in the destructor of any Field |
---|
| 153 | */ |
---|
| 154 | void PhysicsEngine::removeField(Field* field) |
---|
| 155 | { |
---|
[5776] | 156 | this->fields.remove(field); |
---|
[4394] | 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
[4836] | 160 | * @param FieldName the Name of the PhysicsInterface to search for |
---|
| 161 | @returns the Field if found, or NULL if not |
---|
[4728] | 162 | */ |
---|
| 163 | Field* PhysicsEngine::getFieldByName(const char* FieldName) const |
---|
| 164 | { |
---|
[5776] | 165 | list<Field*>::const_iterator field; |
---|
| 166 | for (field = this->fields.begin(); field != this->fields.end(); field++) |
---|
| 167 | if (!strcmp(FieldName, (*field)->getName())) |
---|
| 168 | return (*field); |
---|
[4728] | 169 | return NULL; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | /** |
---|
[4836] | 175 | * adds A Physical Connection to the List of Connections |
---|
| 176 | * @param connection the Connection to add |
---|
[4558] | 177 | |
---|
[4183] | 178 | Usually this is done through the constructor of PhysicshConnections |
---|
| 179 | */ |
---|
| 180 | void PhysicsEngine::addConnection(PhysicsConnection* connection) |
---|
| 181 | { |
---|
[5776] | 182 | this->connections.push_back(connection); |
---|
[4183] | 183 | } |
---|
[3954] | 184 | |
---|
[4183] | 185 | /** |
---|
[4836] | 186 | * removes A Physical Connection from the List of Connections |
---|
| 187 | * @param connection the Connection to remove |
---|
[4558] | 188 | |
---|
[4183] | 189 | Usually this is done through the destructor of PhysicsConnections |
---|
[3954] | 190 | */ |
---|
[4183] | 191 | void PhysicsEngine::removeConnection(PhysicsConnection* connection) |
---|
| 192 | { |
---|
[5776] | 193 | this->connections.remove(connection); |
---|
[4183] | 194 | } |
---|
| 195 | |
---|
[4728] | 196 | /** |
---|
[4836] | 197 | * @param physicsConnectionName the Name of the PhysicsInterface to search for |
---|
| 198 | @returns the PhysicsConnection if found, or NULL if not |
---|
[4728] | 199 | */ |
---|
| 200 | PhysicsConnection* PhysicsEngine::getPhysicsConnectionByName(const char* physicsConnectionName) const |
---|
| 201 | { |
---|
[5776] | 202 | list<PhysicsConnection*>::const_iterator pc; |
---|
| 203 | for (pc = this->connections.begin(); pc != this->connections.end(); pc++) |
---|
| 204 | if (!strcmp(physicsConnectionName, (*pc)->getName())) |
---|
| 205 | delete (*pc); |
---|
[4728] | 206 | return NULL; |
---|
| 207 | } |
---|
[4183] | 208 | |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | /** |
---|
[4836] | 212 | * Steps through all the Connections and Ticks them |
---|
| 213 | * @param dt The time Passed in Seconds |
---|
[4183] | 214 | |
---|
| 215 | This function brings a flow into the whole animation |
---|
| 216 | */ |
---|
| 217 | void PhysicsEngine::tick(float dt) |
---|
| 218 | { |
---|
[4558] | 219 | /* go through all the PhysicsInterface(s) and tick them, |
---|
| 220 | meaning let the fields work */ |
---|
[5776] | 221 | list<PhysicsConnection*>::iterator pc; |
---|
| 222 | for (pc = this->connections.begin(); pc != this->connections.end(); pc++) |
---|
| 223 | (*pc)->apply(); |
---|
[4558] | 224 | |
---|
| 225 | /* actually tick all the PhysicsInterfaces. Move the objects around */ |
---|
[5217] | 226 | if (this->interfaces != NULL || (this->interfaces = ClassList::getList(CL_PHYSICS_INTERFACE)) != NULL) |
---|
| 227 | { |
---|
[5779] | 228 | list<BaseObject*>::const_iterator tickPhys; |
---|
| 229 | for (tickPhys = this->interfaces->begin(); tickPhys != this->interfaces->end(); tickPhys++) |
---|
| 230 | dynamic_cast<PhysicsInterface*>(*tickPhys)->tickPhys(dt); |
---|
[5217] | 231 | } |
---|
[4183] | 232 | } |
---|
[4378] | 233 | |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | /** |
---|
[4836] | 237 | * print out interesting debug information of this class |
---|
[4378] | 238 | */ |
---|
[4746] | 239 | void PhysicsEngine::debug() const |
---|
[4378] | 240 | { |
---|
| 241 | PRINT(0)("====================================\n"); |
---|
| 242 | PRINT(0)("= Physics-Engine debug information =\n"); |
---|
| 243 | PRINT(0)("====================================\n"); |
---|
| 244 | PRINT(0)(" reference: %p\n", this); |
---|
[5217] | 245 | if (this->interfaces != NULL) |
---|
[5779] | 246 | PRINT(0)(" number of Interfaces: %d\n", this->interfaces->size()); |
---|
[5776] | 247 | PRINT(0)(" number of Fields: %d\n", this->fields.size()); |
---|
| 248 | PRINT(0)(" number of Connections: %d\n", this->connections.size()); |
---|
[4378] | 249 | |
---|
| 250 | PRINT(0)("==============================PHYS==\n"); |
---|
| 251 | } |
---|