| 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 | co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION | 
|---|
| 17 |  | 
|---|
| 18 | #include "cd_engine.h" | 
|---|
| 19 | #include "obb_tree.h" | 
|---|
| 20 | #include "debug.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "model.h" | 
|---|
| 23 | #include "world_entity.h" | 
|---|
| 24 | #include "terrain.h" | 
|---|
| 25 | // #include "player.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "spatial_separation.h" | 
|---|
| 28 | #include "quadtree.h" | 
|---|
| 29 | #include "quadtree_node.h" | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | using namespace std; | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | /** | 
|---|
| 37 | *  standard constructor | 
|---|
| 38 | */ | 
|---|
| 39 | CDEngine::CDEngine () | 
|---|
| 40 | { | 
|---|
| 41 | this->setClassID(CL_CD_ENGINE, "CDEngine"); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | /** | 
|---|
| 46 | *  the singleton reference to this class | 
|---|
| 47 | */ | 
|---|
| 48 | CDEngine* CDEngine::singletonRef = NULL; | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 | *  standard deconstructor | 
|---|
| 53 | */ | 
|---|
| 54 | CDEngine::~CDEngine () | 
|---|
| 55 | { | 
|---|
| 56 | CDEngine::singletonRef = NULL; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | /** | 
|---|
| 61 | *  this is the collision checking function | 
|---|
| 62 |  | 
|---|
| 63 | there are some speed improvements that can be done here. a rewrite of the list a would be appropriate to | 
|---|
| 64 | be able to enhance iteration speed. | 
|---|
| 65 | */ | 
|---|
| 66 | //void CDEngine::checkCollisions() | 
|---|
| 67 | //{ | 
|---|
| 68 | //  this->checkCollisionObjects(); | 
|---|
| 69 | //this->checkCollisionGround(); | 
|---|
| 70 | //} | 
|---|
| 71 |  | 
|---|
| 72 | /** | 
|---|
| 73 | *  this checks the collisions with the objects | 
|---|
| 74 | */ | 
|---|
| 75 | //void CDEngine::checkCollisionObjects() | 
|---|
| 76 | //{ | 
|---|
| 77 | //   BVTree* tree; | 
|---|
| 78 | //   tIterator<WorldEntity>* iterator1 = entityList->getIterator(); | 
|---|
| 79 | //   tIterator<WorldEntity>* iterator2 = entityList->getIterator(); | 
|---|
| 80 | //   WorldEntity* entity1 = iterator1->firstElement(); | 
|---|
| 81 | //   WorldEntity* entity2 = iterator2->iteratorElement(iterator1); | 
|---|
| 82 | //   PRINTF(3)("checking for collisions\n"); | 
|---|
| 83 | //   while( entity1 != NULL) | 
|---|
| 84 | //   { | 
|---|
| 85 | //     if( likely(entity1 != this->terrain)) | 
|---|
| 86 | //     { | 
|---|
| 87 | //       entity2 = iterator2->nextElement(); | 
|---|
| 88 | // | 
|---|
| 89 | //       while( entity2 != NULL) | 
|---|
| 90 | //       { | 
|---|
| 91 | //         if( likely(entity2 != this->terrain)) | 
|---|
| 92 | //         { | 
|---|
| 93 | //           PRINTF(4)("checking object %s against %s\n", entity1->getName(), entity2->getName()); | 
|---|
| 94 | //           tree = entity1->getOBBTree(); | 
|---|
| 95 | //           if( likely(tree != NULL) && entity2->getOBBTree() != NULL) tree->collideWith(entity1, entity2); | 
|---|
| 96 | //         } | 
|---|
| 97 | //         entity2 = iterator2->nextElement(); | 
|---|
| 98 | //       } | 
|---|
| 99 | //     } | 
|---|
| 100 | //     entity1 = iterator1->nextElement(); | 
|---|
| 101 | //     entity2 = iterator2->iteratorElement(iterator1); | 
|---|
| 102 | //     entity2 = iterator2->nextElement(); | 
|---|
| 103 | //   } | 
|---|
| 104 | //   delete iterator1; | 
|---|
| 105 | //   delete iterator2; | 
|---|
| 106 | //} | 
|---|
| 107 |  | 
|---|
| 108 | void CDEngine::checkCollisions(std::list<WorldEntity*>& list1, std::list<WorldEntity*>& list2) | 
|---|
| 109 | { | 
|---|
| 110 | BVTree* tree; | 
|---|
| 111 | std::list<WorldEntity*>::iterator entity1, entity2, pre1, pre2; | 
|---|
| 112 | PRINTF(3)("checking for collisions\n"); | 
|---|
| 113 |  | 
|---|
| 114 | pre1 = list1.begin(); | 
|---|
| 115 | while (pre1 != list1.end()) | 
|---|
| 116 | { | 
|---|
| 117 | entity1 = pre1++; | 
|---|
| 118 | if( likely((*entity1) != this->terrain)) | 
|---|
| 119 | { | 
|---|
| 120 | pre2 = list2.begin(); | 
|---|
| 121 | while (pre2 != list2.end()) | 
|---|
| 122 | { | 
|---|
| 123 | entity2 = pre2++; | 
|---|
| 124 | if( likely((*entity2) != this->terrain)) | 
|---|
| 125 | { | 
|---|
| 126 | PRINTF(4)("checking object %s against %s\n", (*entity1)->getName(), (*entity2)->getName()); | 
|---|
| 127 | tree = (*entity1)->getOBBTree(); | 
|---|
| 128 | if( likely(tree != NULL) && (*entity2)->getOBBTree() != NULL) tree->collideWith(*entity1, *entity2); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | /** | 
|---|
| 137 | *  this checks the collisions with the ground | 
|---|
| 138 | */ | 
|---|
| 139 | void CDEngine::checkCollisionGround() | 
|---|
| 140 | { | 
|---|
| 141 | if( likely( this->terrain != NULL)) | 
|---|
| 142 | { | 
|---|
| 143 | Quadtree* q = dynamic_cast<Terrain*>(this->terrain)->ssp->getQuadtree(); | 
|---|
| 144 |  | 
|---|
| 145 | //    QuadtreeNode* n = q->getQuadtreeFromPosition(this->player->getAbsCoor()); | 
|---|
| 146 | } | 
|---|
| 147 | //sTriangleExt* tri = q->getTriangleFromPosition(this->player->getAbsCoor()); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 |  | 
|---|
| 151 | /** | 
|---|
| 152 | *  this draws the bounding volume tree | 
|---|
| 153 | * @param depth until which depth to draw the tree | 
|---|
| 154 | * @param drawMode mod which states how to draw it | 
|---|
| 155 | */ | 
|---|
| 156 | void CDEngine::drawBV(int depth, int drawMode) const | 
|---|
| 157 | { | 
|---|
| 158 | /* this would operate on  worldList bases, for testing purposes, we only use one OBBTree */ | 
|---|
| 159 | //this->rootTree->drawBV(depth, drawMode); | 
|---|
| 160 | /// FIXME | 
|---|
| 161 | /*  tIterator<WorldEntity>* iterator = entityList->getIterator(); | 
|---|
| 162 | WorldEntity* entity = iterator->firstElement(); | 
|---|
| 163 | while( entity != NULL) | 
|---|
| 164 | { | 
|---|
| 165 | entity->drawBVTree(depth, drawMode); | 
|---|
| 166 | entity = iterator->nextElement(); | 
|---|
| 167 | } | 
|---|
| 168 | delete iterator;*/ | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 | /** | 
|---|
| 173 | * some debug output on the class | 
|---|
| 174 | */ | 
|---|
| 175 | void CDEngine::debug() | 
|---|
| 176 | { | 
|---|
| 177 | PRINT(0)("\n=============================| CDEngine::debug() |===\n"); | 
|---|
| 178 | PRINT(0)("=  CDEngine: Spawning Tree Start\n"); | 
|---|
| 179 | //this->rootTree->debug(); | 
|---|
| 180 | PRINT(0)("=  CDEngine: Spawning Tree: Finished\n"); | 
|---|
| 181 | PRINT(0)("=======================================================\n"); | 
|---|
| 182 |  | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 |  | 
|---|
| 186 | /** | 
|---|
| 187 | * this spawns a tree for debug purposes only | 
|---|
| 188 | */ | 
|---|
| 189 | void CDEngine::debugSpawnTree(int depth, sVec3D* vertices, int numVertices) | 
|---|
| 190 | { | 
|---|
| 191 | if ( this->rootTree == NULL) | 
|---|
| 192 | this->rootTree = new OBBTree(); | 
|---|
| 193 | this->rootTree->spawnBVTree(depth, vertices, numVertices); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 | void CDEngine::drawBV(const std::list<WorldEntity*>& drawList ) const | 
|---|
| 198 | { | 
|---|
| 199 | std::list<WorldEntity*>::const_iterator entity; | 
|---|
| 200 | for (entity = drawList.begin(); entity != drawList.end(); entity++) | 
|---|
| 201 | if ((*entity)->isVisible()) | 
|---|
| 202 | (*entity)->drawBVTree(3, 226); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | /** | 
|---|
| 206 | * this draws the debug spawn tree | 
|---|
| 207 | */ | 
|---|
| 208 | void CDEngine::debugDraw(int depth, int drawMode) | 
|---|
| 209 | { | 
|---|
| 210 | if(this-> rootTree != NULL) | 
|---|
| 211 | this->rootTree->drawBV(depth, drawMode); | 
|---|
| 212 | } | 
|---|