| 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_DETECTION | 
|---|
| 17 |  | 
|---|
| 18 | #include "cd_engine.h" | 
|---|
| 19 | #include "obb_tree.h" | 
|---|
| 20 | #include "debug.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "class_list.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "model.h" | 
|---|
| 25 | #include "world_entity.h" | 
|---|
| 26 | #include "terrain.h" | 
|---|
| 27 | // #include "player.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include "spatial_separation.h" | 
|---|
| 30 | #include "quadtree.h" | 
|---|
| 31 | #include "quadtree_node.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "bsp_manager.h" | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | /** | 
|---|
| 39 |  *  standard constructor | 
|---|
| 40 |  */ | 
|---|
| 41 | CDEngine::CDEngine () | 
|---|
| 42 | { | 
|---|
| 43 |   this->setClassID(CL_CD_ENGINE, "CDEngine"); | 
|---|
| 44 |  | 
|---|
| 45 |   this->bAbordOnFirstCollision = false; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | /** | 
|---|
| 50 |  *  the singleton reference to this class | 
|---|
| 51 |  */ | 
|---|
| 52 | CDEngine* CDEngine::singletonRef = NULL; | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | /** | 
|---|
| 56 |  *  standard deconstructor | 
|---|
| 57 |  */ | 
|---|
| 58 | CDEngine::~CDEngine () | 
|---|
| 59 | { | 
|---|
| 60 |   CDEngine::singletonRef = NULL; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | /** | 
|---|
| 65 |  * | 
|---|
| 66 |  */ | 
|---|
| 67 | void CDEngine::checkCollisions(ObjectManager::EntityList& list1, ObjectManager::EntityList& list2) | 
|---|
| 68 | { | 
|---|
| 69 |   BVTree* tree; | 
|---|
| 70 |   ObjectManager::EntityList::iterator entity1, entity2, pre1, pre2; | 
|---|
| 71 |   PRINTF(5)("checking for collisions\n"); | 
|---|
| 72 |  | 
|---|
| 73 |   pre1 = list1.begin(); | 
|---|
| 74 |   while (pre1 != list1.end()) | 
|---|
| 75 |   { | 
|---|
| 76 |     entity1 = pre1++; | 
|---|
| 77 |     if( likely((*entity1) != this->terrain)) | 
|---|
| 78 |     { | 
|---|
| 79 |       pre2 = list2.begin(); | 
|---|
| 80 |       while (pre2 != list2.end()) | 
|---|
| 81 |       { | 
|---|
| 82 |         entity2 = pre2++; | 
|---|
| 83 |         if( likely((*entity2) != this->terrain)) | 
|---|
| 84 |         { | 
|---|
| 85 |           PRINTF(4)("checking object %s against %s\n", (*entity1)->getName(), (*entity2)->getName()); | 
|---|
| 86 |           tree = (*entity1)->getOBBTree(); | 
|---|
| 87 |           if( likely(tree != NULL) && (*entity2)->getOBBTree() != NULL) | 
|---|
| 88 |             tree->collideWith(*entity1, *entity2); | 
|---|
| 89 |         } | 
|---|
| 90 |       } | 
|---|
| 91 |     } | 
|---|
| 92 |   } | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 | /** | 
|---|
| 97 |  *  this checks the collisions with the ground | 
|---|
| 98 |  */ | 
|---|
| 99 | void CDEngine::checkCollisionGround(std::list<WorldEntity*>& list1) | 
|---|
| 100 | { | 
|---|
| 101 |   std::list<BaseObject*>::const_iterator bspIterator; | 
|---|
| 102 |   std::list<WorldEntity*>::iterator entityIterator; | 
|---|
| 103 |   const std::list<BaseObject*>* bspList = ClassList::getList(CL_BSP_MODEL); | 
|---|
| 104 |   if( bspList == NULL) | 
|---|
| 105 |     return; | 
|---|
| 106 |  | 
|---|
| 107 |   // for all bsp managers check all entities | 
|---|
| 108 |   for( bspIterator = bspList->begin(); bspIterator != bspList->end(); bspIterator++) { | 
|---|
| 109 |       for(entityIterator = list1.begin(); entityIterator != list1.end(); entityIterator++) | 
|---|
| 110 |         static_cast<BspManager*>(*bspIterator)->checkCollision(*entityIterator); | 
|---|
| 111 |   } | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | /** | 
|---|
| 116 |  * some debug output on the class | 
|---|
| 117 |  */ | 
|---|
| 118 | void CDEngine::debug() | 
|---|
| 119 | { | 
|---|
| 120 |   PRINT(0)("\n=============================| CDEngine::debug() |===\n"); | 
|---|
| 121 |   PRINT(0)("=  CDEngine: Spawning Tree Start\n"); | 
|---|
| 122 |   //this->rootTree->debug(); | 
|---|
| 123 |   PRINT(0)("=  CDEngine: Spawning Tree: Finished\n"); | 
|---|
| 124 |   PRINT(0)("=======================================================\n"); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | /** | 
|---|
| 129 |  * this spawns a tree for debug purposes only | 
|---|
| 130 |  */ | 
|---|
| 131 | void CDEngine::debugSpawnTree(int depth, sVec3D* vertices, int numVertices) | 
|---|
| 132 | { | 
|---|
| 133 | //   if ( this->rootTree == NULL) | 
|---|
| 134 | //     this->rootTree = new OBBTree(depth, vertices, numVertices); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 | void CDEngine::drawBV(const ObjectManager::EntityList& drawList, int level) const | 
|---|
| 139 | { | 
|---|
| 140 |   ObjectManager::EntityList::const_iterator entity; | 
|---|
| 141 |   for (entity = drawList.begin(); entity != drawList.end(); entity++) | 
|---|
| 142 |     (*entity)->drawBVTree(level, 226); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | /** | 
|---|
| 147 |  * this draws the debug spawn tree | 
|---|
| 148 |  */ | 
|---|
| 149 | void CDEngine::debugDraw(int depth, int drawMode) | 
|---|
| 150 | { | 
|---|
| 151 | //   if(this-> rootTree != NULL) | 
|---|
| 152 | //     this->rootTree->drawBV(depth, drawMode); | 
|---|
| 153 | } | 
|---|