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