Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cd/src/lib/collision_detection/obb_tree_node.cc @ 7666

Last change on this file since 7666 was 7666, checked in by patrick, 18 years ago

cd: found the modelInfo bug, classical vertex index problem :D obb still not correct, but close to…

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