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