Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8089 was 8043, checked in by patrick, 19 years ago

cr: more reaction framework

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