Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4711 was 4711, checked in by patrick, 19 years ago

orxonox/trunk: now the displayer show the data in an appropriate form

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