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