Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: saver removal

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  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 */
86void 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 */
98void 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 */
114void 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 */
131PhysicsInterface* PhysicsEngine::getPhysicsInterfaceByName(const char* physicsInterfaceName) const
132{
133  tIterator<BaseObject>* tmpIt = ClassList::getList(CL_PHYSICS_INTERFACE)->getIterator();
134  BaseObject* tmpInt = tmpIt->firstElement();
135  while(tmpInt)
136  {
137    if (!strcmp(physicsInterfaceName, tmpInt->getName()))
138    {
139      delete tmpIt;
140      return dynamic_cast<PhysicsInterface*>(tmpInt);
141    }
142    tmpInt = tmpIt->nextElement();
143  }
144  delete tmpIt;
145  return NULL;
146}
147
148/**
149 *  adds a Field to the list of handeled fields
150 * @param field the field to add
151
152   this is normally done in the constructor of any Field
153*/
154void PhysicsEngine::addField(Field* field)
155{
156  this->fields.push_back(field);
157}
158
159/**
160 *  removes a Field from the list of handeled fields
161 * @param field the field to remove
162
163   this is normally done in the destructor of any Field
164*/
165void PhysicsEngine::removeField(Field* field)
166{
167  this->fields.remove(field);
168}
169
170/**
171* @param FieldName the Name of the PhysicsInterface to search for
172  @returns the Field if found, or NULL if not
173 */
174Field* PhysicsEngine::getFieldByName(const char* FieldName) const
175{
176  list<Field*>::const_iterator field;
177  for (field = this->fields.begin(); field != this->fields.end(); field++)
178    if (!strcmp(FieldName, (*field)->getName()))
179      return (*field);
180  return NULL;
181}
182
183
184
185/**
186 *  adds A Physical Connection to the List of Connections
187 * @param connection the Connection to add
188
189   Usually this is done through the constructor of PhysicshConnections
190*/
191void PhysicsEngine::addConnection(PhysicsConnection* connection)
192{
193  this->connections.push_back(connection);
194}
195
196/**
197 *  removes A Physical Connection from the List of Connections
198 * @param connection the Connection to remove
199
200   Usually this is done through the destructor of PhysicsConnections
201*/
202void PhysicsEngine::removeConnection(PhysicsConnection* connection)
203{
204  this->connections.remove(connection);
205}
206
207/**
208* @param physicsConnectionName the Name of the PhysicsInterface to search for
209  @returns the PhysicsConnection if found, or NULL if not
210 */
211PhysicsConnection* PhysicsEngine::getPhysicsConnectionByName(const char* physicsConnectionName) const
212{
213  list<PhysicsConnection*>::const_iterator pc;
214  for (pc = this->connections.begin(); pc != this->connections.end(); pc++)
215    if (!strcmp(physicsConnectionName, (*pc)->getName()))
216      delete (*pc);
217  return NULL;
218}
219
220
221
222/**
223 *  Steps through all the Connections and Ticks them
224 * @param dt The time Passed in Seconds
225
226   This function brings a flow into the whole animation
227*/
228void PhysicsEngine::tick(float dt)
229{
230  /* go through all the PhysicsInterface(s) and tick them,
231  meaning let the fields work */
232  list<PhysicsConnection*>::iterator pc;
233  for (pc = this->connections.begin(); pc != this->connections.end(); pc++)
234    (*pc)->apply();
235
236  /* actually tick all the PhysicsInterfaces. Move the objects around */
237  if (this->interfaces != NULL || (this->interfaces = ClassList::getList(CL_PHYSICS_INTERFACE)) != NULL)
238  {
239    tIterator<BaseObject>* itPI = this->interfaces->getIterator();
240    PhysicsInterface* enumPI = dynamic_cast<PhysicsInterface*>(itPI->firstElement());
241    while (enumPI)
242    {
243       enumPI->tickPhys(dt);
244
245      enumPI = dynamic_cast<PhysicsInterface*>(itPI->nextElement());
246    }
247   delete itPI;
248  }
249}
250
251
252
253/**
254 *  print out interesting debug information of this class
255*/
256void PhysicsEngine::debug() const
257{
258  PRINT(0)("====================================\n");
259  PRINT(0)("= Physics-Engine debug information =\n");
260  PRINT(0)("====================================\n");
261  PRINT(0)(" reference: %p\n", this);
262  if (this->interfaces != NULL)
263    PRINT(0)(" number of Interfaces: %d\n", this->interfaces->getSize());
264  PRINT(0)(" number of Fields: %d\n", this->fields.size());
265  PRINT(0)(" number of Connections: %d\n", this->connections.size());
266
267  PRINT(0)("==============================PHYS==\n");
268}
Note: See TracBrowser for help on using the repository browser.