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