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