[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 | |
---|
[4381] | 20 | #include "debug.h" |
---|
| 21 | |
---|
[4183] | 22 | #include "list.h" |
---|
[4728] | 23 | #include "tinyxml.h" |
---|
[4730] | 24 | #include "factory.h" |
---|
[4733] | 25 | #include "load_param.h" |
---|
[3954] | 26 | |
---|
| 27 | using namespace std; |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | /** |
---|
[4836] | 31 | * standard constructor |
---|
[3954] | 32 | */ |
---|
[4746] | 33 | PhysicsEngine::PhysicsEngine() |
---|
[3954] | 34 | { |
---|
[4597] | 35 | this->setClassID(CL_PHYSICS_ENGINE, "PhysicsEngine"); |
---|
| 36 | this->setName("PhysicsEngine"); |
---|
| 37 | this->connections = new tList<PhysicsConnection>; |
---|
| 38 | this->interfaces = new tList<PhysicsInterface>; |
---|
| 39 | this->fields = new tList<Field>; |
---|
[3954] | 40 | } |
---|
| 41 | |
---|
[4183] | 42 | /** |
---|
[4836] | 43 | * the singleton reference to this class |
---|
[4183] | 44 | */ |
---|
| 45 | PhysicsEngine* PhysicsEngine::singletonRef = NULL; |
---|
[3954] | 46 | |
---|
| 47 | /** |
---|
[4836] | 48 | * standard deconstructor |
---|
[3954] | 49 | |
---|
| 50 | */ |
---|
[4746] | 51 | PhysicsEngine::~PhysicsEngine() |
---|
[3954] | 52 | { |
---|
[4749] | 53 | // delete all PhysicsConnections that are still in existence |
---|
[5111] | 54 | tIterator<PhysicsConnection>* itPC = this->connections->getIterator(); |
---|
[5115] | 55 | PhysicsConnection* enumPC = itPC->firstElement(); |
---|
[5111] | 56 | while (enumPC) |
---|
| 57 | { |
---|
| 58 | delete enumPC; |
---|
| 59 | enumPC = itPC->nextElement(); |
---|
| 60 | } |
---|
| 61 | delete itPC; |
---|
[5115] | 62 | delete this->connections; |
---|
| 63 | // |
---|
| 64 | // // delete all PhysicsInterfaces, still in existence (this could be dangerous) |
---|
| 65 | // tIterator<PhysicsInterface>* itPI = this->interfaces->getIterator(); |
---|
| 66 | // PhysicsInterface* enumPI = itPI->firstElement(); |
---|
| 67 | // while (enumPI) |
---|
| 68 | // { |
---|
| 69 | // delete enumPI; |
---|
| 70 | // |
---|
| 71 | // enumPI = itPI->nextElement(); |
---|
| 72 | // } |
---|
| 73 | // delete itPI; |
---|
| 74 | // delete this->interfaces; |
---|
| 75 | // |
---|
| 76 | // // delete all PhysicsFields, still in existence (this could be dangerous) |
---|
| 77 | // tIterator<Field>* itF = this->fields->getIterator(); |
---|
| 78 | // Field* enumF = itF->firstElement(); |
---|
| 79 | // while (enumF) |
---|
| 80 | // { |
---|
| 81 | // delete enumF; |
---|
| 82 | // |
---|
| 83 | // enumF = itF->nextElement(); |
---|
| 84 | // } |
---|
| 85 | // delete itF; |
---|
| 86 | delete this->fields; |
---|
[4749] | 87 | |
---|
| 88 | |
---|
[4183] | 89 | PhysicsEngine::singletonRef = NULL; |
---|
[3954] | 90 | } |
---|
| 91 | |
---|
[4728] | 92 | /** |
---|
[4836] | 93 | * @param root the XML-element to load settings from |
---|
[4728] | 94 | */ |
---|
[4730] | 95 | void PhysicsEngine::loadParams(const TiXmlElement* root) |
---|
[4728] | 96 | { |
---|
[4733] | 97 | LoadParam<PhysicsEngine>(root, "Fields", this, &PhysicsEngine::loadFields) |
---|
| 98 | .describe("loads a list of fields"); |
---|
[4392] | 99 | |
---|
[4733] | 100 | LoadParam<PhysicsEngine>(root, "Connections", this, &PhysicsEngine::loadConnections) |
---|
| 101 | .describe("loads a list of fields"); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
[4836] | 105 | * @param root the XML-element to Load the PhysicsField from |
---|
[4733] | 106 | */ |
---|
| 107 | void PhysicsEngine::loadFields(const TiXmlElement* root) |
---|
| 108 | { |
---|
[4731] | 109 | PRINTF(4)("Loading Physical Fields\n"); |
---|
[4733] | 110 | |
---|
| 111 | const TiXmlElement* element = root->FirstChildElement(); |
---|
[4730] | 112 | while (element != NULL) |
---|
[4728] | 113 | { |
---|
[4731] | 114 | Factory::getFirst()->fabricate(element); |
---|
[4728] | 115 | |
---|
| 116 | element = element->NextSiblingElement(); |
---|
| 117 | } |
---|
[4733] | 118 | } |
---|
[4728] | 119 | |
---|
[4733] | 120 | /** |
---|
[4836] | 121 | * @param root the XML-element to load the PhysicsConnection from |
---|
[4733] | 122 | */ |
---|
| 123 | void PhysicsEngine::loadConnections(const TiXmlElement* root) |
---|
| 124 | { |
---|
[4731] | 125 | PRINTF(4)("Loading Physical Connections\n"); |
---|
[4733] | 126 | |
---|
| 127 | const TiXmlElement* element = root->FirstChildElement(); |
---|
[4730] | 128 | while (element != NULL) |
---|
[4728] | 129 | { |
---|
[4730] | 130 | Factory::getFirst()->fabricate(element); |
---|
[4728] | 131 | |
---|
| 132 | element = element->NextSiblingElement(); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
[3954] | 136 | /** |
---|
[4836] | 137 | * adds a PhysicsInterface to the list of handeled physicsInterfaces |
---|
| 138 | * @param physicsInterface the interface to add |
---|
[4392] | 139 | |
---|
| 140 | this is normally done in the constructor of any PhysicsInterface |
---|
| 141 | */ |
---|
| 142 | void PhysicsEngine::addPhysicsInterface(PhysicsInterface* physicsInterface) |
---|
| 143 | { |
---|
| 144 | this->interfaces->add(physicsInterface); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
[4836] | 148 | * removes a PhysicsInterface from the list of handeled physicsInterfaces |
---|
| 149 | * @param physicsInterface the interface to remove |
---|
[4392] | 150 | |
---|
| 151 | this is normally done in the destructor of any PhysicsInterface |
---|
| 152 | */ |
---|
| 153 | void PhysicsEngine::removePhysicsInterface(PhysicsInterface* physicsInterface) |
---|
| 154 | { |
---|
[4558] | 155 | this->interfaces->remove(physicsInterface); |
---|
[4392] | 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
[4836] | 159 | * @param physicsInterfaceName the Name of the PhysicsInterface to search for |
---|
| 160 | @returns the PhysicsInterface if found, or NULL if not |
---|
[4728] | 161 | */ |
---|
| 162 | PhysicsInterface* PhysicsEngine::getPhysicsInterfaceByName(const char* physicsInterfaceName) const |
---|
| 163 | { |
---|
| 164 | tIterator<PhysicsInterface>* tmpIt = interfaces->getIterator(); |
---|
[5115] | 165 | PhysicsInterface* tmpInt = tmpIt->firstElement(); |
---|
[4728] | 166 | while(tmpInt) |
---|
| 167 | { |
---|
| 168 | if (!strcmp(physicsInterfaceName, tmpInt->getName())) |
---|
| 169 | { |
---|
| 170 | delete tmpIt; |
---|
| 171 | return tmpInt; |
---|
| 172 | } |
---|
| 173 | tmpInt = tmpIt->nextElement(); |
---|
| 174 | } |
---|
| 175 | delete tmpIt; |
---|
| 176 | return NULL; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
[4836] | 180 | * adds a Field to the list of handeled fields |
---|
| 181 | * @param field the field to add |
---|
[4394] | 182 | |
---|
| 183 | this is normally done in the constructor of any Field |
---|
| 184 | */ |
---|
| 185 | void PhysicsEngine::addField(Field* field) |
---|
| 186 | { |
---|
| 187 | this->fields->add(field); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
[4836] | 191 | * removes a Field from the list of handeled fields |
---|
| 192 | * @param field the field to remove |
---|
[4394] | 193 | |
---|
| 194 | this is normally done in the destructor of any Field |
---|
| 195 | */ |
---|
| 196 | void PhysicsEngine::removeField(Field* field) |
---|
| 197 | { |
---|
| 198 | this->fields->remove(field); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
[4836] | 202 | * @param FieldName the Name of the PhysicsInterface to search for |
---|
| 203 | @returns the Field if found, or NULL if not |
---|
[4728] | 204 | */ |
---|
| 205 | Field* PhysicsEngine::getFieldByName(const char* FieldName) const |
---|
| 206 | { |
---|
| 207 | tIterator<Field>* tmpIt = fields->getIterator(); |
---|
[5115] | 208 | Field* tmpField = tmpIt->firstElement(); |
---|
[4728] | 209 | while(tmpField) |
---|
| 210 | { |
---|
| 211 | if (!strcmp(FieldName, tmpField->getName())) |
---|
| 212 | { |
---|
| 213 | delete tmpIt; |
---|
| 214 | return tmpField; |
---|
| 215 | } |
---|
| 216 | tmpField = tmpIt->nextElement(); |
---|
| 217 | } |
---|
| 218 | delete tmpIt; |
---|
| 219 | return NULL; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | /** |
---|
[4836] | 225 | * adds A Physical Connection to the List of Connections |
---|
| 226 | * @param connection the Connection to add |
---|
[4558] | 227 | |
---|
[4183] | 228 | Usually this is done through the constructor of PhysicshConnections |
---|
| 229 | */ |
---|
| 230 | void PhysicsEngine::addConnection(PhysicsConnection* connection) |
---|
| 231 | { |
---|
| 232 | this->connections->add(connection); |
---|
| 233 | } |
---|
[3954] | 234 | |
---|
[4183] | 235 | /** |
---|
[4836] | 236 | * removes A Physical Connection from the List of Connections |
---|
| 237 | * @param connection the Connection to remove |
---|
[4558] | 238 | |
---|
[4183] | 239 | Usually this is done through the destructor of PhysicsConnections |
---|
[3954] | 240 | */ |
---|
[4183] | 241 | void PhysicsEngine::removeConnection(PhysicsConnection* connection) |
---|
| 242 | { |
---|
| 243 | this->connections->remove(connection); |
---|
| 244 | } |
---|
| 245 | |
---|
[4728] | 246 | /** |
---|
[4836] | 247 | * @param physicsConnectionName the Name of the PhysicsInterface to search for |
---|
| 248 | @returns the PhysicsConnection if found, or NULL if not |
---|
[4728] | 249 | */ |
---|
| 250 | PhysicsConnection* PhysicsEngine::getPhysicsConnectionByName(const char* physicsConnectionName) const |
---|
| 251 | { |
---|
| 252 | tIterator<PhysicsConnection>* tmpIt = connections->getIterator(); |
---|
[5115] | 253 | PhysicsConnection* tmpConn = tmpIt->firstElement(); |
---|
[4728] | 254 | while(tmpConn) |
---|
| 255 | { |
---|
| 256 | if (!strcmp(physicsConnectionName, tmpConn->getName())) |
---|
| 257 | { |
---|
| 258 | delete tmpIt; |
---|
| 259 | return tmpConn; |
---|
| 260 | } |
---|
| 261 | tmpConn = tmpIt->nextElement(); |
---|
| 262 | } |
---|
| 263 | delete tmpIt; |
---|
| 264 | return NULL; |
---|
| 265 | } |
---|
[4183] | 266 | |
---|
| 267 | |
---|
| 268 | |
---|
| 269 | /** |
---|
[4836] | 270 | * Steps through all the Connections and Ticks them |
---|
| 271 | * @param dt The time Passed in Seconds |
---|
[4183] | 272 | |
---|
| 273 | This function brings a flow into the whole animation |
---|
| 274 | */ |
---|
| 275 | void PhysicsEngine::tick(float dt) |
---|
| 276 | { |
---|
[4558] | 277 | /* go through all the PhysicsInterface(s) and tick them, |
---|
| 278 | meaning let the fields work */ |
---|
[4396] | 279 | tIterator<PhysicsConnection>* itPC = this->connections->getIterator(); |
---|
[5115] | 280 | PhysicsConnection* enumPC = itPC->firstElement(); |
---|
[4396] | 281 | while (enumPC) |
---|
[4183] | 282 | { |
---|
[4396] | 283 | enumPC->apply(); |
---|
[4558] | 284 | |
---|
[4396] | 285 | enumPC = itPC->nextElement(); |
---|
[4183] | 286 | } |
---|
[4396] | 287 | delete itPC; |
---|
| 288 | |
---|
[4558] | 289 | /* actually tick all the PhysicsInterfaces. Move the objects around */ |
---|
[4396] | 290 | tIterator<PhysicsInterface>* itPI = this->interfaces->getIterator(); |
---|
[5115] | 291 | PhysicsInterface* enumPI = itPI->firstElement(); |
---|
[4396] | 292 | while (enumPI) |
---|
| 293 | { |
---|
| 294 | enumPI->tickPhys(dt); |
---|
[4558] | 295 | |
---|
[4396] | 296 | enumPI = itPI->nextElement(); |
---|
| 297 | } |
---|
| 298 | delete itPI; |
---|
[4183] | 299 | } |
---|
[4378] | 300 | |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | /** |
---|
[4836] | 304 | * print out interesting debug information of this class |
---|
[4378] | 305 | */ |
---|
[4746] | 306 | void PhysicsEngine::debug() const |
---|
[4378] | 307 | { |
---|
| 308 | PRINT(0)("====================================\n"); |
---|
| 309 | PRINT(0)("= Physics-Engine debug information =\n"); |
---|
| 310 | PRINT(0)("====================================\n"); |
---|
| 311 | PRINT(0)(" reference: %p\n", this); |
---|
[4392] | 312 | PRINT(0)(" number of Interfaces: %d\n", this->interfaces->getSize()); |
---|
[4730] | 313 | PRINT(0)(" number of Fields: %d\n", this->fields->getSize()); |
---|
[4392] | 314 | PRINT(0)(" number of Connections: %d\n", this->connections->getSize()); |
---|
[4378] | 315 | |
---|
| 316 | PRINT(0)("==============================PHYS==\n"); |
---|
| 317 | } |
---|