Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5134 was 5129, checked in by bensch, 20 years ago

orxonox/trunk: minor extension-addition to the Shell.

File size: 7.7 KB
RevLine 
[4558]1/*
[3954]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
[4381]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PHYSICS
[3954]17
[4121]18#include "physics_engine.h"
[3954]19
[4183]20#include "list.h"
[4728]21#include "tinyxml.h"
[4730]22#include "factory.h"
[4733]23#include "load_param.h"
[3954]24
25using namespace std;
26
27
28/**
[4836]29 *  standard constructor
[3954]30*/
[4746]31PhysicsEngine::PhysicsEngine()
[3954]32{
[4597]33  this->setClassID(CL_PHYSICS_ENGINE, "PhysicsEngine");
34  this->setName("PhysicsEngine");
35  this->connections = new tList<PhysicsConnection>;
36  this->interfaces = new tList<PhysicsInterface>;
37  this->fields = new tList<Field>;
[3954]38}
39
[4183]40/**
[4836]41 *  the singleton reference to this class
[4183]42*/
43PhysicsEngine* PhysicsEngine::singletonRef = NULL;
[3954]44
45/**
[4836]46 *  standard deconstructor
[3954]47
48*/
[4746]49PhysicsEngine::~PhysicsEngine()
[3954]50{
[4749]51  // delete all PhysicsConnections that are still in existence
[5111]52  tIterator<PhysicsConnection>* itPC = this->connections->getIterator();
[5115]53  PhysicsConnection* enumPC = itPC->firstElement();
[5111]54  while (enumPC)
55  {
56    delete enumPC;
57    enumPC = itPC->nextElement();
58  }
59  delete itPC;
[5115]60   delete this->connections;
61//
62//   // delete all PhysicsInterfaces, still in existence (this could be dangerous)
63//   tIterator<PhysicsInterface>* itPI = this->interfaces->getIterator();
64//   PhysicsInterface* enumPI = itPI->firstElement();
65//   while (enumPI)
66//   {
67//     delete enumPI;
68//
69//     enumPI = itPI->nextElement();
70//   }
71//   delete itPI;
72//   delete this->interfaces;
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;
[4749]85
86
[4183]87  PhysicsEngine::singletonRef = NULL;
[3954]88}
89
[4728]90/**
[4836]91* @param root the XML-element to load settings from
[4728]92 */
[4730]93void PhysicsEngine::loadParams(const TiXmlElement* root)
[4728]94{
[4733]95  LoadParam<PhysicsEngine>(root, "Fields", this, &PhysicsEngine::loadFields)
96      .describe("loads a list of fields");
[4392]97
[4733]98  LoadParam<PhysicsEngine>(root, "Connections", this, &PhysicsEngine::loadConnections)
99      .describe("loads a list of fields");
100}
101
102/**
[4836]103 * @param root the XML-element to Load the PhysicsField from
[4733]104 */
105void PhysicsEngine::loadFields(const TiXmlElement* root)
106{
[4731]107  PRINTF(4)("Loading Physical Fields\n");
[4733]108
109  const TiXmlElement* element = root->FirstChildElement();
[4730]110  while (element != NULL)
[4728]111  {
[4731]112    Factory::getFirst()->fabricate(element);
[4728]113
114    element = element->NextSiblingElement();
115  }
[4733]116}
[4728]117
[4733]118/**
[4836]119 * @param root the XML-element to load the PhysicsConnection from
[4733]120 */
121void PhysicsEngine::loadConnections(const TiXmlElement* root)
122{
[4731]123  PRINTF(4)("Loading Physical Connections\n");
[4733]124
125  const TiXmlElement* element = root->FirstChildElement();
[4730]126  while (element != NULL)
[4728]127  {
[4730]128    Factory::getFirst()->fabricate(element);
[4728]129
130    element = element->NextSiblingElement();
131  }
132}
133
[3954]134/**
[4836]135 *  adds a PhysicsInterface to the list of handeled physicsInterfaces
136 * @param physicsInterface the interface to add
[4392]137
138   this is normally done in the constructor of any PhysicsInterface
139*/
140void PhysicsEngine::addPhysicsInterface(PhysicsInterface* physicsInterface)
141{
142  this->interfaces->add(physicsInterface);
143}
144
145/**
[4836]146 *  removes a PhysicsInterface from the list of handeled physicsInterfaces
147 * @param physicsInterface the interface to remove
[4392]148
149   this is normally done in the destructor of any PhysicsInterface
150*/
151void PhysicsEngine::removePhysicsInterface(PhysicsInterface* physicsInterface)
152{
[4558]153  this->interfaces->remove(physicsInterface);
[4392]154}
155
156/**
[4836]157* @param physicsInterfaceName the Name of the PhysicsInterface to search for
158  @returns the PhysicsInterface if found, or NULL if not
[4728]159 */
160PhysicsInterface* PhysicsEngine::getPhysicsInterfaceByName(const char* physicsInterfaceName) const
161{
162  tIterator<PhysicsInterface>* tmpIt = interfaces->getIterator();
[5115]163  PhysicsInterface* tmpInt = tmpIt->firstElement();
[4728]164  while(tmpInt)
165  {
166    if (!strcmp(physicsInterfaceName, tmpInt->getName()))
167    {
168      delete tmpIt;
169      return tmpInt;
170    }
171    tmpInt = tmpIt->nextElement();
172  }
173  delete tmpIt;
174  return NULL;
175}
176
177/**
[4836]178 *  adds a Field to the list of handeled fields
179 * @param field the field to add
[4394]180
181   this is normally done in the constructor of any Field
182*/
183void PhysicsEngine::addField(Field* field)
184{
185  this->fields->add(field);
186}
187
188/**
[4836]189 *  removes a Field from the list of handeled fields
190 * @param field the field to remove
[4394]191
192   this is normally done in the destructor of any Field
193*/
194void PhysicsEngine::removeField(Field* field)
195{
196  this->fields->remove(field);
197}
198
199/**
[4836]200* @param FieldName the Name of the PhysicsInterface to search for
201  @returns the Field if found, or NULL if not
[4728]202 */
203Field* PhysicsEngine::getFieldByName(const char* FieldName) const
204{
205  tIterator<Field>* tmpIt = fields->getIterator();
[5115]206  Field* tmpField = tmpIt->firstElement();
[4728]207  while(tmpField)
208  {
209    if (!strcmp(FieldName, tmpField->getName()))
210    {
211      delete tmpIt;
212      return tmpField;
213    }
214    tmpField = tmpIt->nextElement();
215  }
216  delete tmpIt;
217  return NULL;
218}
219
220
221
222/**
[4836]223 *  adds A Physical Connection to the List of Connections
224 * @param connection the Connection to add
[4558]225
[4183]226   Usually this is done through the constructor of PhysicshConnections
227*/
228void PhysicsEngine::addConnection(PhysicsConnection* connection)
229{
230  this->connections->add(connection);
231}
[3954]232
[4183]233/**
[4836]234 *  removes A Physical Connection from the List of Connections
235 * @param connection the Connection to remove
[4558]236
[4183]237   Usually this is done through the destructor of PhysicsConnections
[3954]238*/
[4183]239void PhysicsEngine::removeConnection(PhysicsConnection* connection)
240{
241  this->connections->remove(connection);
242}
243
[4728]244/**
[4836]245* @param physicsConnectionName the Name of the PhysicsInterface to search for
246  @returns the PhysicsConnection if found, or NULL if not
[4728]247 */
248PhysicsConnection* PhysicsEngine::getPhysicsConnectionByName(const char* physicsConnectionName) const
249{
250  tIterator<PhysicsConnection>* tmpIt = connections->getIterator();
[5115]251  PhysicsConnection* tmpConn = tmpIt->firstElement();
[4728]252  while(tmpConn)
253  {
254    if (!strcmp(physicsConnectionName, tmpConn->getName()))
255    {
256      delete tmpIt;
257      return tmpConn;
258    }
259    tmpConn = tmpIt->nextElement();
260  }
261  delete tmpIt;
262  return NULL;
263}
[4183]264
265
266
267/**
[4836]268 *  Steps through all the Connections and Ticks them
269 * @param dt The time Passed in Seconds
[4183]270
271   This function brings a flow into the whole animation
272*/
273void PhysicsEngine::tick(float dt)
274{
[4558]275  /* go through all the PhysicsInterface(s) and tick them,
276  meaning let the fields work */
[4396]277  tIterator<PhysicsConnection>* itPC = this->connections->getIterator();
[5115]278  PhysicsConnection* enumPC = itPC->firstElement();
[4396]279  while (enumPC)
[4183]280    {
[4396]281      enumPC->apply();
[4558]282
[4396]283      enumPC = itPC->nextElement();
[4183]284    }
[4396]285  delete itPC;
286
[4558]287  /* actually tick all the PhysicsInterfaces. Move the objects around */
[4396]288  tIterator<PhysicsInterface>* itPI = this->interfaces->getIterator();
[5115]289  PhysicsInterface* enumPI = itPI->firstElement();
[4396]290  while (enumPI)
291    {
292      enumPI->tickPhys(dt);
[4558]293
[4396]294      enumPI = itPI->nextElement();
295    }
296  delete itPI;
[4183]297}
[4378]298
299
300
301/**
[4836]302 *  print out interesting debug information of this class
[4378]303*/
[4746]304void PhysicsEngine::debug() const
[4378]305{
306  PRINT(0)("====================================\n");
307  PRINT(0)("= Physics-Engine debug information =\n");
308  PRINT(0)("====================================\n");
309  PRINT(0)(" reference: %p\n", this);
[4392]310  PRINT(0)(" number of Interfaces: %d\n", this->interfaces->getSize());
[4730]311  PRINT(0)(" number of Fields: %d\n", this->fields->getSize());
[4392]312  PRINT(0)(" number of Connections: %d\n", this->connections->getSize());
[4378]313
314  PRINT(0)("==============================PHYS==\n");
315}
Note: See TracBrowser for help on using the repository browser.