Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cd: obb tree gets spawned again

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