Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now all three axis are checked, there seem to be some problems.

File size: 32.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION
17
18#include "obb_tree_node.h"
19#include "list.h"
20#include "obb.h"
21#include "obb_tree.h"
22#include "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  box->axis = axis;
368
369  PRINTF(3)("-- Got Axis\n");
370
371  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[0].x, box->axis[0].y, box->axis[0].z);
372  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[1].x, box->axis[1].y, box->axis[1].z);
373  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[2].x, box->axis[2].y, box->axis[2].z);
374}
375
376
377void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length)
378{
379
380  /* now get the axis length */
381  Line                ax[3];                                 //!< the axis
382  float*              halfLength = new float[3];             //!< half length of the axis
383  float               tmpLength;                             //!< tmp save point for the length
384  Plane               p0(box->axis[0], *box->center);       //!< the axis planes
385  Plane               p1(box->axis[1], *box->center);
386  Plane               p2(box->axis[2], *box->center);
387  float               maxLength[3];
388  float               minLength[3];
389
390
391  /* get a bad bounding box */
392  halfLength[0] = -1.0f;
393  for(int j = 0; j < length; ++j)
394    {
395      tmpLength = fabs(p0.distancePoint(vertices[j]));
396      if( tmpLength > halfLength[0])
397        halfLength[0] = tmpLength;
398    }
399
400  halfLength[1] = -1.0f;
401  for(int j = 0; j < length; ++j)
402    {
403      tmpLength = fabs(p1.distancePoint(vertices[j]));
404      if( tmpLength > halfLength[1])
405        halfLength[1] = tmpLength;
406    }
407
408  halfLength[2] = -1.0f;
409  for(int j = 0; j < length; ++j)
410    {
411      tmpLength = fabs(p2.distancePoint(vertices[j]));
412      if( tmpLength > halfLength[2])
413        halfLength[2] = tmpLength;
414    }
415
416
417
418  /* get the maximal dimensions of the body in all directions */
419   maxLength[0] = 0.0f;
420   minLength[0] = 0.0f;
421   for(int j = 0; j < length; ++j)
422   {
423     tmpLength = p0.distancePoint(vertices[j]);
424     if( tmpLength > maxLength[0])
425       maxLength[0] = tmpLength;
426     else if( tmpLength < minLength[0])
427       minLength[0] = tmpLength;
428   }
429
430   maxLength[1] = 0.0f;
431   minLength[1] = 0.0f;
432   for(int j = 0; j < length; ++j)
433   {
434     tmpLength = p1.distancePoint(vertices[j]);
435     if( tmpLength > maxLength[1])
436       maxLength[1] = tmpLength;
437     else if( tmpLength < minLength[1])
438       minLength[1] = tmpLength;
439   }
440
441   maxLength[2] = 0.0f;
442   minLength[2] = 0.0f;
443   for(int j = 0; j < length; ++j)
444   {
445     tmpLength = p2.distancePoint(vertices[j]);
446     if( tmpLength > maxLength[2])
447       maxLength[2] = tmpLength;
448     else if( tmpLength < minLength[2])
449       minLength[2] = tmpLength;
450   }
451
452
453   /* calculate the real centre of the body by using the axis length */
454   float centerOffset[3];
455   float newHalfLength[3];
456   for(int i = 0; i < 3; ++i)
457     {
458       PRINTF(3)("max: %f, min: %f \n", maxLength[i], minLength[i]);
459       centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f; // min length is negatie
460       newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f; // min length is negative
461       *box->center +=  (box->axis[i] * centerOffset[i]);            // update the new center vector
462       halfLength[i] = newHalfLength[i];
463     }
464
465
466
467  box->halfLength = halfLength;
468  PRINTF(3)("-- Written Axis to obb\n");
469  PRINTF(3)("-- Finished Calculating Attributes\n");
470
471
472
473//   PRINTF(3)("\nwe got length: \n");
474  for(int i = 0; i < 3; ++i)
475  {
476    //if( box->halfLength[i] == 0.0)
477    PRINTF(3)("length[%i] = %f\n", i, box->halfLength[i]);
478  }
479}
480
481
482
483/**
484  \brief this separates an ob-box in the middle
485  \param box: the box to separate
486
487  this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
488 */
489void OBBTreeNode::forkBox(OBB* box)
490{
491  /* get the longest axis of the box */
492  float               aLength = -1.0f;                     //!< the length of the longest axis
493  int                 axisIndex = 0;                       //!< this is the nr of the longest axis
494
495  for(int i = 0; i < 3; ++i)
496  {
497    if( aLength < box->halfLength[i])
498    {
499      aLength = box->halfLength[i];
500      axisIndex = i;
501    }
502  }
503
504   PRINTF(3)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength);
505
506
507  /* get the closest vertex near the center */
508  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
509  float               tmpDist;                             //!< temporary distance
510  int                 vertexIndex;
511  Plane               middlePlane(box->axis[axisIndex], *box->center); //!< the middle plane
512
513  vertexIndex = 0;
514  for(int i = 0; i < box->numOfVertices; ++i)
515  {
516    tmpDist = fabs(middlePlane.distancePoint(box->vertices[i]));
517    if( tmpDist < dist)
518    {
519      dist = tmpDist;
520      vertexIndex = i;
521    }
522  }
523
524//   PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist);
525
526
527  /* now definin the separation plane through this specified nearest point and partition
528  the points depending on which side they are located
529  */
530  tList<sVec3D>      partition1;                           //!< the vertex partition 1
531  tList<sVec3D>      partition2;                           //!< the vertex partition 2
532
533  PRINTF(3)("vertex index: %i, of %i\n", vertexIndex, box->numOfVertices);
534  this->separationPlane = new Plane(box->axis[axisIndex], box->vertices[vertexIndex]);  //!< separation plane
535  this->sepPlaneCenter = &box->vertices[vertexIndex];
536  this->longestAxisIndex = axisIndex;
537
538  for(int i = 0; i < box->numOfVertices; ++i)
539  {
540    if( this->separationPlane->distancePoint(box->vertices[i]) > 0.0f)
541      partition1.add(&box->vertices[i]);
542    else
543      partition2.add(&box->vertices[i]);
544  }
545  partition1.add(&box->vertices[vertexIndex]);
546
547//   PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize());
548
549
550  /* now comes the separation into two different sVec3D arrays */
551  tIterator<sVec3D>* iterator;                             //!< the iterator to go through the lists
552  sVec3D*            element;                              //!< the elements
553  int                index;                                //!< index storage place
554  sVec3D*            vertList1;                            //!< the vertex list 1
555  sVec3D*            vertList2;                            //!< the vertex list 2
556
557  vertList1 = new sVec3D[partition1.getSize()];
558  vertList2 = new sVec3D[partition2.getSize()];
559
560  iterator = partition1.getIterator();
561  element = iterator->nextElement();
562  index = 0;
563  while( element != NULL)
564  {
565    vertList1[index][0] = element[0][0];
566    vertList1[index][1] = element[0][1];
567    vertList1[index][2] = element[0][2];
568    ++index;
569    element = iterator->nextElement();
570  }
571
572//   PRINTF(0)("\npartition 1:\n");
573//   for(int i = 0; i < partition1.getSize(); ++i)
574//   {
575//     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]);
576//   }
577
578  iterator = partition2.getIterator();
579  element = iterator->nextElement();
580  index = 0;
581  while( element != NULL)
582  {
583    vertList2[index][0] = element[0][0];
584    vertList2[index][1] = element[0][1];
585    vertList2[index][2] = element[0][2];
586    ++index;
587    element = iterator->nextElement();
588  }
589
590  this->tmpVert1 = vertList1;
591  this->tmpVert2 = vertList2;
592  this->tmpLen1 = partition1.getSize();
593  this->tmpLen2 = partition2.getSize();
594
595  delete iterator;
596
597//   PRINTF(0)("\npartition 2:\n");
598//   for(int i = 0; i < partition2.getSize(); ++i)
599//   {
600//     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]);
601//   }
602}
603
604
605
606
607void OBBTreeNode::collideWith(BVTreeNode* treeNode, PNode* nodeA, PNode* nodeB)
608{
609  PRINTF(0)("collideWith\n");
610  /* if the obb overlap, make subtests: check which node is realy overlaping  */
611  if( this->overlapTest(this->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
612  {
613    /* check if left node overlaps */
614    if( unlikely( this->nodeLeft != NULL))
615      if( this->overlapTest(this->nodeLeft->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
616        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB);
617    /* check if right node overlaps */
618    if( unlikely( this->nodeRight != NULL))
619      if(this->overlapTest(this->nodeRight->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
620        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB);
621  }
622}
623
624
625
626bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, PNode* nodeA, PNode* nodeB)
627{
628
629
630  /* first check all axis */
631  Vector t = nodeA->getAbsCoor() + *boxA->center - ( nodeB->getAbsCoor() + *boxB->center);
632  float rA = 0.0f;
633  float rB = 0.0f;
634  Vector l;
635
636  /* All 3 axis of the object A */
637  for( int j = 0; j < 3; ++j)
638    {
639      rA = 0.0f;
640      rB = 0.0f;
641      l = boxA->axis[j];
642
643      for(int i = 0; i < 3; ++i)
644        rA += fabs(boxA->halfLength[i] * boxA->axis[i].dot(l));
645      for(int i = 0; i < 3; ++i)
646        rB += fabs(boxB->halfLength[i] * boxB->axis[i].dot(l));
647
648
649      PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
650
651      if( (rA + rB) < fabs(t.dot(l)))
652      {
653        PRINTF(0)(" - Keine Kollision in der Bahnfohstrasse! Passagiere der Linien 6, 14 sind gebeten zu fliegen!\n");
654        return false;
655      }
656    }
657
658    /* All 3 axis of the object B */
659    for( int j = 0; j < 3; ++j)
660    {
661      rA = 0.0f;
662      rB = 0.0f;
663      l = boxB->axis[j];
664
665      for(int i = 0; i < 3; ++i)
666        rA += fabs(boxA->halfLength[i] * boxA->axis[i].dot(l));
667      for(int i = 0; i < 3; ++i)
668        rB += fabs(boxB->halfLength[i] * boxB->axis[i].dot(l));
669
670
671      PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
672
673      if( (rA + rB) < fabs(t.dot(l)))
674      {
675        PRINTF(0)(" - Keine Kollision in der Bahnfohstrasse! Passagiere der Linien 6, 14 sind gebeten zu fliegen!\n");
676        return false;
677      }
678    }
679
680
681    /* Now check for all face cross products */
682
683      for( int j = 0; j < 3; ++j)
684      {
685        for(int k = 0; k < 3; ++k )
686        {
687          rA = 0.0f;
688          rB = 0.0f;
689          l = boxA->axis[j].cross(boxB->axis[k]);
690
691          for(int i = 0; i < 3; ++i)
692            rA += fabs(boxA->halfLength[i] * boxA->axis[i].dot(l));
693          for(int i = 0; i < 3; ++i)
694            rB += fabs(boxB->halfLength[i] * boxB->axis[i].dot(l));
695
696
697          PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
698
699          if( (rA + rB) < fabs(t.dot(l)))
700          {
701            PRINTF(0)(" - Keine Kollision in der Bahnfohstrasse! Passagiere der Linien 6, 14 sind gebeten zu fliegen!\n");
702            return false;
703          }
704        }
705      }
706
707
708
709
710    boxA->bCollided = true; /* use this ONLY!!!! for drawing operations */
711    boxB->bCollided = true;
712    return true;
713}
714
715
716
717void OBBTreeNode::drawBV(int depth, int drawMode)
718{
719  this->obbTree->getMaterial(treeIndex)->select();
720
721  /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */
722  if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL)
723  {
724    if( !(drawMode & DRAW_SINGLE && depth != 0))
725    {
726      for(int i = 0; i < this->bvElement->numOfVertices; ++i)
727      {
728        glPushMatrix();
729        //glMatrixMode(GL_MODELVIEW);
730        //glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
731        glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
732        gluSphere(this->sphereObj, 0.1, 10, 10);
733        //PRINTF(0)("v(%f, %f, %f)\n", this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
734        glPopMatrix();
735      }
736    }
737  }
738
739
740  /* draw world axes */
741  if( drawMode & DRAW_BV_AXIS)
742  {
743    glBegin(GL_LINES);
744    glColor3f(0.0, 0.4, 0.3);
745    glVertex3f(0.0, 0.0, 0.0);
746    glVertex3f(3.0, 0.0, 0.0);
747
748    glVertex3f(0.0, 0.0, 0.0);
749    glVertex3f(0.0, 3.0, 0.0);
750
751    glVertex3f(0.0, 0.0, 0.0);
752    glVertex3f(0.0, 0.0, 3.0);
753    glEnd();
754  }
755
756
757  if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL)
758  {
759    if( !(drawMode & DRAW_SINGLE && depth != 0))
760    {
761      /* draw the obb axes */
762      glBegin(GL_LINES);
763      glColor3f(0.0, 0.4, 0.3);
764      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
765      glVertex3f(this->bvElement->center->x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
766                 this->bvElement->center->y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
767                 this->bvElement->center->z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
768
769      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
770      glVertex3f(this->bvElement->center->x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
771                 this->bvElement->center->y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
772                 this->bvElement->center->z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
773
774      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
775      glVertex3f(this->bvElement->center->x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
776                 this->bvElement->center->y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
777                 this->bvElement->center->z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
778      glEnd();
779    }
780  }
781
782
783  /* DRAW POLYGONS */
784  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
785  {
786    if( !(drawMode & DRAW_SINGLE && depth != 0))
787    {
788    Vector cen = *this->bvElement->center;
789    Vector* axis = this->bvElement->axis;
790    float* len = this->bvElement->halfLength;
791
792    if( this->bvElement->bCollided)
793      this->obbTree->getCollisionMaterial()->select();
794    else if( drawMode & DRAW_BV_BLENDED)
795      this->obbTree->getTransparentMaterial(treeIndex)->select();
796
797
798
799    /* draw bounding box */
800    if( drawMode & DRAW_BV_BLENDED)
801      glBegin(GL_QUADS);
802    else
803      glBegin(GL_LINE_LOOP);
804    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
805               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
806               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
807    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
808               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
809               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
810    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
811               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
812               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
813    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
814               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
815               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
816    glEnd();
817
818    if( drawMode & DRAW_BV_BLENDED)
819      glBegin(GL_QUADS);
820    else
821      glBegin(GL_LINE_LOOP);
822    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
823               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
824               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
825    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
826               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
827               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
828    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
829               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
830               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
831    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
832               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
833               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
834    glEnd();
835
836    if( drawMode & DRAW_BV_BLENDED)
837      glBegin(GL_QUADS);
838    else
839      glBegin(GL_LINE_LOOP);
840    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
841               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
842               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
843    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
844               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
845               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
846    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
847               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
848               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
849    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
850               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
851               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
852    glEnd();
853
854    if( drawMode & DRAW_BV_BLENDED)
855      glBegin(GL_QUADS);
856    else
857      glBegin(GL_LINE_LOOP);
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    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
865               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
866               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
867    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
868               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
869               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
870    glEnd();
871
872
873    if( drawMode & DRAW_BV_BLENDED)
874    {
875      glBegin(GL_QUADS);
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      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
883                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
884                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
885      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
886                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
887                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
888      glEnd();
889
890      glBegin(GL_QUADS);
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      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
901                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
902                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
903      glEnd();
904    }
905
906
907    if( drawMode & DRAW_BV_BLENDED)
908      this->obbTree->getMaterial(treeIndex)->select();
909    }
910
911  }
912
913  /* DRAW SEPARATING PLANE */
914  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
915  {
916    if( !(drawMode & DRAW_SINGLE && depth != 0))
917    {
918      if( drawMode & DRAW_BV_BLENDED)
919        this->obbTree->getTransparentMaterial(treeIndex)->select();
920
921    /* now draw the separation plane */
922    Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
923    Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
924    Vector c = *this->bvElement->center;
925    float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
926    float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
927    glBegin(GL_QUADS);
928    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);
929    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);
930    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);
931    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);
932    glEnd();
933
934    if( drawMode & DRAW_BV_BLENDED)
935      this->obbTree->getMaterial(treeIndex)->select();
936
937    }
938  }
939
940
941
942  if( this->nodeLeft != NULL && depth != 0 )
943    this->nodeLeft->drawBV(depth - 1, drawMode);
944  if( this->nodeRight != NULL && depth != 0)
945    this->nodeRight->drawBV(depth - 1, drawMode);
946
947  this->bvElement->bCollided = false;
948}
949
950
951
952void OBBTreeNode::debug(void) const
953{
954
955  /*
956  for(int i = 0; i < length; i++)
957  {
958  PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
959}
960  */
961}
Note: See TracBrowser for help on using the repository browser.