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