Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the presentation back

File size: 35.7 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*/
14
15#define DEBUG_SPECIAL_MODULE 3/* DEBUG_MODULE_COLLISION_DETECTION*/
16
17#include "obb_tree_node.h"
18#include "obb_tree.h"
19#include "obb.h"
20
21#include "matrix.h"
22#include "model.h"
23#include "world_entity.h"
24#include "plane.h"
25
26#include "color.h"
27#include "glincl.h"
28
29#include <list>
30#include <vector>
31#include "debug.h"
32
33
34
35using namespace std;
36
37
38GLUquadricObj* OBBTreeNode_sphereObj = NULL;
39
40
41/**
42 *  standard constructor
43 * @param tree: reference to the obb tree
44 * @param depth: the depth of the obb tree to generate
45 */
46OBBTreeNode::OBBTreeNode (const OBBTree& tree, OBBTreeNode* prev, int depth)
47    : BVTreeNode()
48{
49  this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode");
50
51  this->obbTree = &tree;
52  this->nodePrev = prev;
53  this->depth = depth;
54  this->nextID = 0;
55
56  this->nodeLeft = NULL;
57  this->nodeRight = NULL;
58  this->bvElement = NULL;
59
60  this->triangleIndexList1 = NULL;
61  this->triangleIndexList2 = NULL;
62
63  this->modelInf = NULL;
64  this->triangleIndexes = NULL;
65
66  if( OBBTreeNode_sphereObj == NULL)
67    OBBTreeNode_sphereObj = gluNewQuadric();
68
69  this->owner = NULL;
70
71  /* debug ids */
72  if( this->nodePrev)
73    this->treeIndex = 100 * this->depth + this->nodePrev->getID();
74  else
75    this->treeIndex = 0;
76}
77
78
79/**
80 *  standard deconstructor
81 */
82OBBTreeNode::~OBBTreeNode ()
83{
84  if( this->nodeLeft)
85    delete this->nodeLeft;
86  if( this->nodeRight)
87    delete this->nodeRight;
88
89  if( this->bvElement)
90    delete this->bvElement;
91}
92
93
94
95void OBBTreeNode::createBox(Vector start, Vector end)
96{
97
98  this->bvElement = new OBB();
99  this->nodeLeft = NULL;
100  this->nodeRight = NULL;
101//   this->depth = 0;
102
103  this->bvElement->center = (end - start) * 0.5f;
104  this->bvElement->halfLength[0] = (end.x - start.x) * 0.5f;
105  this->bvElement->halfLength[1] = (end.y - start.y) * 0.5f;
106  this->bvElement->halfLength[2] = (end.z - start.z) * 0.5f;
107
108  this->bvElement->axis[0] = Vector(1,0,0);
109  this->bvElement->axis[1] = Vector(0,1,0);
110  this->bvElement->axis[2] = Vector(0,0,1);
111}
112
113
114
115/**
116 *  creates a new BVTree or BVTree partition
117 * @param depth: how much more depth-steps to go: if == 1 don't go any deeper!
118 * @param modInfo: model informations from the abstrac model
119 *
120 * this function creates the Bounding Volume tree from a modelInfo struct and bases its calculations
121 * on the triangle informations (triangle soup not polygon soup)
122 */
123void OBBTreeNode::spawnBVTree(const modelInfo& modelInf, const int* triangleIndexes, int length)
124{
125  PRINTF(4)("\n==============================Creating OBB Tree Node==================\n");
126  PRINT(4)(" OBB Tree Infos: \n");
127  PRINT(4)("\tDepth: %i \n\tTree Index: %i \n\tNumber of Triangles: %i\n", depth, this->treeIndex, length);
128  this->depth = depth;
129
130  this->bvElement = new OBB();
131  this->bvElement->modelInf = &modelInf;
132  this->bvElement->triangleIndexes = triangleIndexes;
133  this->bvElement->triangleIndexesLength = length;
134
135  /* create the bounding boxes in three steps */
136  this->calculateBoxCovariance(*this->bvElement, modelInf, triangleIndexes, length);
137  this->calculateBoxEigenvectors(*this->bvElement, modelInf, triangleIndexes, length);
138  this->calculateBoxAxis(*this->bvElement, modelInf, triangleIndexes, length);
139
140  /* do we need to descent further in the obb tree?*/
141  if( likely( this->depth > 0))
142  {
143    this->forkBox(*this->bvElement);
144
145    if( this->triangleIndexLength1 >= 3)
146    {
147      this->nodeLeft = new OBBTreeNode(*this->obbTree, this, depth - 1);
148      this->nodeLeft->spawnBVTree(modelInf, this->triangleIndexList1, this->triangleIndexLength1);
149    }
150    if( this->triangleIndexLength2 >= 3)
151    {
152      this->nodeRight = new OBBTreeNode(*this->obbTree, this, depth - 1);
153      this->nodeRight->spawnBVTree(modelInf, this->triangleIndexList2, this->triangleIndexLength2);
154    }
155  }
156}
157
158
159
160/**
161 *  calculate the box covariance matrix
162 * @param box: reference to the box
163 * @param modelInf: the model info structure of the model
164 * @param tirangleIndexes: an array with the indexes of the triangles inside this
165 * @param length: the length of the indexes array
166 */
167void OBBTreeNode::calculateBoxCovariance(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, int length)
168{
169  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
170  float     face = 0.0f;                             //!< surface area of the entire convex hull
171  Vector    centroid[length];                        //!< centroid of the i'th convex hull
172  Vector    center;                                  //!< the center of the entire hull
173  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
174  Vector    t1, t2;                                  //!< temporary values
175  float     covariance[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};//!< the covariance matrix
176
177  /* fist compute all the convex hull face/facelets and centroids */
178  for( int i = 0; i < length ; ++i)
179  {
180    p = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]];
181    q = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]];
182    r = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]];
183
184    /* finding the facelet surface via cross-product */
185    t1 = p - q;
186    t2 = p - r;
187    facelet[i] = 0.5f * /*fabs*/( t1.cross(t2).len() );
188    /* update the entire convex hull surface */
189    face += facelet[i];
190
191    /* calculate the cetroid of the hull triangles */
192    centroid[i] = (p + q + r) / 3.0f;
193    /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
194    center += centroid[i] * facelet[i];
195    /* the arithmetical center */
196  }
197  /* take the average of the centroid sum */
198  center /= face;
199
200
201  /* now calculate the covariance matrix - if not written in three for-loops,
202     it would compute faster: minor */
203  for( int j = 0; j < 3; ++j)
204  {
205    for( int k = 0; k < 3; ++k)
206    {
207      for( int i = 0; i < length; ++i)
208      {
209        p = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]);
210        q = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]);
211        r = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]);
212
213        covariance[j][k] = facelet[i] * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] +
214                           q[j] * q[k] + r[j] * r[k]);
215      }
216      covariance[j][k] = covariance[j][k] / (12.0f * face) - center[j] * center[k];
217    }
218  }
219  for( int i = 0; i < 3; ++i)
220  {
221    box.covarianceMatrix[i][0] = covariance[i][0];
222    box.covarianceMatrix[i][1] = covariance[i][1];
223    box.covarianceMatrix[i][2] = covariance[i][2];
224  }
225  box.center = center;
226
227  /* debug output section*/
228  PRINTF(4)("\nOBB Covariance Matrix:\n");
229  for(int j = 0; j < 3; ++j)
230  {
231    PRINT(4)("\t\t");
232    for(int k = 0; k < 3; ++k)
233    {
234      PRINT(4)("%11.4f\t", covariance[j][k]);
235    }
236    PRINT(4)("\n");
237  }
238  PRINTF(4)("\nWeighteed OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", center.x, center.y, center.z);
239}
240
241
242
243/**
244 *  calculate the eigenvectors for the object oriented box
245 * @param box: reference to the box
246 * @param modelInf: the model info structure of the model
247 * @param tirangleIndexes: an array with the indexes of the triangles inside this
248 * @param length: the length of the indexes array
249 */
250void OBBTreeNode::calculateBoxEigenvectors(OBB& box, const modelInfo& modelInf,
251    const int* triangleIndexes, int length)
252{
253
254  Vector         axis[3];                            //!< the references to the obb axis
255  Matrix         covMat(  box.covarianceMatrix  );   //!< covariance matrix (in the matrix dataform)
256
257  /*
258  now getting spanning vectors of the sub-space:
259  the eigenvectors of a symmertric matrix, such as the
260  covarience matrix are mutually orthogonal.
261  after normalizing them, they can be used as a the basis
262  vectors
263  */
264
265  /* calculate the axis */
266  covMat.getEigenVectors(axis[0], axis[1], axis[2] );
267  box.axis[0] = axis[0];
268  box.axis[1] = axis[1];
269  box.axis[2] = axis[2];
270
271  // this is for axis aligned bouning boxes only
272//   box.axis[0] = Vector(1,0,0);
273//   box.axis[1] = Vector(0,1,0);
274//   box.axis[2] = Vector(0,0,1);
275
276  PRINTF(4)("Eigenvectors:\n");
277  PRINT(4)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[0].x, box.axis[0].y, box.axis[0].z);
278  PRINT(4)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[1].x, box.axis[1].y, box.axis[1].z);
279  PRINT(4)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[2].x, box.axis[2].y, box.axis[2].z);
280}
281
282
283
284
285/**
286 *  calculate the eigenvectors for the object oriented box
287 * @param box: reference to the box
288 * @param modelInf: the model info structure of the model
289 * @param tirangleIndexes: an array with the indexes of the triangles inside this
290 * @param length: the length of the indexes array
291 */
292void OBBTreeNode::calculateBoxAxis(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, int length)
293{
294
295  PRINTF(4)("Calculate Box Axis\n");
296  /* now get the axis length */
297  float               tmpLength;                             //!< tmp save point for the length
298  Plane               p0(box.axis[0], box.center);           //!< the axis planes
299  Plane               p1(box.axis[1], box.center);           //!< the axis planes
300  Plane               p2(box.axis[2], box.center);           //!< the axis planes
301  float               maxLength[3];                          //!< maximal lenth of the axis
302  float               minLength[3];                          //!< minimal length of the axis
303  const float*        tmpVec;                                //!< variable taking tmp vectors
304  float               centerOffset[3];
305
306  /* get the maximal dimensions of the body in all directions */
307  /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */
308  for( int k = 0; k  < 3; k++)
309  {
310    tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]);
311    Plane* p;
312    if( k == 0)
313      p = &p0;
314    else if( k == 1)
315      p = &p1;
316    else
317      p = &p2;
318    maxLength[k] = p->distancePoint(tmpVec);
319    minLength[k] = p->distancePoint(tmpVec);
320
321    for( int j = 0; j < length; ++j) {
322      for( int i = 0; i < 3; ++i) {
323        tmpVec = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]];
324        tmpLength = p->distancePoint(tmpVec);
325
326        if( tmpLength > maxLength[k])
327          maxLength[k] = tmpLength;
328        else if( tmpLength < minLength[k])
329          minLength[k] = tmpLength;
330      }
331    }
332  }
333
334
335
336  /* calculate the real centre of the body by using the axis length */
337  for( int i = 0; i < 3; ++i)
338  {
339    if( maxLength[i] > 0.0f && minLength[i] > 0.0f)  // both axis positiv
340      centerOffset[i] = minLength[i] + (maxLength[i] - minLength[i]) / 2.0f;
341    else if( maxLength[i] > 0.0f && maxLength[i] < 0.0f) // positiv and negativ
342      centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f;
343    else //both negativ
344     centerOffset[i] = minLength[i] + (maxLength[i] - minLength[i]) / 2.0f;
345
346    box.halfLength[i] = (maxLength[i] - minLength[i]) / 2.0f;
347  }
348
349  box.center += (box.axis[0] * centerOffset[0]);
350  box.center += (box.axis[1] * centerOffset[1]);
351  box.center += (box.axis[2] * centerOffset[2]);
352
353
354  PRINTF(4)("\n");
355  PRINT(4)("\tAxis halflength x: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[0], maxLength[0], minLength[0], centerOffset[0]);
356  PRINT(4)("\tAxis halflength y: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[1], maxLength[1], minLength[1], centerOffset[1] );
357  PRINT(4)("\tAxis halflength z: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[2], maxLength[2], minLength[2], centerOffset[2]);
358}
359
360
361
362/**
363 *  this separates an ob-box in the middle
364 * @param box: the box to separate
365 *
366 * this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
367 */
368void OBBTreeNode::forkBox(OBB& box)
369{
370
371  PRINTF(4)("Fork Box\n");
372  PRINTF(4)("Calculating the longest Axis\n");
373  /* get the longest axis of the box */
374  float               longestAxis = -1.0f;                 //!< the length of the longest axis
375  int                 longestAxisIndex = 0;                //!< this is the nr of the longest axis
376
377
378  /* now get the longest axis of the three exiting */
379  for( int i = 0; i < 3; ++i)
380  {
381    if( longestAxis < box.halfLength[i])
382    {
383      longestAxis = box.halfLength[i];
384      longestAxisIndex = i;
385    }
386  }
387  this->bvElement->radius = longestAxis;
388  PRINTF(4)("\nLongest Axis is: Nr %i with a half-length of:%11.2f\n", longestAxisIndex, longestAxis);
389
390
391  PRINTF(4)("Separating along the longest axis\n");
392  /* get the closest vertex near the center */
393  float               tmpDist;                             //!< variable to save diverse distances temporarily
394  Plane               middlePlane(box.axis[longestAxisIndex], box.center); //!< the middle plane
395
396
397  /* now definin the separation plane through this specified nearest point and partition
398  the points depending on which side they are located
399  */
400  std::list<int>           partition1;                           //!< the vertex partition 1
401  std::list<int>           partition2;                           //!< the vertex partition 2
402  float*                   triangleCenter = new float[3];        //!< the center of the triangle
403  const float*             a;                                    //!< triangle  edge a
404  const float*             b;                                    //!< triangle  edge b
405  const float*             c;                                    //!< triangle  edge c
406
407
408  /* find the center of the box */
409  this->separationPlane = Plane(box.axis[longestAxisIndex], box.center);
410  this->sepPlaneCenter[0] = box.center.x;
411  this->sepPlaneCenter[1] = box.center.y;
412  this->sepPlaneCenter[2] = box.center.z;
413  this->longestAxisIndex = longestAxisIndex;
414
415  for( int i = 0; i < box.triangleIndexesLength; ++i)
416  {
417    /* first calculate the middle of the triangle */
418    a = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[0]];
419    b = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[1]];
420    c = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[2]];
421
422    triangleCenter[0] = (a[0] + b[0] + c[0]) / 3.0f;
423    triangleCenter[1] = (a[1] + b[1] + c[1]) / 3.0f;
424    triangleCenter[2] = (a[2] + b[2] + c[2]) / 3.0f;
425    tmpDist = this->separationPlane.distancePoint(*((sVec3D*)triangleCenter));
426
427    if( tmpDist > 0.0f)
428      partition1.push_back(box.triangleIndexes[i]); /* positive numbers plus zero */
429    else if( tmpDist < 0.0f)
430      partition2.push_back(box.triangleIndexes[i]); /* negatice numbers */
431    else {
432      partition1.push_back(box.triangleIndexes[i]); /* 0.0f? unprobable... */
433      partition2.push_back(box.triangleIndexes[i]);
434    }
435  }
436  PRINTF(4)("\nPartition1: got \t%i Vertices \nPartition2: got \t%i Vertices\n", partition1.size(), partition2.size());
437
438
439  /* now comes the separation into two different sVec3D arrays */
440  int                index;                                //!< index storage place
441  int*               triangleIndexList1;                   //!< the vertex list 1
442  int*               triangleIndexList2;                   //!< the vertex list 2
443  std::list<int>::iterator element;                        //!< the list iterator
444
445  triangleIndexList1 = new int[partition1.size()];
446  triangleIndexList2 = new int[partition2.size()];
447
448  for( element = partition1.begin(), index = 0; element != partition1.end(); element++, index++)
449    triangleIndexList1[index] = (*element);
450
451  for( element = partition2.begin(), index = 0; element != partition2.end(); element++, index++)
452    triangleIndexList2[index] = (*element);
453
454  if( this->triangleIndexList1!= NULL)
455    delete[] this->triangleIndexList1;
456  this->triangleIndexList1 = triangleIndexList1;
457  this->triangleIndexLength1 = partition1.size();
458
459  if( this->triangleIndexList2 != NULL)
460    delete[] this->triangleIndexList2;
461  this->triangleIndexList2 = triangleIndexList2;
462  this->triangleIndexLength2 = partition2.size();
463}
464
465
466
467/**
468 * collides one tree with an other
469 *  @param treeNode the other bv tree node
470 *  @param nodeA  the worldentity belonging to this bv
471 *  @param nodeB the worldentity belonging to treeNode
472 */
473void OBBTreeNode::collideWith(BVTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB)
474{
475  if( unlikely(treeNode == NULL || nodeA == NULL || nodeB == NULL))
476    return;
477
478  float distanceMax = this->bvElement->radius + ((OBBTreeNode*)treeNode)->bvElement->radius;
479  float distance = fabs((nodeA->getAbsCoor() - nodeB->getAbsCoor()).len());
480
481
482//   if( distance < distanceMax)
483//     PRINTF(0)(" %s (%s: group %i) vs %s (%s: group %i): distanceMax: %f, distance: %f\n", nodeA->getClassName(), nodeA->getName(), nodeA->getOMListNumber(), nodeB->getClassName(),  nodeB->getName(), nodeB->getOMListNumber(), distanceMax, distance);
484
485
486  PRINTF(4)("collideWith\n");
487  PRINTF(5)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex());
488
489  // for now only collide with OBBTreeNodes
490  this->collideWithOBB((OBBTreeNode*)treeNode, nodeA, nodeB);
491}
492
493
494
495/**
496 * collides one obb tree with an other
497 *  @param treeNode the other bv tree node
498 *  @param nodeA  the worldentity belonging to this bv
499 *  @param nodeB the worldentity belonging to treeNode
500 */
501void OBBTreeNode::collideWithOBB(OBBTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB)
502{
503
504  if( this->overlapTest(this->bvElement, treeNode->bvElement, nodeA, nodeB))
505  {
506    PRINTF(5)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight);
507
508
509    // left node
510    if( this->nodeLeft != NULL )
511    {
512      if( this->overlapTest(this->nodeLeft->bvElement, treeNode->bvElement, nodeA, nodeB))
513      {
514        bool bAdvance = false;
515        if( treeNode->nodeLeft != NULL)
516          this->nodeLeft->collideWith(treeNode->nodeLeft, nodeA, nodeB);
517        else
518          bAdvance = true;
519
520        if( treeNode->nodeRight != NULL)
521          this->nodeLeft->collideWith(treeNode->nodeRight, nodeA, nodeB);
522        else
523          bAdvance = true;
524
525        if( bAdvance)
526          this->nodeLeft->collideWith(treeNode, nodeA, nodeB);  // go down the other tree also
527      }
528    }
529
530    // right node
531    if( this->nodeRight != NULL )
532    {
533      if( this->overlapTest(this->nodeRight->bvElement, treeNode->bvElement, nodeA, nodeB))
534      {
535        bool bAdvance = false;
536
537        if( treeNode->nodeLeft != NULL)
538          this->nodeRight->collideWith(treeNode->nodeLeft, nodeA, nodeB);
539        else
540          bAdvance = true;
541
542        if( treeNode->nodeRight != NULL)
543          this->nodeRight->collideWith(treeNode->nodeRight, nodeA, nodeB);
544        else
545          bAdvance = true;
546
547        if( bAdvance)
548          this->nodeRight->collideWith(treeNode, nodeA, nodeB);  // go down the other tree also
549      }
550    }
551
552
553    // hybrid mode: we reached the end of this obbtree, now reach the end of the other tree
554    if( this->nodeLeft == NULL && this->nodeRight == NULL)
555    {
556      if( treeNode->nodeLeft != NULL)
557        this->collideWith(treeNode->nodeLeft, nodeA, nodeB);
558      if( treeNode->nodeRight != NULL)
559        this->collideWith(treeNode->nodeRight, nodeA, nodeB);
560    }
561
562
563    // now check if we reached the end of both trees
564    if( unlikely((this->nodeRight == NULL && this->nodeLeft == NULL) &&
565        (treeNode->nodeRight == NULL && treeNode->nodeLeft == NULL)) )
566    {
567//       PRINTF(0)("----------------------------------------------\n\n\n\n\n\n--------------------------------\n\n\n");
568      nodeA->registerCollision(nodeA, nodeB, (BoundingVolume*)this->bvElement, (BoundingVolume*)treeNode->bvElement);
569    }
570
571  }
572}
573
574
575/**
576 * this actualy checks if one obb box touches the other
577 * @param boxA the box from nodeA
578 * @param boxB the box from nodeB
579 * @param nodeA the node itself
580 * @param nodeB the node itself
581 */
582bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, WorldEntity* nodeA, WorldEntity* nodeB)
583{
584
585  //HACK remove this again
586  this->owner = nodeA;
587  //   if( boxB == NULL || boxA == NULL)
588  //     return false;
589
590  /* first check all axis */
591  Vector      t;
592  float       rA = 0.0f;
593  float       rB = 0.0f;
594  Vector      l;
595  Vector      rotAxisA[3];
596  Vector      rotAxisB[3];
597
598  rotAxisA[0] =  nodeA->getAbsDir().apply(boxA->axis[0]);
599  rotAxisA[1] =  nodeA->getAbsDir().apply(boxA->axis[1]);
600  rotAxisA[2] =  nodeA->getAbsDir().apply(boxA->axis[2]);
601
602  rotAxisB[0] =  nodeB->getAbsDir().apply(boxB->axis[0]);
603  rotAxisB[1] =  nodeB->getAbsDir().apply(boxB->axis[1]);
604  rotAxisB[2] =  nodeB->getAbsDir().apply(boxB->axis[2]);
605
606  t = nodeA->getAbsCoor() + nodeA->getAbsDir().apply(boxA->center) - ( nodeB->getAbsCoor() + nodeB->getAbsDir().apply(boxB->center));
607
608  /* All 3 axis of the object A */
609  for( int j = 0; j < 3; ++j)
610  {
611    rA = 0.0f;
612    rB = 0.0f;
613    l = rotAxisA[j];
614
615    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
616    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
617    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
618
619    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
620    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
621    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
622
623    PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
624
625    if( (rA + rB) < fabs(t.dot(l)))
626    {
627      PRINTF(4)("no Collision\n");
628      return false;
629    }
630  }
631
632  /* All 3 axis of the object B */
633  for( int j = 0; j < 3; ++j)
634  {
635    rA = 0.0f;
636    rB = 0.0f;
637    l = rotAxisB[j];
638
639    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
640    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
641    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
642
643    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
644    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
645    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
646
647    PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
648
649    if( (rA + rB) < fabs(t.dot(l)))
650    {
651      PRINTF(4)("no Collision\n");
652      return false;
653    }
654  }
655
656
657  /* Now check for all face cross products */
658
659  for( int j = 0; j < 3; ++j)
660  {
661    for(int k = 0; k < 3; ++k )
662    {
663      rA = 0.0f;
664      rB = 0.0f;
665      l = rotAxisA[j].cross(rotAxisB[k]);
666
667      rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
668      rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
669      rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
670
671      rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
672      rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
673      rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
674
675      PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
676
677      if( (rA + rB) < fabs(t.dot(l)))
678      {
679        PRINTF(4)("keine Kollision\n");
680        return false;
681      }
682    }
683  }
684
685  /* FIXME: there is no collision mark set now */
686     boxA->bCollided = true; /* use this ONLY(!!!!) for drawing operations */
687     boxB->bCollided = true;
688
689
690  PRINTF(4)("Kollision!\n");
691  return true;
692}
693
694
695/**
696 *
697 * draw the BV tree - debug mode
698 */
699void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color,  bool top) const
700{
701
702  /* this function can be used to draw the triangles and/or the points only  */
703  if( 1 /*drawMode & DRAW_MODEL || drawMode & DRAW_ALL*/)
704  {
705    if( depth == 0/*!(drawMode & DRAW_SINGLE && depth != 0)*/)
706    {
707      if( 0 /*drawMode & DRAW_POINTS*/)
708      {
709        glBegin(GL_POINTS);
710        glColor3f(0.3, 0.8, 0.54);
711        for(unsigned int i = 0; i < this->bvElement->modelInf->numVertices*3; i+=3)
712          glVertex3f(this->bvElement->modelInf->pVertices[i],
713                     this->bvElement->modelInf->pVertices[i+1],
714                     this->bvElement->modelInf->pVertices[i+2]);
715        glEnd();
716      }
717    }
718  }
719
720  if (top)
721  {
722    glPushAttrib(GL_ENABLE_BIT);
723    glDisable(GL_LIGHTING);
724    glDisable(GL_TEXTURE_2D);
725  }
726  glColor3f(color.x, color.y, color.z);
727
728
729  /* draw world axes */
730//   if( 1 /*drawMode & DRAW_BV_AXIS*/)
731//   {
732//     glBegin(GL_LINES);
733//     glColor3f(1.0, 0.0, 0.0);
734//     glVertex3f(0.0, 0.0, 0.0);
735//     glVertex3f(3.0, 0.0, 0.0);
736//
737//     glColor3f(0.0, 1.0, 0.0);
738//     glVertex3f(0.0, 0.0, 0.0);
739//     glVertex3f(0.0, 3.0, 0.0);
740//
741//     glColor3f(0.0, 0.0, 1.0);
742//     glVertex3f(0.0, 0.0, 0.0);
743//     glVertex3f(0.0, 0.0, 3.0);
744//     glEnd();
745//   }
746
747
748  if( 1/*drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL*/)
749  {
750    if( 1/*drawMode & DRAW_SINGLE && depth != 0*/)
751    {
752      /* draw the obb axes */
753      glBegin(GL_LINES);
754      glColor3f(1.0, 0.0, 0.0);
755      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
756      glVertex3f(this->bvElement->center.x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
757                 this->bvElement->center.y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
758                 this->bvElement->center.z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
759
760      glColor3f(0.0, 1.0, 0.0);
761      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
762      glVertex3f(this->bvElement->center.x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
763                 this->bvElement->center.y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
764                 this->bvElement->center.z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
765
766      glColor3f(0.0, 0.0, 1.0);
767      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
768      glVertex3f(this->bvElement->center.x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
769                 this->bvElement->center.y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
770                 this->bvElement->center.z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
771      glEnd();
772    }
773  }
774
775
776  /* DRAW POLYGONS */
777  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
778  {
779    if (top)
780    {
781      glEnable(GL_BLEND);
782      glBlendFunc(GL_SRC_ALPHA, GL_ONE);
783    }
784
785    if( this->nodeLeft == NULL && this->nodeRight == NULL)
786      depth = 0;
787
788    if( depth == 0 /*!(drawMode & DRAW_SINGLE && depth != 0)*/)
789    {
790
791
792      Vector cen = this->bvElement->center;
793      Vector* axis = this->bvElement->axis;
794      float* len = this->bvElement->halfLength;
795
796      if( this->bvElement->bCollided)
797      {
798        glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR
799      }
800      else if( drawMode & DRAW_BV_BLENDED)
801      {
802        glColor4f(color.x, color.y, color.z, .5);
803      }
804
805      // debug out
806      if( this->obbTree->getOwner() != NULL)
807      {
808        PRINTF(4)("debug poly draw: depth: %i, mode: %i, entity-name: %s, class: %s\n", depth, drawMode, this->obbTree->getOwner()->getName(), this->obbTree->getOwner()->getClassName());
809      }
810      else
811        PRINTF(4)("debug poly draw: depth: %i, mode: %i\n", depth, drawMode);
812
813
814      /* draw bounding box */
815      if( drawMode & DRAW_BV_BLENDED)
816        glBegin(GL_QUADS);
817      else
818        glBegin(GL_LINE_LOOP);
819      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
820                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
821                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
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      glEnd();
832
833      if( drawMode & DRAW_BV_BLENDED)
834        glBegin(GL_QUADS);
835      else
836        glBegin(GL_LINE_LOOP);
837      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
838                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
839                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
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      glEnd();
850
851      if( drawMode & DRAW_BV_BLENDED)
852        glBegin(GL_QUADS);
853      else
854        glBegin(GL_LINE_LOOP);
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      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      glEnd();
868
869      if( drawMode & DRAW_BV_BLENDED)
870        glBegin(GL_QUADS);
871      else
872        glBegin(GL_LINE_LOOP);
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      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      glEnd();
886
887
888      if( drawMode & DRAW_BV_BLENDED)
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        glBegin(GL_QUADS);
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        glColor3f(color.x, color.y, color.z);
923    }
924  }
925
926  /* DRAW SEPARATING PLANE */
927  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
928  {
929    if( !(drawMode & DRAW_SINGLE && depth != 0))
930    {
931      if( drawMode & DRAW_BV_BLENDED)
932        glColor4f(color.x, color.y, color.z, .6);
933
934      /* now draw the separation plane */
935      Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
936      Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
937      Vector c = this->bvElement->center;
938      float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
939      float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
940      glBegin(GL_QUADS);
941      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);
942      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);
943      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);
944      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);
945      glEnd();
946
947      if( drawMode & DRAW_BV_BLENDED)
948        glColor4f(color.x, color.y, color.z, 1.0);
949
950    }
951  }
952
953
954
955  if (depth > 0)
956  {
957    if( this->nodeLeft != NULL)
958      this->nodeLeft->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(15.0,0.0,0.0)), false);
959    if( this->nodeRight != NULL)
960      this->nodeRight->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(30.0,0.0,0.0)), false);
961  }
962  this->bvElement->bCollided = false;
963
964  if (top)
965    glPopAttrib();
966}
967
968
969
970void OBBTreeNode::debug() const
971{
972  PRINT(0)("========OBBTreeNode::debug()=====\n");
973  PRINT(0)(" Current depth: %i", this->depth);
974  PRINT(0)(" ");
975  PRINT(0)("=================================\n");
976}
Note: See TracBrowser for help on using the repository browser.