Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/collision_reaction/collision_handle.cc @ 8490

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

merged the bsp branche back to trunk

File size: 7.0 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*/
14
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION
16
17#include "collision_handle.h"
18
19#include "world_entity.h"
20
21#include "collision.h"
22#include "collision_event.h"
23#include "collision_reaction.h"
24
25#include "cr_object_damage.h"
26#include "cr_physics_ground_walk.h"
27
28#include "debug.h"
29
30using namespace std;
31
32
33/**
34 * standard constructor
35 * @todo this constructor is not jet implemented - do it
36*/
37CollisionHandle::CollisionHandle (WorldEntity* owner, CREngine::CRType type)
38{
39  this->setClassID(CL_COLLISION_HANDLE, "CollisionHandle");
40
41  this->owner = owner;
42  this->type = type;
43
44  this->bCollided = false;
45  this->bDispatched = true;
46
47  this->collisionReaction = NULL;
48  this->bContinuousPoll = false;
49  this->bStopOnFirstCollision = false;
50
51
52  switch( type)
53  {
54    case CREngine::CR_PHYSICS_STEP_BACK:
55//       this->collisionReaction = new CRPhysicsGroundWalk();
56      this->bContinuousPoll = true;
57      break;
58    case CREngine::CR_PHYSICS_GROUND_WALK:
59      this->collisionReaction = new CRPhysicsGroundWalk();
60      this->bContinuousPoll = true;
61      break;
62    case CREngine::CR_OBJECT_DAMAGE:
63      this->collisionReaction = new CRObjectDamage();
64      this->bStopOnFirstCollision = true;
65      break;
66    default:
67      break;
68  };
69}
70
71
72/**
73 * standard deconstructor
74*/
75CollisionHandle::~CollisionHandle ()
76{
77  // delete what has to be deleted here
78  if( this->collisionReaction != NULL)
79    delete this->collisionReaction;
80}
81
82/**
83 * restores the CollisionHandle to its initial state
84 */
85void CollisionHandle::reset()
86{
87  this->flushCollisions();
88}
89
90
91/**
92 * add more filter targets to this collision handle
93 *  @param classID the classid to look for
94 */
95void CollisionHandle::addTarget(long target)
96{
97  // make sure there is no dublicate
98  std::vector<long>::iterator it = this->targetList.begin();
99  for( ; it < this->targetList.end(); it++)
100    if( (*it) == target)
101      return;
102
103  // add element
104   PRINTF(0)("addTarget: %i \n", target);
105   this->targetList.push_back(target);
106}
107
108
109/**
110 * registers a new Collision Object
111 *  @param entityA WorldEntity A of the collision
112 *  @param entityB WorldEntity B of the collision
113 * if a there is already a collision object with the same stats
114 * registration will be skipped and the last collision object is returned
115 */
116Collision* CollisionHandle::registerCollision(WorldEntity* entityA, WorldEntity* entityB)
117{
118  //first get the collision object, multiple sources
119  Collision* c;
120  if( this->collisionList.empty() ||
121      ((this->collisionList.back())->getEntityA() != entityA && (this->collisionList.back())->getEntityB() != entityB )) {
122    c = CREngine::getInstance()->popCollisionObject();
123    c->collide(entityA, entityB);
124    this->collisionList.push_back(c);
125
126    // now register it as a shared collision with the other collision entity
127    CollisionHandle* ch = entityB->getCollisionHandle(this->type);
128    if( ch != NULL)
129      ch->registerSharedCollision(c);
130  }
131  else
132    c = this->collisionList.back();
133
134  return c;
135}
136
137
138/**
139 * register a Collision to the Collision handle.
140 *  @param collision the collision object to register
141 *
142 * This is used for internal collision registration: sharing the collision objects between Collision Reactions
143 * Therefore dispatching it only once
144 */
145void CollisionHandle::registerSharedCollision(Collision* collision)
146{
147  // fist check if we are listening for this Collision
148  if( !this->filterCollision(collision))
149    return;
150
151  // set the state to not dispatched
152  this->bDispatched = false;
153  this->bCollided = true;
154  collision->setEntityBCollide(true);
155
156  this->collisionList.push_back(collision);
157}
158
159
160/**
161 * this is the function to be called on a collision event for this handle
162 *  @param collision the collision objects containing all collision informations
163 */
164void CollisionHandle::registerCollisionEvent(CollisionEvent* collisionEvent)
165{
166  if( !this->filterCollisionEvent(collisionEvent))
167    return;
168
169  // set the state to not dispatched
170  this->bDispatched = false;
171  this->bCollided = true;
172
173  // checks if these WorldEntities have already collided or if its a new collision -> create a new Collision object
174 Collision* c = this->registerCollision(collisionEvent->getEntityA(), collisionEvent->getEntityB());
175 c->setEntityACollide(true);
176
177 c->registerCollisionEvent(collisionEvent);
178 PRINTF(0)("Registering Collision Event: %s, %s\n", collisionEvent->getEntityA()->getClassName(), collisionEvent->getEntityB()->getClassName());
179}
180
181
182/**
183 * flushes the collision list
184 */
185void CollisionHandle::flushCollisions()
186{
187  this->collisionList.clear();
188}
189
190
191/**
192 * handles the collisions and react according to algorithm
193 */
194void CollisionHandle::handleCollisions()
195{
196  // if continuous poll poll the reaction
197  if( this->bContinuousPoll && !this->bCollided)
198  {
199    this->collisionReaction->update(this->owner);
200    return;
201  }
202
203  // collision reaction calculations (for every collision there will be a reaction)
204  vector<Collision*>::iterator it = this->collisionList.begin();
205  for(; it < this->collisionList.end(); it++) {
206    if( !(*it)->isDispatched())
207    {
208      this->collisionReaction->reactToCollision(*it);
209      (*it)->flushCollisionEvents();
210    }
211  }
212
213  // now set state to dispatched
214  this->bDispatched = true;
215  this->bCollided = false;
216
217  this->flushCollisions();
218}
219
220
221/**
222 * filter out the CollisionEvents that are not wanted
223 *  @param collisionEvent the collision event to filter
224 */
225bool CollisionHandle::filterCollisionEvent(CollisionEvent* collisionEvent)
226{
227  vector<long>::iterator it = this->targetList.begin();
228  for(; it < this->targetList.end(); it++)
229  {
230    if( collisionEvent->getEntityA() == this->owner) {
231      if( collisionEvent->getEntityB()->isA((ClassID)(*it))) {
232        PRINTF(0)("I am: %s colliding with: %s is a %i filter ok\n", owner->getClassName(),
233                  collisionEvent->getEntityB()->getClassName(), *it);
234        return true; }
235    }
236    else {
237      if( collisionEvent->getEntityA()->isA((ClassID)(*it))) {
238        PRINTF(0)("I am: %s colliding with: %s is a %i filter ok\n", owner->getClassName(),
239                  collisionEvent->getEntityA()->getClassName(), *it);
240      return true; }
241    }
242  }
243
244  return false;
245}
246
247
248/**
249 * filter Collisions that are not wanted to be reacted to
250 *  @param collision the collision object to filter
251 */
252bool CollisionHandle::filterCollision(Collision* collision)
253{
254  vector<long>::iterator it = this->targetList.begin();
255  for(; it < this->targetList.end(); it++)
256  {
257    if( collision->getEntityA() == this->owner) {
258      if( collision->getEntityA()->isA((ClassID)(*it)))
259        return true; }
260      else {
261        if( collision->getEntityB()->isA((ClassID)(*it)))
262          return true; }
263  }
264
265  return false;
266}
267
268
269
270
271
272
273
Note: See TracBrowser for help on using the repository browser.