Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/coll_rect/src/lib/collision_reaction/collision_handle.cc @ 9893

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

collision reactions now only registered with collision tube. no compile

File size: 7.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollersf
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#include "cr_physics_full_walk.h"
28
29#include "debug.h"
30
31namespace CoRe
32{
33
34  ObjectListDefinition(CollisionHandle);
35
36  /**
37   * standard constructor
38   * @todo this constructor is not jet implemented - do it
39  */
40  CollisionHandle::CollisionHandle (WorldEntity* owner, CREngine::ReactionType type)
41  {
42    this->registerObject(this, CollisionHandle::_objectList);
43
44    this->owner = owner;
45    this->type = type;
46
47    this->bCollided = false;
48    this->bDispatched = true;
49
50    this->collisionReaction = NULL;
51    this->bContinuousPoll = false;
52    this->bStopOnFirstCollision = false;
53
54
55    switch( type)
56    {
57        case CREngine::CR_PHYSICS_FULL_WALK:
58        this->collisionReaction = new CRPhysicsFullWalk();
59        this->bContinuousPoll = true;
60        break;
61        case CREngine::CR_PHYSICS_GROUND_WALK:
62        this->collisionReaction = new CRPhysicsGroundWalk();
63        this->bContinuousPoll = true;
64        break;
65        case CREngine::CR_OBJECT_DAMAGE:
66        this->collisionReaction = new CRObjectDamage();
67        this->bStopOnFirstCollision = true;
68        break;
69        default:
70        break;
71    };
72  }
73
74
75  /**
76   * standard deconstructor
77  */
78  CollisionHandle::~CollisionHandle ()
79  {
80    // delete what has to be deleted here
81    if( this->collisionReaction != NULL)
82      delete this->collisionReaction;
83  }
84
85  /**
86   * restores the CollisionHandle to its initial state
87   */
88  void CollisionHandle::reset()
89  {
90    this->flushCollisions();
91  }
92
93
94  /**
95   * add more filter targets to this collision handle
96   *  @param classID the classid to look for
97   */
98  void CollisionHandle::addTarget(const ClassID& target)
99  {
100    // make sure there is no dublicate
101    std::vector<ClassID>::iterator it = this->targetList.begin();
102    for( ; it < this->targetList.end(); it++)
103      if( (*it) == target)
104        return;
105
106
107    // add element
108    this->targetList.push_back(target);
109    PRINTF(5)("addTarget: %i \n", target.id());
110  }
111
112
113  /**
114   * handles the collisions and react according to algorithm
115   */
116  void CollisionHandle::handleCollisions()
117  {
118    // if continuous poll the reaction
119    if( this->bContinuousPoll && !this->bCollided)
120    {
121      this->collisionReaction->update(this->owner);
122      return;
123    }
124
125    // collision reaction calculations (for every collision there will be a reaction)
126    std::vector<Collision*>::iterator it = this->collisionList.begin();
127    for(; it < this->collisionList.end(); it++)
128    {
129      if( !(*it)->isDispatched())
130      {
131        this->collisionReaction->reactToCollision(*it);
132        (*it)->flushCollisionEvents();
133      }
134    }
135
136    // now set state to dispatched
137    this->bDispatched = true;
138    this->bCollided = false;
139
140    this->flushCollisions();
141  }
142
143
144  /**
145   * filter out the CollisionEvents that are not wanted
146   *  @param collisionEvent the collision event to filter
147   */
148  bool CollisionHandle::filterCollisionEvent(CollisionEvent* collisionEvent)
149  {
150    std::vector<ClassID>::iterator it = this->targetList.begin();
151    for(; it < this->targetList.end(); it++)
152    {
153      //     if(collisionEvent->getEntityB()->isA(CL_AIMING_SYSTEM) || collisionEvent->getEntityA()->isA(CL_AIMING_SYSTEM))
154      //     {
155      //        PRINTF(0)("I am: %s colliding with: %s\n", owner->getClassCName(), collisionEvent->getEntityB()->getClassCName(), *it);
156      //        if( collisionEvent->getEntityA() == this->owner) {
157      //          PRINTF(0)("I am owner -> I am: %s colliding with: %s is a %i filter?\n", owner->getClassCName(),
158      //          collisionEvent->getEntityB()->getClassCName(), *it);
159      //          if( collisionEvent->getEntityB()->isA((ClassID)(*it))) {
160      //            PRINTF(0)("I am owner -> I am: %s colliding with: %s is a %i filter ok\n", owner->getClassCName(),
161      //            collisionEvent->getEntityB()->getClassCName(), *it);
162      //             }
163      //        }
164      //        else {
165      //          PRINTF(0)("I am not owner -> I am: %s colliding with: %s is a %i filter?\n", owner->getClassCName(),
166      //          collisionEvent->getEntityB()->getClassCName(), *it);
167      //          if( collisionEvent->getEntityA()->isA((ClassID)(*it))) {
168      //            PRINTF(0)("I'm not owner -> I am: %s colliding with: %s is a %i filter ok\n", owner->getClassCName(),
169      //            collisionEvent->getEntityA()->getClassCName(), *it);
170      //             }
171      //        }
172      //
173      //     }
174
175      if( collisionEvent->getEntityA() == this->owner)
176      {
177        if( collisionEvent->getEntityB()->isA((*it)))
178        {
179          PRINTF(5)("I am: %s colliding with: %s is a %i filter ok\n", owner->getClassCName(),
180                    collisionEvent->getEntityB()->getClassCName(), (*it).id());
181          return true;
182        }
183      }
184      else
185      {
186        if( collisionEvent->getEntityA()->isA((*it)))
187        {
188          PRINTF(5)("I am: %s colliding with: %s is a %i filter ok\n", owner->getClassCName(),
189                    collisionEvent->getEntityA()->getClassCName(), (*it).id());
190          return true;
191        }
192      }
193    }
194
195    return false;
196  }
197
198
199  /**
200   * filter Collisions that are not wanted to be reacted to
201   *  @param collision the collision object to filter
202   */
203  bool CollisionHandle::filterCollision(Collision* collision)
204  {
205    std::vector<ClassID>::iterator it = this->targetList.begin();
206    for(; it < this->targetList.end(); it++)
207    {
208
209      //     if(collision->getEntityB()->isA(CL_AIMING_SYSTEM) || collision->getEntityA()->isA(CL_AIMING_SYSTEM))
210      //     {
211      //       PRINTF(0)("Shared!!! I am: %s colliding with: %s\n", owner->getClassCName(), collision->getEntityB()->getClassCName(), *it);
212      //       if( collision->getEntityA() == this->owner) {
213      //         PRINTF(0)("I am owner -> I am: %s colliding with: %s is a %i filter?\n", owner->getClassCName(),
214      //         collision->getEntityB()->getClassCName(), *it);
215      //         if( collision->getEntityB()->isA((ClassID)(*it))) {
216      //           PRINTF(0)("I am owner -> I am: %s colliding with: %s is a %i filter ok\n", owner->getClassCName(),
217      //           collision->getEntityB()->getClassCName(), *it);
218      //         }
219      //       }
220      //       else {
221      //         PRINTF(0)("I'm not owner -> I am: %s colliding with: %s is a %i filter?\n", owner->getClassCName(),
222      //         collision->getEntityB()->getClassCName(), *it);
223      //         if( collision->getEntityA()->isA((ClassID)(*it))) {
224      //           PRINTF(0)("I'm not owner -> I am: %s colliding with: %s is a %i filter ok\n", owner->getClassCName(),
225      //           collision->getEntityA()->getClassCName(), *it);
226      //         }
227      //       }
228      //     }
229
230      if( collision->getEntityA() == this->owner)
231      {
232        if( collision->getEntityA()->isA(*it))
233          return true;
234      }
235      else
236      {
237        if( collision->getEntityB()->isA(*it))
238          return true;
239      }
240    }
241
242    return false;
243  }
244
245
246
247}
248
249
250
Note: See TracBrowser for help on using the repository browser.