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