| 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 | |
|---|
| 23 | using namespace std; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * standard constructor |
|---|
| 28 | * @todo this constructor is not jet implemented - do it |
|---|
| 29 | */ |
|---|
| 30 | CollisionHandle::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 | */ |
|---|
| 42 | CollisionHandle::~CollisionHandle () |
|---|
| 43 | { |
|---|
| 44 | // delete what has to be deleted here |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * restores the CollisionHandle to its initial state |
|---|
| 49 | */ |
|---|
| 50 | void 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 | */ |
|---|
| 60 | void 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 | */ |
|---|
| 79 | Collision* 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 | */ |
|---|
| 100 | void CollisionHandle::registerCollisionEvent(CollisionEvent* collisionEvent) |
|---|
| 101 | { |
|---|
| 102 | // set the state to not dispatched |
|---|
| 103 | this->bDispatched = false; |
|---|
| 104 | |
|---|
| 105 | // first element only |
|---|
| 106 | Collision* c = this->registerCollision(collisionEvent->getEntityA(), collisionEvent->getEntityB()); |
|---|
| 107 | c->registerCollisionEvent(collisionEvent); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * flushes the collision list |
|---|
| 113 | */ |
|---|
| 114 | void CollisionHandle::flushCollisions() |
|---|
| 115 | { |
|---|
| 116 | this->collisionList.clear(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * handles the collisions and react according to algorithm |
|---|
| 122 | */ |
|---|
| 123 | void CollisionHandle::handleCollisions() |
|---|
| 124 | { |
|---|
| 125 | // collision reaction calculations (for every collision there will be a reaction) |
|---|
| 126 | vector<Collision*>::iterator it = this->collisionList.begin(); |
|---|
| 127 | for(; it < this->collisionList.end(); it++) |
|---|
| 128 | { |
|---|
| 129 | (*it)->handleCollisionEvents(); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | // now set state to dispatched |
|---|
| 133 | this->bDispatched = true; |
|---|
| 134 | this->bCollided = false; |
|---|
| 135 | this->flushCollisions(); |
|---|
| 136 | } |
|---|