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