Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/collision_detection/cd_engine.cc @ 7913

Last change on this file since 7913 was 7833, checked in by bottac, 19 years ago

collision detection.

File size: 5.5 KB
Line 
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#include "bsp_manager.h"
32
33using namespace std;
34
35
36/**
37 *  standard constructor
38 */
39CDEngine::CDEngine ()
40{
41  this->setClassID(CL_CD_ENGINE, "CDEngine");
42}
43
44
45/**
46 *  the singleton reference to this class
47 */
48CDEngine* CDEngine::singletonRef = NULL;
49
50
51/**
52 *  standard deconstructor
53 */
54CDEngine::~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
108void 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 */
139void CDEngine::checkCollisionGround(std::list<WorldEntity*>& list1)
140{
141  if( likely( this->terrain != NULL))
142  {
143    Quadtree* q = dynamic_cast<Terrain*>(this->terrain)->ssp->getQuadtree();
144//    QuadtreeNode* n = q->getQuadtreeFromPosition(this->player->getAbsCoor());
145  }
146 
147  if( likely( this->bspManager != NULL))
148  {
149    std::list<WorldEntity*>::iterator iterator;
150    PRINTF(3)("checking for collisions\n");
151
152    iterator = list1.begin();
153    while (iterator != list1.end())
154    {
155      bspManager->checkCollision(*iterator);
156      iterator++;
157    }
158  }
159}
160
161
162/**
163 *  this draws the bounding volume tree
164 * @param depth until which depth to draw the tree
165 * @param drawMode mod which states how to draw it
166 */
167void CDEngine::drawBV(int depth, int drawMode) const
168{
169  /* this would operate on  worldList bases, for testing purposes, we only use one OBBTree */
170  //this->rootTree->drawBV(depth, drawMode);
171  /// FIXME
172/*  tIterator<WorldEntity>* iterator = entityList->getIterator();
173  WorldEntity* entity = iterator->firstElement();
174  while( entity != NULL)
175  {
176    entity->drawBVTree(depth, drawMode);
177    entity = iterator->nextElement();
178  }
179  delete iterator;*/
180}
181
182
183/**
184 * some debug output on the class
185 */
186void CDEngine::debug()
187{
188  PRINT(0)("\n=============================| CDEngine::debug() |===\n");
189  PRINT(0)("=  CDEngine: Spawning Tree Start\n");
190  //this->rootTree->debug();
191  PRINT(0)("=  CDEngine: Spawning Tree: Finished\n");
192  PRINT(0)("=======================================================\n");
193
194}
195
196
197/**
198 * this spawns a tree for debug purposes only
199 */
200void CDEngine::debugSpawnTree(int depth, sVec3D* vertices, int numVertices)
201{
202  if ( this->rootTree == NULL)
203    this->rootTree = new OBBTree();
204  this->rootTree->spawnBVTree(depth, vertices, numVertices);
205}
206
207
208void CDEngine::drawBV(const std::list<WorldEntity*>& drawList ) const
209{
210  std::list<WorldEntity*>::const_iterator entity;
211  for (entity = drawList.begin(); entity != drawList.end(); entity++)
212    if ((*entity)->isVisible())
213      (*entity)->drawBVTree(3, 226);
214}
215
216/**
217 * this draws the debug spawn tree
218 */
219void CDEngine::debugDraw(int depth, int drawMode)
220{
221  if(this-> rootTree != NULL)
222    this->rootTree->drawBV(depth, drawMode);
223}
Note: See TracBrowser for help on using the repository browser.