Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10000 was 10000, checked in by patrick, 17 years ago

this is a great moment: commit nr 10k! We will celebrate this on December the 20 with a very nice fondue! join us

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