Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/physics/physics_engine.cc @ 5516

Last change on this file since 5516 was 5217, checked in by bensch, 19 years ago

orxonox/trunk: physicsInterface fixed

File size: 7.3 KB
Line 
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
26using namespace std;
27
28
29/**
30 *  standard constructor
31*/
32PhysicsEngine::PhysicsEngine()
33{
34  this->setClassID(CL_PHYSICS_ENGINE, "PhysicsEngine");
35  this->setName("PhysicsEngine");
36  this->connections = new tList<PhysicsConnection>;
37  this->fields = new tList<Field>;
38  this->interfaces = NULL;
39}
40
41/**
42 *  the singleton reference to this class
43*/
44PhysicsEngine* PhysicsEngine::singletonRef = NULL;
45
46/**
47 *  standard deconstructor
48
49*/
50PhysicsEngine::~PhysicsEngine()
51{
52  // delete all PhysicsConnections that are still in existence
53  tIterator<PhysicsConnection>* itPC = this->connections->getIterator();
54  PhysicsConnection* enumPC = itPC->firstElement();
55  while (enumPC)
56  {
57    delete enumPC;
58    enumPC = itPC->nextElement();
59  }
60  delete itPC;
61  delete this->connections;
62//
63//   // delete all PhysicsInterfaces, still in existence (this could be dangerous)
64//   tIterator<PhysicsInterface>* itPI = this->interfaces->getIterator();
65//   PhysicsInterface* enumPI = itPI->firstElement();
66//   while (enumPI)
67//   {
68//     delete enumPI;
69//
70//     enumPI = itPI->nextElement();
71//   }
72//   delete itPI;
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 */
93void 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 */
105void 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 */
121void 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* @param physicsInterfaceName the Name of the PhysicsInterface to search for
136  @returns the PhysicsInterface if found, or NULL if not
137 */
138PhysicsInterface* PhysicsEngine::getPhysicsInterfaceByName(const char* physicsInterfaceName) const
139{
140  tIterator<BaseObject>* tmpIt = ClassList::getList(CL_PHYSICS_INTERFACE)->getIterator();
141  BaseObject* tmpInt = tmpIt->firstElement();
142  while(tmpInt)
143  {
144    if (!strcmp(physicsInterfaceName, tmpInt->getName()))
145    {
146      delete tmpIt;
147      return dynamic_cast<PhysicsInterface*>(tmpInt);
148    }
149    tmpInt = tmpIt->nextElement();
150  }
151  delete tmpIt;
152  return NULL;
153}
154
155/**
156 *  adds a Field to the list of handeled fields
157 * @param field the field to add
158
159   this is normally done in the constructor of any Field
160*/
161void PhysicsEngine::addField(Field* field)
162{
163  this->fields->add(field);
164}
165
166/**
167 *  removes a Field from the list of handeled fields
168 * @param field the field to remove
169
170   this is normally done in the destructor of any Field
171*/
172void PhysicsEngine::removeField(Field* field)
173{
174  this->fields->remove(field);
175}
176
177/**
178* @param FieldName the Name of the PhysicsInterface to search for
179  @returns the Field if found, or NULL if not
180 */
181Field* PhysicsEngine::getFieldByName(const char* FieldName) const
182{
183  tIterator<Field>* tmpIt = fields->getIterator();
184  Field* tmpField = tmpIt->firstElement();
185  while(tmpField)
186  {
187    if (!strcmp(FieldName, tmpField->getName()))
188    {
189      delete tmpIt;
190      return tmpField;
191    }
192    tmpField = tmpIt->nextElement();
193  }
194  delete tmpIt;
195  return NULL;
196}
197
198
199
200/**
201 *  adds A Physical Connection to the List of Connections
202 * @param connection the Connection to add
203
204   Usually this is done through the constructor of PhysicshConnections
205*/
206void PhysicsEngine::addConnection(PhysicsConnection* connection)
207{
208  this->connections->add(connection);
209}
210
211/**
212 *  removes A Physical Connection from the List of Connections
213 * @param connection the Connection to remove
214
215   Usually this is done through the destructor of PhysicsConnections
216*/
217void PhysicsEngine::removeConnection(PhysicsConnection* connection)
218{
219  this->connections->remove(connection);
220}
221
222/**
223* @param physicsConnectionName the Name of the PhysicsInterface to search for
224  @returns the PhysicsConnection if found, or NULL if not
225 */
226PhysicsConnection* PhysicsEngine::getPhysicsConnectionByName(const char* physicsConnectionName) const
227{
228  tIterator<PhysicsConnection>* tmpIt = connections->getIterator();
229  PhysicsConnection* tmpConn = tmpIt->firstElement();
230  while(tmpConn)
231  {
232    if (!strcmp(physicsConnectionName, tmpConn->getName()))
233    {
234      delete tmpIt;
235      return tmpConn;
236    }
237    tmpConn = tmpIt->nextElement();
238  }
239  delete tmpIt;
240  return NULL;
241}
242
243
244
245/**
246 *  Steps through all the Connections and Ticks them
247 * @param dt The time Passed in Seconds
248
249   This function brings a flow into the whole animation
250*/
251void PhysicsEngine::tick(float dt)
252{
253  /* go through all the PhysicsInterface(s) and tick them,
254  meaning let the fields work */
255  tIterator<PhysicsConnection>* itPC = this->connections->getIterator();
256  PhysicsConnection* enumPC = itPC->firstElement();
257  while (enumPC)
258    {
259      enumPC->apply();
260
261      enumPC = itPC->nextElement();
262    }
263  delete itPC;
264
265  /* actually tick all the PhysicsInterfaces. Move the objects around */
266  if (this->interfaces != NULL || (this->interfaces = ClassList::getList(CL_PHYSICS_INTERFACE)) != NULL)
267  {
268    tIterator<BaseObject>* itPI = this->interfaces->getIterator();
269    PhysicsInterface* enumPI = dynamic_cast<PhysicsInterface*>(itPI->firstElement());
270    while (enumPI)
271    {
272       enumPI->tickPhys(dt);
273
274      enumPI = dynamic_cast<PhysicsInterface*>(itPI->nextElement());
275    }
276   delete itPI;
277  }
278}
279
280
281
282/**
283 *  print out interesting debug information of this class
284*/
285void PhysicsEngine::debug() const
286{
287  PRINT(0)("====================================\n");
288  PRINT(0)("= Physics-Engine debug information =\n");
289  PRINT(0)("====================================\n");
290  PRINT(0)(" reference: %p\n", this);
291  if (this->interfaces != NULL)
292    PRINT(0)(" number of Interfaces: %d\n", this->interfaces->getSize());
293  PRINT(0)(" number of Fields: %d\n", this->fields->getSize());
294  PRINT(0)(" number of Connections: %d\n", this->connections->getSize());
295
296  PRINT(0)("==============================PHYS==\n");
297}
Note: See TracBrowser for help on using the repository browser.