Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the cd now supports rotations in the detection

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