Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/lib/collision_detection/obb_tree_node.cc @ 5929

Last change on this file since 5929 was 5929, checked in by patrick, 18 years ago

collision_deteciton: some minor bug fixes. the eigenvc algorithm doesn't work yet. this realy sucks…

File size: 37.9 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_DETECTION
17
18#include "obb_tree_node.h"
19#include "obb_tree.h"
20#include "obb.h"
21
22#include "matrix.h"
23#include "abstract_model.h"
24#include "world_entity.h"
25
26#include "color.h"
27#include "glincl.h"
28
29#include <list>
30#include <vector>
31#include "debug.h"
32
33
34
35using namespace std;
36
37
38GLUquadricObj* OBBTreeNode_sphereObj = NULL;
39
40
41/**
42 *  standard constructor
43 * @param tree: reference to the obb tree
44 * @param depth: the depth of the obb tree to generate
45 */
46OBBTreeNode::OBBTreeNode (const OBBTree& tree, unsigned int depth)
47    : BVTreeNode()
48{
49  this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode");
50
51  this->obbTree = &tree;
52  this->depth = depth;
53
54  this->nodeLeft = NULL;
55  this->nodeRight = NULL;
56  this->bvElement = NULL;
57
58  this->triangleIndexList1 = NULL;
59  this->triangleIndexList2 = NULL;
60
61  this->modelInf = NULL;
62  this->triangleIndexes = NULL;
63
64  if( OBBTreeNode_sphereObj == NULL)
65    OBBTreeNode_sphereObj = gluNewQuadric();
66}
67
68
69/**
70 *  standard deconstructor
71 */
72OBBTreeNode::~OBBTreeNode ()
73{
74  if( this->nodeLeft)
75    delete this->nodeLeft;
76  if( this->nodeRight)
77    delete this->nodeRight;
78
79  if( this->bvElement)
80    delete this->bvElement;
81
82  if( this->triangleIndexList1 != NULL)
83    delete [] this->triangleIndexList1;
84  if( this->triangleIndexList2 != NULL)
85    delete [] this->triangleIndexList2;
86}
87
88
89/**
90 *  creates a new BVTree or BVTree partition
91 * @param depth: how much more depth-steps to go: if == 1 don't go any deeper!
92 * @param modInfo: model informations from the abstrac model
93 *
94 * this function creates the Bounding Volume tree from a modelInfo struct and bases its calculations
95 * on the triangle informations (triangle soup not polygon soup)
96 */
97void OBBTreeNode::spawnBVTree(const modelInfo& modelInf, const int* triangleIndexes, unsigned int length)
98{
99  PRINTF(3)("\n==============================Creating OBB Tree Node==================\n");
100  PRINT(3)(" OBB Tree Infos: \n");
101  PRINT(3)("\tDepth: %i \n\tTree Index: %i \n\tNumber of Triangles: %i\n", depth, treeIndex, length);
102  this->depth = depth;
103
104  this->bvElement = new OBB();
105  this->bvElement->modelInf = &modelInf;
106  this->bvElement->triangleIndexes = triangleIndexes;
107  this->bvElement->triangleIndexesLength = length;
108
109  /* create the boxes in three steps */
110  this->calculateBoxCovariance(*this->bvElement, modelInf, triangleIndexes, length);
111  this->calculateBoxEigenvectors(*this->bvElement, modelInf, triangleIndexes, length);
112  this->calculateBoxAxis(*this->bvElement, modelInf, triangleIndexes, length);
113
114  /* do we need to descent further in the obb tree?*/
115  if( likely( this->depth > 0))
116  {
117    this->forkBox(*this->bvElement);
118
119    if( this->triangleIndexLength1 >= 3)
120    {
121      this->nodeLeft = new OBBTreeNode(*this->obbTree, depth - 1);
122      this->nodeLeft->spawnBVTree(modelInf, this->triangleIndexList1, this->triangleIndexLength1);
123    }
124    if( this->triangleIndexLength2 >= 3)
125    {
126      this->nodeRight = new OBBTreeNode(*this->obbTree, depth - 1);
127      this->nodeRight->spawnBVTree(modelInf, this->triangleIndexList2, this->triangleIndexLength2);
128    }
129  }
130}
131
132
133
134/**
135 *  calculate the box covariance matrix
136 * @param box: reference to the box
137 * @param modelInf: the model info structure of the model
138 * @param tirangleIndexes: an array with the indexes of the triangles inside this
139 * @param length: the length of the indexes array
140 */
141void OBBTreeNode::calculateBoxCovariance(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, unsigned int length)
142{
143  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
144  float     face = 0.0f;                             //!< surface area of the entire convex hull
145  Vector    centroid[length];                        //!< centroid of the i'th convex hull
146  Vector    center;                                  //!< the center of the entire hull
147  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
148  Vector    t1, t2;                                  //!< temporary values
149  float     covariance[3][3] = {0,0,0, 0,0,0, 0,0,0};//!< the covariance matrix
150  sVec3D*   tmpVec = NULL;                           //!< a temp saving place for sVec3Ds
151
152
153  /* fist compute all the convex hull face/facelets and centroids */
154  for( int i = 0; i < length ; ++i)
155  {
156    tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]);
157    p = *tmpVec;
158    tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]);
159    q = *tmpVec;
160    tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]);
161    r = *tmpVec;
162
163    /* finding the facelet surface via cross-product */
164    t1 = p - q;
165    t2 = p - r;
166    facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
167    /* update the entire convex hull surface */
168    face += facelet[i];
169
170    /* calculate the cetroid of the hull triangles */
171    centroid[i] = (p + q + r) / 3.0f;
172    /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
173    center += centroid[i] * facelet[i];
174    /* the arithmetical center */
175  }
176  /* take the average of the centroid sum */
177  center /= face;
178
179
180  /* now calculate the covariance matrix - if not written in three for-loops,
181     it would compute faster: minor */
182  for( int j = 0; j < 3; ++j)
183  {
184    for( int k = 0; k < 3; ++k)
185    {
186      for( int i = 0; i < length; ++i)
187      {
188        tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]);
189        p = *tmpVec;
190        tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]);
191        q = *tmpVec;
192        tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]);
193        r = *tmpVec;
194
195        covariance[j][k] = facelet[i] * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] +
196                           q[j] * q[k] + r[j] * r[k]);
197      }
198      covariance[j][k] = covariance[j][k] / (12.0f * face) - center[j] * center[k];
199    }
200  }
201  for(int i = 0; i < 3; ++i)
202  {
203    box.covarianceMatrix[i][0] = covariance[i][0];
204    box.covarianceMatrix[i][1] = covariance[i][1];
205    box.covarianceMatrix[i][2] = covariance[i][2];
206  }
207  box.center = center;
208
209
210  std::vector<int>           vertIndexVector;                           //!< vertex indexes list
211  int                        vertIndex;                                 //!< index to vertex
212  bool                       vertexFound;                               //!< vertex found flag
213  Vector                     arithCenter;                               //!< aritmetical center
214
215  /* calculate the arithmetical center of the box */
216
217  /* go thourgh all vertices, add only the used vertices indexes */
218  for( int i = 0; i < length; ++i)
219  {
220    for(int j = 0; j < 3; ++j)
221    {
222      vertIndex = modelInf.pTriangles[triangleIndexes[i]].indexToVertices[j];
223
224      vertexFound = false;
225      for( int i = 0; i < vertIndexVector.size(); i++)
226      {
227        if( vertIndexVector[i] == vertIndex)
228          vertexFound = true;
229      }
230      if( !vertexFound)
231        vertIndexVector.push_back(vertIndex);
232    }
233  }
234  /* now realy calculate the center */
235  for( int i = 0; i < vertIndexVector.size(); ++i)
236  {
237    tmpVec = (sVec3D*)(&modelInf.pVertices[vertIndexVector[i]]);
238    arithCenter += *tmpVec;
239  }
240  box.arithCenter = arithCenter / vertIndexVector.size();
241
242
243
244  /* debug output section*/
245  PRINTF(3)("\nOBB Covariance Matrix:\n");
246  for(int j = 0; j < 3; ++j)
247  {
248    PRINT(3)("\t\t");
249    for(int k = 0; k < 3; ++k)
250    {
251      PRINT(3)("%11.4f\t", covariance[j][k]);
252    }
253    PRINT(3)("\n");
254  }
255  PRINTF(3)("\nWeighteed OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", center.x, center.y, center.z);
256  PRINTF(3)("\nArithmetical OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", box.arithCenter.x, box.arithCenter.y, box.arithCenter.z);
257
258  /* write back the covariance matrix data to the object oriented bouning box */
259}
260
261
262
263/**
264 *  calculate the eigenvectors for the object oriented box
265 * @param box: reference to the box
266 * @param modelInf: the model info structure of the model
267 * @param tirangleIndexes: an array with the indexes of the triangles inside this
268 * @param length: the length of the indexes array
269 */
270void OBBTreeNode::calculateBoxEigenvectors(OBB& box, const modelInfo& modelInf,
271    const int* triangleIndexes, unsigned int length)
272{
273
274  Vector         axis[3];                            //!< the references to the obb axis
275  Matrix         covMat(  box.covarianceMatrix  );   //!< covariance matrix (in the matrix dataform)
276
277  /*
278  now getting spanning vectors of the sub-space:
279  the eigenvectors of a symmertric matrix, such as the
280  covarience matrix are mutually orthogonal.
281  after normalizing them, they can be used as a the basis
282  vectors
283  */
284
285  /* calculate the axis */
286  covMat.getEigenVectors(axis[0], axis[1], axis[2] );
287  box.axis[0] = axis[0];
288  box.axis[1] = axis[1];
289  box.axis[2] = axis[2];
290
291  PRINTF(3)("Eigenvectors:\n");
292  PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[0].x, box.axis[0].y, box.axis[0].z);
293  PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[1].x, box.axis[1].y, box.axis[1].z);
294  PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[2].x, box.axis[2].y, box.axis[2].z);
295}
296
297
298
299
300/**
301 *  calculate the eigenvectors for the object oriented box
302 * @param box: reference to the box
303 * @param modelInf: the model info structure of the model
304 * @param tirangleIndexes: an array with the indexes of the triangles inside this
305 * @param length: the length of the indexes array
306 */
307void OBBTreeNode::calculateBoxAxis(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, unsigned int length)
308{
309
310  PRINTF(3)("Calculate Box Axis\n");
311  /* now get the axis length */
312  Line                ax[3];                                 //!< the axis
313  float               halfLength[3];                         //!< half length of the axis
314  float               tmpLength;                             //!< tmp save point for the length
315  Plane               p0(box.axis[0], box.center);           //!< the axis planes
316  Plane               p1(box.axis[1], box.center);           //!< the axis planes
317  Plane               p2(box.axis[2], box.center);           //!< the axis planes
318  float               maxLength[3];                          //!< maximal lenth of the axis
319  float               minLength[3];                          //!< minimal length of the axis
320  const sVec3D*       tmpVec;                                //!< variable taking tmp vectors
321
322  /*
323  this step is split up in three: first there will be made a bounding box which is
324  very badly centered. In the section step there will be calculated a new center
325  and a better bounding box.
326  */
327
328  /* get a bad bounding box axis */
329//   halfLength[0] = -1.0f;
330//   for( int j = 0; j < length; ++j)
331//   {
332//     for( int i = 0; i < 3; ++i)
333//     {
334//       tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]);
335//       tmpLength = fabs(p0.distancePoint(*tmpVec));
336//       if( tmpLength > halfLength[0])
337//         halfLength[0] = tmpLength;
338//     }
339//   }
340//
341//   halfLength[1] = -1.0f;
342//   for( int j = 0; j < length; ++j)
343//   {
344//     for( int i = 0; i < 3; ++i)
345//     {
346//       tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]);
347//       tmpLength = fabs(p0.distancePoint(*tmpVec));
348//       if( tmpLength > halfLength[1])
349//         halfLength[1] = tmpLength;
350//     }
351//   }
352//
353//   halfLength[2] = -1.0f;
354//   for( int j = 0; j < length; ++j)
355//   {
356//     for( int i = 0; i < 3; ++i)
357//     {
358//       tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]);
359//       tmpLength = fabs(p0.distancePoint(*tmpVec));
360//       if( tmpLength > halfLength[2])
361//         halfLength[2] = tmpLength;
362//     }
363//   }
364
365
366
367  /* get the maximal dimensions of the body in all directions */
368  /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */
369  tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]);
370  maxLength[0] = p0.distancePoint(*tmpVec);
371  minLength[0] = p0.distancePoint(*tmpVec);
372  for( int j = 0; j < length; ++j)
373  {
374    for( int i = 0; i < 3; ++i)
375    {
376      tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]);
377      tmpLength = p0.distancePoint(*tmpVec);
378      if( tmpLength > maxLength[0])
379        maxLength[0] = tmpLength;
380      else if( tmpLength < minLength[0])
381        minLength[0] = tmpLength;
382    }
383  }
384
385  /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */
386  tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]);
387  maxLength[1] = p1.distancePoint(*tmpVec);
388  minLength[1] = p1.distancePoint(*tmpVec);
389  for( int j = 0; j < length; ++j)
390  {
391    for( int i = 0; i < 3; ++i)
392    {
393      tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]);
394      tmpLength = p1.distancePoint(*tmpVec);
395      if( tmpLength > maxLength[1])
396        maxLength[1] = tmpLength;
397      else if( tmpLength < minLength[1])
398        minLength[1] = tmpLength;
399    }
400  }
401
402  /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */
403  tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]);
404  maxLength[2] = p2.distancePoint(*tmpVec);
405  minLength[2] = p2.distancePoint(*tmpVec);
406  for( int j = 0; j < length; ++j)
407  {
408    for( int i = 0; i < 3; ++i)
409    {
410      tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]);
411      tmpLength = p2.distancePoint(*tmpVec);
412      if( tmpLength > maxLength[2])
413        maxLength[2] = tmpLength;
414      else if( tmpLength < minLength[2])
415        minLength[2] = tmpLength;
416    }
417  }
418
419
420  /* calculate the real centre of the body by using the axis length */
421  float               centerOffset[3];
422  float               newHalfLength[3];
423
424  for( int i = 0; i < 3; ++i)
425  {
426    centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f;       // min length is negatie
427    newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f;      // min length is negative
428    box.center +=  (box.axis[i] * centerOffset[i]);               // update the new center vector
429    halfLength[i] = newHalfLength[i];
430  }
431  PRINTF(3)("\n");
432  PRINT(3)("\tAxis Length x: %f (max: %11.2f, \tmin: %11.2f)\n", halfLength[0], maxLength[0], minLength[0]);
433  PRINT(3)("\tAxis Length x: %f (max: %11.2f, \tmin: %11.2f)\n", halfLength[1], maxLength[1], minLength[1]);
434  PRINT(3)("\tAxis Length x: %f (max: %11.2f, \tmin: %11.2f)\n", halfLength[2], maxLength[2], minLength[2]);
435
436
437  box.halfLength[0] = halfLength[0];
438  box.halfLength[1] = halfLength[1];
439  box.halfLength[2] = halfLength[2];
440}
441
442
443
444/**
445 *  this separates an ob-box in the middle
446 * @param box: the box to separate
447 *
448 * this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
449 */
450void OBBTreeNode::forkBox(OBB& box)
451{
452
453  PRINTF(3)("Fork Box\n");
454  PRINTF(4)("Calculating the longest Axis\n");
455  /* get the longest axis of the box */
456  float               longestAxis = -1.0f;                 //!< the length of the longest axis
457  int                 longestAxisIndex = 0;                //!< this is the nr of the longest axis
458
459
460  /* now get the longest axis of the three exiting */
461  for( int i = 0; i < 3; ++i)
462  {
463    if( longestAxis < box.halfLength[i])
464    {
465      longestAxis = box.halfLength[i];
466      longestAxisIndex = i;
467    }
468  }
469  PRINTF(3)("\nLongest Axis is: Nr %i with a half-length of:%11.2f\n", longestAxisIndex, longestAxis);
470
471
472  PRINTF(4)("Separating along the longest axis\n");
473  /* get the closest vertex near the center */
474  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
475  float               tmpDist;                             //!< variable to save diverse distances temporarily
476  int                 vertexIndex;                         //!< index of the vertex near the center
477  Plane               middlePlane(box.axis[longestAxisIndex], box.center); //!< the middle plane
478  const sVec3D*       tmpVec;                              //!< temp simple 3D vector
479
480
481  /* now definin the separation plane through this specified nearest point and partition
482  the points depending on which side they are located
483  */
484  std::list<int>           partition1;                           //!< the vertex partition 1
485  std::list<int>           partition2;                           //!< the vertex partition 2
486  float*                   triangleCenter = new float[3];        //!< the center of the triangle
487  const float*             a;                                    //!< triangle  edge a
488  const float*             b;                                    //!< triangle  edge b
489  const float*             c;                                    //!< triangle  edge c
490
491
492  /* find the center of the box */
493
494  this->separationPlane = Plane(box.axis[longestAxisIndex], box.center);
495  this->sepPlaneCenter[0] = box.center.x;
496  this->sepPlaneCenter[1] = box.center.y;
497  this->sepPlaneCenter[2] = box.center.z;
498  this->longestAxisIndex = longestAxisIndex;
499
500  for( int i = 0; i < box.triangleIndexesLength; ++i)
501  {
502    /* first calculate the middle of the triangle */
503    a = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[0]];
504    b = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[1]];
505    c = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[2]];
506
507    triangleCenter[0] = (a[0] + b[0] + c[0]) / 3.0f;
508    triangleCenter[1] = (a[1] + b[1] + c[1]) / 3.0f;
509    triangleCenter[2] = (a[2] + b[2] + c[2]) / 3.0f;
510    tmpDist = this->separationPlane.distancePoint(*((sVec3D*)triangleCenter));
511
512    if( tmpDist > 0.0f)
513      partition1.push_back(box.triangleIndexes[i]); /* positive numbers plus zero */
514    else
515      partition2.push_back(box.triangleIndexes[i]); /* negatice numbers */
516  }
517  PRINTF(3)("\nPartition1: got \t%i Vertices \nPartition2: got \t%i Vertices\n", partition1.size(), partition2.size());
518
519
520  /* now comes the separation into two different sVec3D arrays */
521  int                index;                                //!< index storage place
522  int*               triangleIndexList1;                   //!< the vertex list 1
523  int*               triangleIndexList2;                   //!< the vertex list 2
524  std::list<int>::iterator element;                        //!< the list iterator
525
526  triangleIndexList1 = new int[partition1.size()];
527  triangleIndexList2 = new int[partition2.size()];
528
529  for( element = partition1.begin(), index = 0; element != partition1.end(); element++, index++)
530    triangleIndexList1[index] = (*element);
531
532  for( element = partition2.begin(), index = 0; element != partition2.end(); element++, index++)
533    triangleIndexList2[index] = (*element);
534
535  if( this->triangleIndexList1!= NULL)
536    delete[] this->triangleIndexList1;
537  this->triangleIndexList1 = triangleIndexList1;
538  this->triangleIndexLength1 = partition1.size();
539
540  if( this->triangleIndexList2 != NULL)
541    delete[] this->triangleIndexList2;
542  this->triangleIndexList2 = triangleIndexList2;
543  this->triangleIndexLength2 = partition2.size();
544}
545
546
547
548
549void OBBTreeNode::collideWith(const BVTreeNode& treeNode, const WorldEntity& nodeA, const WorldEntity& nodeB) const
550{
551  PRINTF(3)("collideWith\n");
552  /* if the obb overlap, make subtests: check which node is realy overlaping  */
553  PRINTF(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode.getIndex());
554  //   if( unlikely(treeNode == NULL)) return;
555
556
557  if( this->overlapTest(*this->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB))
558  {
559    PRINTF(3)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA.getClassName(), nodeB.getClassName(), this->nodeLeft, this->nodeRight);
560
561    /* check if left node overlaps */
562    if( likely( this->nodeLeft != NULL))
563    {
564      PRINTF(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode.getIndex());
565      if( this->overlapTest(*this->nodeLeft->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB))
566      {
567        this->nodeLeft->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeLeft), nodeA, nodeB);
568        this->nodeLeft->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeRight), nodeA, nodeB);
569      }
570    }
571    /* check if right node overlaps */
572    if( likely( this->nodeRight != NULL))
573    {
574      PRINTF(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode.getIndex());
575      if(this->overlapTest(*this->nodeRight->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB))
576      {
577        this->nodeRight->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeLeft), nodeA, nodeB);
578        this->nodeRight->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeRight), nodeA, nodeB);
579      }
580    }
581
582    /* so there is a collision and this is the last box in the tree (i.e. leaf) */
583    /* FIXME: If we would choose || insead of && there would also be asymmetrical cases supported */
584    if( unlikely(this->nodeRight == NULL && this->nodeLeft == NULL))
585    {
586      nodeA.collidesWith(nodeB, (((const OBBTreeNode*)&treeNode)->bvElement->center));
587
588      nodeB.collidesWith(nodeA, this->bvElement->center);
589    }
590
591  }
592}
593
594
595
596bool OBBTreeNode::overlapTest(const OBB& boxA, const OBB& boxB, const WorldEntity& nodeA, const WorldEntity& nodeB) const
597{
598  //   if( boxB == NULL || boxA == NULL)
599  //     return false;
600
601  /* first check all axis */
602  Vector t;
603  float rA = 0.0f;
604  float rB = 0.0f;
605  Vector l;
606  Vector rotAxisA[3];
607  Vector rotAxisB[3];
608
609  rotAxisA[0] =  nodeA.getAbsDir().apply(boxA.axis[0]);
610  rotAxisA[1] =  nodeA.getAbsDir().apply(boxA.axis[1]);
611  rotAxisA[2] =  nodeA.getAbsDir().apply(boxA.axis[2]);
612
613  rotAxisB[0] =  nodeB.getAbsDir().apply(boxB.axis[0]);
614  rotAxisB[1] =  nodeB.getAbsDir().apply(boxB.axis[1]);
615  rotAxisB[2] =  nodeB.getAbsDir().apply(boxB.axis[2]);
616
617
618  t = nodeA.getAbsCoor() + nodeA.getAbsDir().apply(boxA.center) - ( nodeB.getAbsCoor() + nodeB.getAbsDir().apply(boxB.center));
619
620  //   printf("\n");
621  //   printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxA->axis[0].x, boxA->axis[0].y, boxA->axis[0].z, rotAxisA[0].x, rotAxisA[0].y, rotAxisA[0].z);
622  //   printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxA->axis[1].x, boxA->axis[1].y, boxA->axis[1].z, rotAxisA[1].x, rotAxisA[1].y, rotAxisA[1].z);
623  //   printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxA->axis[2].x, boxA->axis[2].y, boxA->axis[2].z, rotAxisA[2].x, rotAxisA[2].y, rotAxisA[2].z);
624  //
625  //   printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxB->axis[0].x, boxB->axis[0].y, boxB->axis[0].z, rotAxisB[0].x, rotAxisB[0].y, rotAxisB[0].z);
626  //   printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxB->axis[1].x, boxB->axis[1].y, boxB->axis[1].z, rotAxisB[1].x, rotAxisB[1].y, rotAxisB[1].z);
627  //   printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxB->axis[2].x, boxB->axis[2].y, boxB->axis[2].z, rotAxisB[2].x, rotAxisB[2].y, rotAxisB[2].z);
628
629
630  /* All 3 axis of the object A */
631  for( int j = 0; j < 3; ++j)
632  {
633    rA = 0.0f;
634    rB = 0.0f;
635    l = rotAxisA[j];
636
637    rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l));
638    rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l));
639    rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l));
640
641    rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l));
642    rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l));
643    rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l));
644
645    PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
646
647    if( (rA + rB) < fabs(t.dot(l)))
648    {
649      PRINTF(3)("no Collision\n");
650      return false;
651    }
652  }
653
654  /* All 3 axis of the object B */
655  for( int j = 0; j < 3; ++j)
656  {
657    rA = 0.0f;
658    rB = 0.0f;
659    l = rotAxisB[j];
660
661    rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l));
662    rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l));
663    rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l));
664
665    rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l));
666    rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l));
667    rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l));
668
669    PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
670
671    if( (rA + rB) < fabs(t.dot(l)))
672    {
673      PRINTF(3)("no Collision\n");
674      return false;
675    }
676  }
677
678
679  /* Now check for all face cross products */
680
681  for( int j = 0; j < 3; ++j)
682  {
683    for(int k = 0; k < 3; ++k )
684    {
685      rA = 0.0f;
686      rB = 0.0f;
687      l = rotAxisA[j].cross(rotAxisB[k]);
688
689      rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l));
690      rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l));
691      rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l));
692
693      rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l));
694      rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l));
695      rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l));
696
697      PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
698
699      if( (rA + rB) < fabs(t.dot(l)))
700      {
701        PRINTF(3)("keine Kollision\n");
702        return false;
703      }
704    }
705  }
706
707  /* FIXME: there is no collision mark set now */
708  //   boxA.bCollided = true; /* use this ONLY(!!!!) for drawing operations */
709  //   boxB.bCollided = true;
710
711
712  PRINTF(3)("Kollision!\n");
713  return true;
714}
715
716
717
718
719
720void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color,  bool top) const
721{
722
723  /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */
724  if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL)
725  {
726    if( !(drawMode & DRAW_SINGLE && depth != 0))
727    {
728      if( drawMode & DRAW_POINTS)
729        glBegin(GL_POINTS);
730      for( int i = 0; i < this->bvElement->modelInf->numVertices; i+=3)
731      {
732        if( drawMode & DRAW_POINTS)
733          glVertex3f(this->bvElement->modelInf->pVertices[i], this->bvElement->modelInf->pVertices[i+1], this->bvElement->modelInf->pVertices[i+2]);
734        else
735        {
736          glPushMatrix();
737          glVertex3f(this->bvElement->modelInf->pVertices[i], this->bvElement->modelInf->pVertices[i+1], this->bvElement->modelInf->pVertices[i+2]);
738          gluSphere(OBBTreeNode_sphereObj, 0.1, 10, 10);
739          glPopMatrix();
740        }
741      }
742      if( drawMode & DRAW_POINTS)
743        glEnd();
744    }
745  }
746
747  if (top)
748  {
749    glPushAttrib(GL_ENABLE_BIT);
750    glDisable(GL_LIGHTING);
751    glDisable(GL_TEXTURE_2D);
752  }
753  glColor3f(color.x, color.y, color.z);
754
755
756  /* draw world axes */
757  if( drawMode & DRAW_BV_AXIS)
758  {
759    glBegin(GL_LINES);
760    glColor3f(1.0, 0.0, 0.0);
761    glVertex3f(0.0, 0.0, 0.0);
762    glVertex3f(3.0, 0.0, 0.0);
763
764    glColor3f(0.0, 1.0, 0.0);
765    glVertex3f(0.0, 0.0, 0.0);
766    glVertex3f(0.0, 3.0, 0.0);
767
768    glColor3f(0.0, 0.0, 1.0);
769    glVertex3f(0.0, 0.0, 0.0);
770    glVertex3f(0.0, 0.0, 3.0);
771    glEnd();
772  }
773
774
775  if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL)
776  {
777    if( !(drawMode & DRAW_SINGLE && depth != 0))
778    {
779      /* draw the obb axes */
780      glBegin(GL_LINES);
781      glColor3f(0.0, 0.4, 0.3);
782      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
783      glVertex3f(this->bvElement->center.x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
784                 this->bvElement->center.y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
785                 this->bvElement->center.z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
786
787      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
788      glVertex3f(this->bvElement->center.x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
789                 this->bvElement->center.y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
790                 this->bvElement->center.z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
791
792      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
793      glVertex3f(this->bvElement->center.x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
794                 this->bvElement->center.y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
795                 this->bvElement->center.z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
796      glEnd();
797    }
798  }
799
800
801  /* DRAW POLYGONS */
802  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
803  {
804    if (top)
805    {
806      glEnable(GL_BLEND);
807      glBlendFunc(GL_SRC_ALPHA, GL_ONE);
808    }
809
810    if(this->nodeLeft == NULL || this->nodeRight == NULL)
811      depth = 0;
812    if( !(drawMode & DRAW_SINGLE && depth != 0))
813    {
814      Vector cen = this->bvElement->center;
815      Vector* axis = this->bvElement->axis;
816      float* len = this->bvElement->halfLength;
817
818      if( this->bvElement->bCollided)
819      {
820        glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR
821      }
822      else if( drawMode & DRAW_BV_BLENDED)
823      {
824        glColor4f(color.x, color.y, color.z, .5);
825      }
826
827      /* draw bounding box */
828      if( drawMode & DRAW_BV_BLENDED)
829        glBegin(GL_QUADS);
830      else
831        glBegin(GL_LINE_LOOP);
832      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
833                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
834                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
835      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
836                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
837                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
838      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
839                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
840                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
841      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
842                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
843                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
844      glEnd();
845
846      if( drawMode & DRAW_BV_BLENDED)
847        glBegin(GL_QUADS);
848      else
849        glBegin(GL_LINE_LOOP);
850      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
851                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
852                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
853      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
854                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
855                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
856      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
857                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
858                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
859      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
860                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
861                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
862      glEnd();
863
864      if( drawMode & DRAW_BV_BLENDED)
865        glBegin(GL_QUADS);
866      else
867        glBegin(GL_LINE_LOOP);
868      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
869                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
870                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
871      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
872                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
873                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
874      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
875                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
876                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
877      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
878                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
879                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
880      glEnd();
881
882      if( drawMode & DRAW_BV_BLENDED)
883        glBegin(GL_QUADS);
884      else
885        glBegin(GL_LINE_LOOP);
886      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
887                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
888                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
889      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
890                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
891                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
892      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
893                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
894                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
895      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
896                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
897                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
898      glEnd();
899
900
901      if( drawMode & DRAW_BV_BLENDED)
902      {
903        glBegin(GL_QUADS);
904        glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
905                   cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
906                   cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
907        glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
908                   cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
909                   cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
910        glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
911                   cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
912                   cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
913        glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
914                   cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
915                   cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
916        glEnd();
917
918        glBegin(GL_QUADS);
919        glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
920                   cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
921                   cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
922        glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
923                   cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
924                   cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
925        glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
926                   cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
927                   cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
928        glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
929                   cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
930                   cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
931        glEnd();
932      }
933
934
935      if( drawMode & DRAW_BV_BLENDED)
936        glColor3f(color.x, color.y, color.z);
937    }
938
939  }
940
941  /* DRAW SEPARATING PLANE */
942  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
943  {
944    if( !(drawMode & DRAW_SINGLE && depth != 0))
945    {
946      if( drawMode & DRAW_BV_BLENDED)
947        glColor4f(color.x, color.y, color.z, .6);
948
949      /* now draw the separation plane */
950      Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
951      Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
952      Vector c = this->bvElement->center;
953      float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
954      float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
955      glBegin(GL_QUADS);
956      glVertex3f(c.x + a1.x * l1 + a2.x * l2, c.y + a1.y * l1+ a2.y * l2, c.z + a1.z * l1 + a2.z * l2);
957      glVertex3f(c.x - a1.x * l1 + a2.x * l2, c.y - a1.y * l1+ a2.y * l2, c.z - a1.z * l1 + a2.z * l2);
958      glVertex3f(c.x - a1.x * l1 - a2.x * l2, c.y - a1.y * l1- a2.y * l2, c.z - a1.z * l1 - a2.z * l2);
959      glVertex3f(c.x + a1.x * l1 - a2.x * l2, c.y + a1.y * l1- a2.y * l2, c.z + a1.z * l1 - a2.z * l2);
960      glEnd();
961
962      if( drawMode & DRAW_BV_BLENDED)
963        glColor4f(color.x, color.y, color.z, 1.0);
964
965    }
966  }
967
968
969
970  if (depth > 0)
971  {
972    if( this->nodeLeft != NULL)
973      this->nodeLeft->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(15.0,0.0,0.0)), false);
974    if( this->nodeRight != NULL)
975      this->nodeRight->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(30.0,0.0,0.0)), false);
976  }
977  this->bvElement->bCollided = false;
978
979  if (top)
980    glPopAttrib();
981}
982
983
984
985void OBBTreeNode::debug() const
986{
987  PRINT(0)("========OBBTreeNode::debug()=====\n");
988  PRINT(0)(" Current depth: %i", this->depth);
989  PRINT(0)(" ");
990  PRINT(0)("=================================\n");
991}
Note: See TracBrowser for help on using the repository browser.