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 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | |
---|
27 | /** |
---|
28 | * standard constructor |
---|
29 | * @todo this constructor is not jet implemented - do it |
---|
30 | */ |
---|
31 | CollisionHandle::CollisionHandle (WorldEntity* owner, CREngine::CRType type) |
---|
32 | { |
---|
33 | this->setClassID(CL_COLLISION_HANDLE, "CollisionHandle"); |
---|
34 | |
---|
35 | this->owner = owner; |
---|
36 | this->type = type; |
---|
37 | |
---|
38 | this->bCollided = false; |
---|
39 | this->bContinuousPoll = false; |
---|
40 | this->bDispatched = false; |
---|
41 | this->bStopOnFirstCollision = false; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | /** |
---|
46 | * standard deconstructor |
---|
47 | */ |
---|
48 | CollisionHandle::~CollisionHandle () |
---|
49 | { |
---|
50 | // delete what has to be deleted here |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * restores the CollisionHandle to its initial state |
---|
55 | */ |
---|
56 | void CollisionHandle::reset() |
---|
57 | { |
---|
58 | this->flushCollisions(); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | * add more filter targets to this collision handle |
---|
64 | * @param classID the classid to look for |
---|
65 | */ |
---|
66 | void CollisionHandle::addTarget(long target) |
---|
67 | { |
---|
68 | // make sure there is no dublicate |
---|
69 | std::vector<long>::iterator it = this->targetList.begin(); |
---|
70 | for( ; it < this->targetList.end(); it++) |
---|
71 | if( (*it) == target) |
---|
72 | return; |
---|
73 | |
---|
74 | // add element |
---|
75 | PRINTF(0)("addTarget: %i \n", target); |
---|
76 | this->targetList.push_back(target); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * registers a new Collision Object |
---|
82 | * if a there is already a collision object with the same stats |
---|
83 | * registration will be skipped and the last collision object is returned |
---|
84 | */ |
---|
85 | Collision* CollisionHandle::registerCollision(WorldEntity* entityA, WorldEntity* entityB) |
---|
86 | { |
---|
87 | //first get the collision object, multiple sources |
---|
88 | Collision* c; |
---|
89 | if( this->collisionList.empty() || |
---|
90 | ((this->collisionList.back())->getEntityA() != entityA && (this->collisionList.back())->getEntityB() != entityB )) |
---|
91 | c = CREngine::getInstance()->popCollisionObject(); |
---|
92 | else |
---|
93 | c = this->collisionList.back(); |
---|
94 | |
---|
95 | c->collide(entityA, entityB); |
---|
96 | c->setEntityACollide(true); |
---|
97 | this->collisionList.push_back(c); |
---|
98 | |
---|
99 | // now register it as a shared collision with the other collision entity |
---|
100 | CollisionHandle* ch = entityB->getCollisionHandle(this->type); |
---|
101 | if( ch != NULL) { |
---|
102 | ch->registerSharedCollision(c); |
---|
103 | c->setEntityBCollide(true); |
---|
104 | } |
---|
105 | |
---|
106 | return c; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * register a Collision to the Collision handle. |
---|
112 | * @param collision |
---|
113 | * This is used for internal collision registration: sharing the collision objects between Collision Reactions |
---|
114 | * Therefore dispatching it only once |
---|
115 | */ |
---|
116 | void CollisionHandle::registerSharedCollision(Collision* collision) |
---|
117 | { |
---|
118 | // set the state to not dispatched |
---|
119 | this->bDispatched = false; |
---|
120 | |
---|
121 | this->collisionList.push_back(collision); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * this is the function to be called on a collision event for this handle |
---|
127 | * @param collision the collision objects containing all collision informations |
---|
128 | */ |
---|
129 | void CollisionHandle::registerCollisionEvent(CollisionEvent* collisionEvent) |
---|
130 | { |
---|
131 | // set the state to not dispatched |
---|
132 | this->bDispatched = false; |
---|
133 | |
---|
134 | // first element only |
---|
135 | Collision* c = this->registerCollision(collisionEvent->getEntityA(), collisionEvent->getEntityB()); |
---|
136 | c->registerCollisionEvent(collisionEvent); |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | /** |
---|
142 | * flushes the collision list |
---|
143 | */ |
---|
144 | void CollisionHandle::flushCollisions() |
---|
145 | { |
---|
146 | this->collisionList.clear(); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | /** |
---|
151 | * handles the collisions and react according to algorithm |
---|
152 | */ |
---|
153 | void CollisionHandle::handleCollisions() |
---|
154 | { |
---|
155 | // collision reaction calculations (for every collision there will be a reaction) |
---|
156 | vector<Collision*>::iterator it = this->collisionList.begin(); |
---|
157 | for(; it < this->collisionList.end(); it++) |
---|
158 | { |
---|
159 | (*it)->handleCollisionEvents(); |
---|
160 | } |
---|
161 | |
---|
162 | // now set state to dispatched |
---|
163 | this->bDispatched = true; |
---|
164 | this->bCollided = false; |
---|
165 | this->flushCollisions(); |
---|
166 | } |
---|