Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/coll_rect/src/lib/collision_reaction/cr_engine.cc @ 9986

Last change on this file since 9986 was 9985, checked in by patrick, 18 years ago

giving the crengine the ability to check for collisions itself, collision tube only used as container

File size: 5.1 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: Patrick Boenzli
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION
17
18
19
20#include "collision.h"
21#include "collision_event.h"
22#include "collision_filter.h"
23#include "cr_defs.h"
24
25#include "cr_engine.h"
26
27#include "debug.h"
28
29namespace CoRe
30{
31
32  ObjectListDefinition(CREngine);
33
34
35  /**
36   * standard constructor
37   */
38  CREngine::CREngine ()
39      : BaseObject()
40  {
41    this->registerObject(this, CREngine::_objectList);
42    this->setName("CREngine");
43
44    this->init();
45  }
46
47  /**
48   *  the singleton reference to this class
49   */
50  CREngine* CREngine::singletonRef = NULL;
51
52  /**
53     @brief standard deconstructor
54   */
55  CREngine::~CREngine ()
56  {
57    CREngine::singletonRef = NULL;
58
59    if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
60      PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
61    if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
62      PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
63
64    this->reset();
65    this->flushCollisions();
66
67    CollisionList it1 = this->collisionsUnused.begin();
68    for(; it1 < this->collisionsUnused.end(); it1++)
69      delete *it1;
70    CollisionEventList it2 = this->collisionEventsUnused.begin();
71    for(; it2 < this->collisionEventsUnused.end(); it2++)
72      delete *it2;
73
74    this->collisionsUnused.clear();
75    this->collisionEventsUnused.clear();
76  }
77
78
79  /**
80   * inits the CREngine to a working state
81   */
82  void CREngine::init()
83  {
84    // precaching:
85    // create a list of Collisions and CollisionEvents for fast object recycling purposes
86    for( int i = 0; i < CR_MAX_COLLISIONS; i++)
87      this->collisionsUnused.push_back(new Collision());
88    for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
89      this->collisionEventsUnused.push_back(new CollisionEvent());
90
91
92    // push the collision reaction object on the list in the right order
93
94    // physical reactions
95    this->_reactionList[CREngine::CR_PHYSICS_MOMENTUM]      = NULL;
96    this->_reactionList[CREngine::CR_PHYSICS_STEP_BACK]     = NULL;
97    this->_reactionList[CREngine::CR_PHYSICS_GROUND_WALK]   = new CRPhysicsGroundWalk();
98    this->_reactionList[CREngine::CR_PHYSICS_FULL_WALK]     = new CRPhysicsFullWalk();
99    this->_reactionList[CREngine::CR_PHYSICS_DAMAGE]        = NULL;
100    // object based reactions
101    this->_reactionList[CREngine::CR_OBJECT_DAMAGE]         = new CRObjectDamage();
102    this->_reactionList[CREngine::CR_OBJECT_PICKUP]         = NULL;
103    // misc reactions
104    this->_reactionList[CREngine::CR_VERTEX_TRAFO]          = NULL;
105    this->_reactionList[CREngine::CR_SPECIAL_CALLBACK]      = NULL;
106  }
107
108
109  /**
110   * @returns an instance to a collision object. instead of creating new object this ones can be resycled
111   */
112  Collision* CREngine::popCollisionObject()
113  {
114    if( !this->collisionsUnused.empty())
115    {
116      this->collisionsUsed.push_back(this->collisionsUnused.back());
117      this->collisionsUnused.pop_back();
118      return this->collisionsUsed.back();
119    }
120    else return NULL;
121  }
122
123
124  /**
125   * @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled
126   */
127  CollisionEvent* CREngine::popCollisionEventObject()
128  {
129    if( !this->collisionEventsUnused.empty())
130    {
131      this->collisionEventsUsed.push_back(this->collisionEventsUnused.back());
132      this->collisionEventsUnused.pop_back();
133      return this->collisionEventsUsed.back();
134    }
135    else return NULL;
136  }
137
138
139  /**
140   * handles all collisions in registered in this tube
141   */
142  void CREngine::handleCollisions()
143  {
144    // for all collisions:
145    CollisionIterator ci = CollisionTube::getInstance()->begin();
146    for(; ci < CollisionTube::getInstance()->end(); ++ci)
147    {
148      for( int i = CREngine::CR_PHYSICS_MOMENTUM; i < CREngine::CR_NUBER; i++)
149      {
150        // check if entity A or B is subscibed for this event
151        if( (*ci)->getEntityA()->bReactibe((*it)->getEnityB(), i) || (*ci)->getEntityB()->bReactibe((*it)->getEnityA(), i))
152          (*ci)->reactToCollision(*ci);
153
154        (*ci)->reset();
155      }
156    }
157  }
158
159
160  /**
161   * flushes all the collision lists and puts them to their initial state
162   */
163  void CREngine::flushCollisions()
164  {
165    CollisionIterator it1 = this->collisionsUsed.begin();
166    for(; it1 < this->collisionsUsed.end(); it1++)
167      this->collisionsUnused.push_back(*it1);
168
169    CollisionEventIterator it2 = this->collisionEventsUsed.begin();
170    for(; it2 < this->collisionEventsUsed.end(); it2++)
171      this->collisionEventsUnused.push_back(*it2);
172
173    this->collisionsUsed.clear();
174    this->collisionEventsUsed.clear();
175  }
176
177
178  void CREngine::debug()
179  {
180  }
181
182}
Note: See TracBrowser for help on using the repository browser.