Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

collision_detection: some source reformat and function reorg

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