Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/physics/physics_engine.cc @ 9715

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

renamed newclassid to classid and newobjectlist to objectlist

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