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