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