Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cr/src/lib/collision_reaction/collision_handle.cc @ 8000

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

cr: collision/collision event creation and resycling works now

File size: 3.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*/
14
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION
16
17#include "collision_handle.h"
18
19
20#include "collision.h"
21#include "collision_event.h"
22
23using namespace std;
24
25
26/**
27 * standard constructor
28 * @todo this constructor is not jet implemented - do it
29*/
30CollisionHandle::CollisionHandle (WorldEntity* owner, CREngine::CRType type)
31{
32  this->setClassID(CL_COLLISION_HANDLE, "CollisionHandle");
33
34  this->owner = owner;
35  this->type = type;
36}
37
38
39/**
40 * standard deconstructor
41*/
42CollisionHandle::~CollisionHandle ()
43{
44  // delete what has to be deleted here
45}
46
47/**
48 * restores the CollisionHandle to its initial state
49 */
50void CollisionHandle::reset()
51{
52  this->flushCollisions();
53}
54
55
56/**
57 * add more filter targets to this collision handle
58 *  @param classID the classid to look for
59 */
60void CollisionHandle::addTarget(long target)
61{
62  // make sure there is no dublicate
63  std::vector<long>::iterator it = this->targetList.begin();
64  for( ; it < this->targetList.end(); it++)
65    if( (*it) == target)
66      return;
67
68  // add element
69   PRINTF(0)("addTarget: %i \n", target);
70   this->targetList.push_back(target);
71}
72
73
74/**
75 * registers a new Collision Object
76 * if a there is already a collision object with the same stats
77 * registration will be skipped and the last collision object is returned
78 */
79Collision* CollisionHandle::registerCollision(WorldEntity* entityA, WorldEntity* entityB)
80{
81  //first get the collision object, multiple sources
82  Collision* c;
83  if( this->collisionList.empty() ||
84      ((this->collisionList.back())->getEntityA() != entityA && (this->collisionList.back())->getEntityB() != entityB ))
85    c = CREngine::getInstance()->popCollisionObject();
86  else
87    c = this->collisionList.back();
88
89  c->collide(entityA, entityB);
90  this->collisionList.push_back(c);
91
92  return c;
93}
94
95
96/**
97 * this is the function to be called on a collision event for this handle
98 *  @param collision the collision objects containing all collision informations
99 */
100void CollisionHandle::registerCollisionEvent(CollisionEvent* collisionEvent)
101{
102  // first element only
103 Collision* c = this->registerCollision(collisionEvent->getEntityA(), collisionEvent->getEntityB());
104 c->registerCollisionEvent(collisionEvent);
105}
106
107
108/**
109 * flushes the collision list
110 */
111void CollisionHandle::flushCollisions()
112{
113  this->collisionList.clear();
114}
115
116
117/**
118 * handles the collisions and react according to algorithm
119 */
120void CollisionHandle::handleCollisions()
121{
122  // collision reaction calculations (for every collision there will be a reaction)
123  vector<Collision*>::iterator it = this->collisionList.begin();
124  for(; it < this->collisionList.end(); it++)
125  {
126    (*it)->handleCollisionEvents();
127  }
128
129  // now set state to dispatched
130  this->bDispatched = true;
131  this->bCollided = false;
132  this->flushCollisions();
133}
Note: See TracBrowser for help on using the repository browser.