Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/collision_detection/obb_tree_node.cc @ 5674

Last change on this file since 5674 was 5674, checked in by bensch, 18 years ago

orxonox/trunk: collision-detection with new Matrix-class

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