Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the collision detection engine works again and is ready for the presentation

File size: 34.4 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(3)("\n");
94  this->treeIndex = this->obbTree->getID();
95  PRINTF(3)("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(3)("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(3)("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
477
478/**
479  \brief this separates an ob-box in the middle
480  \param box: the box to separate
481
482  this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
483 */
484void OBBTreeNode::forkBox(OBB* box)
485{
486  /* get the longest axis of the box */
487  float               aLength = -1.0f;                     //!< the length of the longest axis
488  int                 axisIndex = 0;                       //!< this is the nr of the longest axis
489
490  for(int i = 0; i < 3; ++i)
491  {
492    if( aLength < box->halfLength[i])
493    {
494      aLength = box->halfLength[i];
495      axisIndex = i;
496    }
497  }
498
499   PRINTF(3)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength);
500
501
502  /* get the closest vertex near the center */
503  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
504  float               tmpDist;                             //!< temporary distance
505  int                 vertexIndex;
506  Plane               middlePlane(box->axis[axisIndex], *box->center); //!< the middle plane
507
508  vertexIndex = 0;
509  for(int i = 0; i < box->numOfVertices; ++i)
510  {
511    tmpDist = fabs(middlePlane.distancePoint(box->vertices[i]));
512    if( tmpDist < dist)
513    {
514      dist = tmpDist;
515      vertexIndex = i;
516    }
517  }
518
519  PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist);
520
521
522  /* now definin the separation plane through this specified nearest point and partition
523  the points depending on which side they are located
524  */
525  tList<sVec3D>      partition1;                           //!< the vertex partition 1
526  tList<sVec3D>      partition2;                           //!< the vertex partition 2
527
528
529  PRINTF(3)("vertex index: %i, of %i\n", vertexIndex, box->numOfVertices);
530  this->separationPlane = new Plane(box->axis[axisIndex], box->vertices[vertexIndex]);  //!< separation plane
531  this->sepPlaneCenter = &box->vertices[vertexIndex];
532  this->longestAxisIndex = axisIndex;
533
534  for(int i = 0; i < box->numOfVertices; ++i)
535  {
536    if( i == vertexIndex) continue;
537    tmpDist = this->separationPlane->distancePoint(box->vertices[i]);
538    if( tmpDist > 0.0)
539      partition1.add(&box->vertices[i]); /* positive numbers plus zero */
540    else
541      partition2.add(&box->vertices[i]); /* negatice numbers */
542  }
543  partition1.add(&box->vertices[vertexIndex]);
544  partition2.add(&box->vertices[vertexIndex]);
545
546  PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize());
547
548
549  /* now comes the separation into two different sVec3D arrays */
550  tIterator<sVec3D>* iterator;                             //!< the iterator to go through the lists
551  sVec3D*            element;                              //!< the elements
552  int                index;                                //!< index storage place
553  sVec3D*            vertList1;                            //!< the vertex list 1
554  sVec3D*            vertList2;                            //!< the vertex list 2
555
556  vertList1 = new sVec3D[partition1.getSize()];
557  vertList2 = new sVec3D[partition2.getSize()];
558
559  iterator = partition1.getIterator();
560  element = iterator->nextElement();
561  index = 0;
562  while( element != NULL)
563  {
564    vertList1[index][0] = element[0][0];
565    vertList1[index][1] = element[0][1];
566    vertList1[index][2] = element[0][2];
567    ++index;
568    element = iterator->nextElement();
569  }
570
571//   PRINTF(0)("\npartition 1:\n");
572//   for(int i = 0; i < partition1.getSize(); ++i)
573//   {
574//     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]);
575//   }
576
577  iterator = partition2.getIterator();
578  element = iterator->nextElement();
579  index = 0;
580  while( element != NULL)
581  {
582    vertList2[index][0] = element[0][0];
583    vertList2[index][1] = element[0][1];
584    vertList2[index][2] = element[0][2];
585    ++index;
586    element = iterator->nextElement();
587  }
588
589  this->tmpVert1 = vertList1;
590  this->tmpVert2 = vertList2;
591  this->tmpLen1 = partition1.getSize();
592  this->tmpLen2 = partition2.getSize();
593
594  delete iterator;
595
596//   PRINTF(0)("\npartition 2:\n");
597//   for(int i = 0; i < partition2.getSize(); ++i)
598//   {
599//     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]);
600//   }
601}
602
603
604
605
606void OBBTreeNode::collideWith(BVTreeNode* treeNode, PNode* nodeA, PNode* nodeB)
607{
608  PRINTF(3)("collideWith\n");
609  /* if the obb overlap, make subtests: check which node is realy overlaping  */
610  PRINT(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex());
611  if( unlikely(treeNode == NULL)) return;
612  if( this->overlapTest(this->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
613  {
614    /* check if left node overlaps */
615    if( likely( this->nodeLeft != NULL))
616    {
617      PRINT(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode->getIndex());
618      if( this->overlapTest(this->nodeLeft->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
619      {
620        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB);
621        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB);
622      }
623    }
624    /* check if right node overlaps */
625    if( likely( this->nodeRight != NULL))
626    {
627      PRINT(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode->getIndex());
628      if(this->overlapTest(this->nodeRight->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
629      {
630       this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB);
631       this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB);
632      }
633    }
634  }
635}
636
637
638
639bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, PNode* nodeA, PNode* nodeB)
640{
641  /* first check all axis */
642  Vector t;
643  float rA = 0.0f;
644  float rB = 0.0f;
645  Vector l;
646  Vector rotAxisA[3];
647  Vector rotAxisB[3];
648
649  rotAxisA[0] =  nodeA->getAbsDir().apply(boxA->axis[0]);
650  rotAxisA[1] =  nodeA->getAbsDir().apply(boxA->axis[1]);
651  rotAxisA[2] =  nodeA->getAbsDir().apply(boxA->axis[2]);
652
653  rotAxisB[0] =  nodeB->getAbsDir().apply(boxB->axis[0]);
654  rotAxisB[1] =  nodeB->getAbsDir().apply(boxB->axis[1]);
655  rotAxisB[2] =  nodeB->getAbsDir().apply(boxB->axis[2]);
656
657  t = nodeA->getAbsCoor() + nodeA->getAbsDir().apply(*boxA->center) - ( nodeB->getAbsCoor() + nodeB->getAbsDir().apply(*boxB->center));
658
659//   printf("\n");
660//   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);
661//   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);
662//   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);
663//
664//   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);
665//   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);
666//   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);
667
668
669  /* All 3 axis of the object A */
670  for( int j = 0; j < 3; ++j)
671  {
672    rA = 0.0f;
673    rB = 0.0f;
674    l = rotAxisA[j];
675
676    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
677    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
678    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
679
680    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
681    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
682    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
683
684    PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
685
686    if( (rA + rB) < fabs(t.dot(l)))
687    {
688      PRINT(3)("keine Kollision\n");
689      return false;
690    }
691  }
692
693  /* All 3 axis of the object B */
694  for( int j = 0; j < 3; ++j)
695  {
696    rA = 0.0f;
697    rB = 0.0f;
698    l = rotAxisB[j];
699
700    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
701    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
702    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
703
704    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
705    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
706    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
707
708    PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
709
710    if( (rA + rB) < fabs(t.dot(l)))
711    {
712      PRINT(3)("keine Kollision\n");
713      return false;
714    }
715  }
716
717
718  /* Now check for all face cross products */
719
720  for( int j = 0; j < 3; ++j)
721  {
722    for(int k = 0; k < 3; ++k )
723    {
724      rA = 0.0f;
725      rB = 0.0f;
726      l = rotAxisA[j].cross(rotAxisB[k]);
727
728      rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
729      rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
730      rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
731
732      rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
733      rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
734      rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
735
736      PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
737
738      if( (rA + rB) < fabs(t.dot(l)))
739      {
740        PRINT(3)("keine Kollision\n");
741        return false;
742      }
743    }
744  }
745
746
747  boxA->bCollided = true; /* use this ONLY(!!!!) for drawing operations */
748  boxB->bCollided = true;
749  PRINT(3)("Kollision!\n");
750  return true;
751}
752
753
754
755
756
757void OBBTreeNode::drawBV(int depth, int drawMode)
758{
759  this->obbTree->getMaterial(treeIndex)->select();
760
761  /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */
762  if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL)
763  {
764    if( !(drawMode & DRAW_SINGLE && depth != 0))
765    {
766      if( drawMode & DRAW_POINTS)
767        glBegin(GL_POINTS);
768      for(int i = 0; i < this->bvElement->numOfVertices; ++i)
769      {
770        if( drawMode & DRAW_POINTS)
771          glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
772        else
773        {
774          glPushMatrix();
775          glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
776          gluSphere(this->sphereObj, 0.1, 10, 10);
777          glPopMatrix();
778        }
779      }
780      if( drawMode & DRAW_POINTS)
781        glEnd();
782    }
783  }
784
785
786  /* draw world axes */
787  if( drawMode & DRAW_BV_AXIS)
788  {
789    glBegin(GL_LINES);
790    glColor3f(0.0, 0.4, 0.3);
791    glVertex3f(0.0, 0.0, 0.0);
792    glVertex3f(3.0, 0.0, 0.0);
793
794    glVertex3f(0.0, 0.0, 0.0);
795    glVertex3f(0.0, 3.0, 0.0);
796
797    glVertex3f(0.0, 0.0, 0.0);
798    glVertex3f(0.0, 0.0, 3.0);
799    glEnd();
800  }
801
802
803  if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL)
804  {
805    if( !(drawMode & DRAW_SINGLE && depth != 0))
806    {
807      /* draw the obb axes */
808      glBegin(GL_LINES);
809      glColor3f(0.0, 0.4, 0.3);
810      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
811      glVertex3f(this->bvElement->center->x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
812                 this->bvElement->center->y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
813                 this->bvElement->center->z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
814
815      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
816      glVertex3f(this->bvElement->center->x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
817                 this->bvElement->center->y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
818                 this->bvElement->center->z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
819
820      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
821      glVertex3f(this->bvElement->center->x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
822                 this->bvElement->center->y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
823                 this->bvElement->center->z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
824      glEnd();
825    }
826  }
827
828
829  /* DRAW POLYGONS */
830  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
831  {
832    if(this->nodeLeft == NULL || this->nodeRight == NULL)
833      depth = 0;
834    if( !(drawMode & DRAW_SINGLE && depth != 0))
835    {
836    Vector cen = *this->bvElement->center;
837    Vector* axis = this->bvElement->axis;
838    float* len = this->bvElement->halfLength;
839
840    if( this->bvElement->bCollided)
841      this->obbTree->getCollisionMaterial()->select();
842    else if( drawMode & DRAW_BV_BLENDED)
843      this->obbTree->getTransparentMaterial(treeIndex)->select();
844
845
846
847    /* draw bounding box */
848    if( drawMode & DRAW_BV_BLENDED)
849      glBegin(GL_QUADS);
850    else
851      glBegin(GL_LINE_LOOP);
852    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
853               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
854               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
855    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
856               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
857               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
858    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
859               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
860               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
861    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
862               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
863               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
864    glEnd();
865
866    if( drawMode & DRAW_BV_BLENDED)
867      glBegin(GL_QUADS);
868    else
869      glBegin(GL_LINE_LOOP);
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    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
877               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
878               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
879    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
880               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
881               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
882    glEnd();
883
884    if( drawMode & DRAW_BV_BLENDED)
885      glBegin(GL_QUADS);
886    else
887      glBegin(GL_LINE_LOOP);
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    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
895               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
896               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
897    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
898               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
899               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
900    glEnd();
901
902    if( drawMode & DRAW_BV_BLENDED)
903      glBegin(GL_QUADS);
904    else
905      glBegin(GL_LINE_LOOP);
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    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
913               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
914               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
915    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
916               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
917               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
918    glEnd();
919
920
921    if( drawMode & DRAW_BV_BLENDED)
922    {
923      glBegin(GL_QUADS);
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      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
931                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
932                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
933      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
934                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
935                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
936      glEnd();
937
938      glBegin(GL_QUADS);
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      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
949                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
950                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
951      glEnd();
952    }
953
954
955    if( drawMode & DRAW_BV_BLENDED)
956      this->obbTree->getMaterial(treeIndex)->select();
957    }
958
959  }
960
961  /* DRAW SEPARATING PLANE */
962  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
963  {
964    if( !(drawMode & DRAW_SINGLE && depth != 0))
965    {
966      if( drawMode & DRAW_BV_BLENDED)
967        this->obbTree->getTransparentMaterial(treeIndex)->select();
968
969    /* now draw the separation plane */
970    Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
971    Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
972    Vector c = *this->bvElement->center;
973    float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
974    float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
975    glBegin(GL_QUADS);
976    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);
977    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);
978    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);
979    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);
980    glEnd();
981
982    if( drawMode & DRAW_BV_BLENDED)
983      this->obbTree->getMaterial(treeIndex)->select();
984
985    }
986  }
987
988
989
990  if( this->nodeLeft != NULL && depth != 0 )
991    this->nodeLeft->drawBV(depth - 1, drawMode);
992  if( this->nodeRight != NULL && depth != 0)
993    this->nodeRight->drawBV(depth - 1, drawMode);
994
995  this->bvElement->bCollided = false;
996}
997
998
999
1000void OBBTreeNode::debug(void) const
1001{
1002
1003  /*
1004  for(int i = 0; i < length; i++)
1005  {
1006  PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
1007}
1008  */
1009}
Note: See TracBrowser for help on using the repository browser.