[7841] | 1 | /* |
---|
[8495] | 2 | orxonox - the future of 3D-vertical-scrollersf |
---|
[7841] | 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: |
---|
[7927] | 12 | main-programmer: Patrick Boenzli |
---|
[7841] | 13 | */ |
---|
| 14 | |
---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION |
---|
| 16 | |
---|
| 17 | #include "collision_handle.h" |
---|
| 18 | |
---|
[8190] | 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" |
---|
[8490] | 26 | #include "cr_physics_ground_walk.h" |
---|
[9235] | 27 | #include "cr_physics_full_walk.h" |
---|
[8190] | 28 | |
---|
[8362] | 29 | #include "debug.h" |
---|
| 30 | |
---|
[9889] | 31 | namespace CoRe |
---|
[7841] | 32 | { |
---|
| 33 | |
---|
[9889] | 34 | ObjectListDefinition(CollisionHandle); |
---|
[7927] | 35 | |
---|
[9889] | 36 | /** |
---|
| 37 | * standard constructor |
---|
| 38 | * @todo this constructor is not jet implemented - do it |
---|
| 39 | */ |
---|
[9892] | 40 | CollisionHandle::CollisionHandle (WorldEntity* owner, CREngine::ReactionType type) |
---|
[9889] | 41 | { |
---|
| 42 | this->registerObject(this, CollisionHandle::_objectList); |
---|
[8190] | 43 | |
---|
[9889] | 44 | this->owner = owner; |
---|
| 45 | this->type = type; |
---|
[8190] | 46 | |
---|
[9889] | 47 | this->bCollided = false; |
---|
| 48 | this->bDispatched = true; |
---|
[8190] | 49 | |
---|
[9889] | 50 | this->collisionReaction = NULL; |
---|
| 51 | this->bContinuousPoll = false; |
---|
| 52 | this->bStopOnFirstCollision = false; |
---|
[7841] | 53 | |
---|
| 54 | |
---|
[9889] | 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 | } |
---|
[8190] | 73 | |
---|
| 74 | |
---|
[9889] | 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 | } |
---|
[8190] | 84 | |
---|
[9889] | 85 | /** |
---|
| 86 | * restores the CollisionHandle to its initial state |
---|
| 87 | */ |
---|
| 88 | void CollisionHandle::reset() |
---|
| 89 | { |
---|
| 90 | this->flushCollisions(); |
---|
| 91 | } |
---|
[8190] | 92 | |
---|
[9869] | 93 | |
---|
[9889] | 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; |
---|
[8190] | 105 | |
---|
| 106 | |
---|
[9889] | 107 | // add element |
---|
| 108 | this->targetList.push_back(target); |
---|
| 109 | PRINTF(5)("addTarget: %i \n", target.id()); |
---|
[8190] | 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
[9889] | 113 | /** |
---|
| 114 | * handles the collisions and react according to algorithm |
---|
| 115 | */ |
---|
| 116 | void CollisionHandle::handleCollisions() |
---|
| 117 | { |
---|
[9890] | 118 | // if continuous poll the reaction |
---|
[9889] | 119 | if( this->bContinuousPoll && !this->bCollided) |
---|
[8190] | 120 | { |
---|
[9889] | 121 | this->collisionReaction->update(this->owner); |
---|
| 122 | return; |
---|
[8190] | 123 | } |
---|
| 124 | |
---|
[9889] | 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 | } |
---|
[8190] | 135 | |
---|
[9889] | 136 | // now set state to dispatched |
---|
| 137 | this->bDispatched = true; |
---|
| 138 | this->bCollided = false; |
---|
[8190] | 139 | |
---|
[9889] | 140 | this->flushCollisions(); |
---|
| 141 | } |
---|
[8190] | 142 | |
---|
[9889] | 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) |
---|
[8190] | 149 | { |
---|
[9889] | 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 | // } |
---|
[9235] | 174 | |
---|
[9889] | 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 | } |
---|
[8490] | 193 | } |
---|
[9889] | 194 | |
---|
| 195 | return false; |
---|
[8190] | 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
[9889] | 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) |
---|
[8190] | 204 | { |
---|
[9889] | 205 | std::vector<ClassID>::iterator it = this->targetList.begin(); |
---|
| 206 | for(; it < this->targetList.end(); it++) |
---|
| 207 | { |
---|
[9235] | 208 | |
---|
[9889] | 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 | // } |
---|
[9235] | 229 | |
---|
[9889] | 230 | if( collision->getEntityA() == this->owner) |
---|
| 231 | { |
---|
| 232 | if( collision->getEntityA()->isA(*it)) |
---|
| 233 | return true; |
---|
| 234 | } |
---|
| 235 | else |
---|
| 236 | { |
---|
[9869] | 237 | if( collision->getEntityB()->isA(*it)) |
---|
[9889] | 238 | return true; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | return false; |
---|
[8190] | 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | |
---|
[9889] | 247 | } |
---|
[8190] | 248 | |
---|
| 249 | |
---|
| 250 | |
---|