Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5776 was 5776, checked in by bensch, 18 years ago

orxonox/trunk: stl::list in PhysicsEngine

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