Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: the new collision detection seems to be much more accurate than the old. There is still a little issue with the detection

File size: 35.5 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(4)("\n==============================Creating OBB Tree Node==================\n");
110  PRINT(4)(" OBB Tree Infos: \n");
111  PRINT(4)("\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(4)("\nOBB Covariance Matrix:\n");
242  for(int j = 0; j < 3; ++j)
243  {
244    PRINT(4)("\t\t");
245    for(int k = 0; k < 3; ++k)
246    {
247      PRINT(4)("%11.4f\t", covariance[j][k]);
248    }
249    PRINT(4)("\n");
250  }
251  PRINTF(4)("\nWeighteed OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", center.x, center.y, center.z);
252}
253
254
255
256/**
257 *  calculate the eigenvectors for the object oriented box
258 * @param box: reference to the box
259 * @param modelInf: the model info structure of the model
260 * @param tirangleIndexes: an array with the indexes of the triangles inside this
261 * @param length: the length of the indexes array
262 */
263void OBBTreeNode::calculateBoxEigenvectors(OBB& box, const modelInfo& modelInf,
264    const int* triangleIndexes, int length)
265{
266
267  Vector         axis[3];                            //!< the references to the obb axis
268  Matrix         covMat(  box.covarianceMatrix  );   //!< covariance matrix (in the matrix dataform)
269
270  /*
271  now getting spanning vectors of the sub-space:
272  the eigenvectors of a symmertric matrix, such as the
273  covarience matrix are mutually orthogonal.
274  after normalizing them, they can be used as a the basis
275  vectors
276  */
277
278  /* calculate the axis */
279  covMat.getEigenVectors(axis[0], axis[1], axis[2] );
280  box.axis[0] = axis[0];
281  box.axis[1] = axis[1];
282  box.axis[2] = axis[2];
283
284  // this is for axis aligned bouning boxes only
285//   box.axis[0] = Vector(1,0,0);
286//   box.axis[1] = Vector(0,1,0);
287//   box.axis[2] = Vector(0,0,1);
288
289  PRINTF(4)("Eigenvectors:\n");
290  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);
291  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);
292  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);
293}
294
295
296
297
298/**
299 *  calculate the eigenvectors for the object oriented box
300 * @param box: reference to the box
301 * @param modelInf: the model info structure of the model
302 * @param tirangleIndexes: an array with the indexes of the triangles inside this
303 * @param length: the length of the indexes array
304 */
305void OBBTreeNode::calculateBoxAxis(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, int length)
306{
307
308  PRINTF(4)("Calculate Box Axis\n");
309  /* now get the axis length */
310  float               halfLength[3];                         //!< half length of the axis
311  float               tmpLength;                             //!< tmp save point for the length
312  Plane               p0(box.axis[0], box.center);           //!< the axis planes
313  Plane               p1(box.axis[1], box.center);           //!< the axis planes
314  Plane               p2(box.axis[2], box.center);           //!< the axis planes
315  float               maxLength[3];                          //!< maximal lenth of the axis
316  float               minLength[3];                          //!< minimal length of the axis
317  const float*        tmpVec;                                //!< variable taking tmp vectors
318  float               centerOffset[3];
319
320  /* get the maximal dimensions of the body in all directions */
321  /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */
322  for( int k = 0; k  < 3; k++)
323  {
324    tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]);
325    Plane* p;
326    if( k == 0)
327      p = &p0;
328    else if( k == 1)
329      p = &p1;
330    else
331      p = &p2;
332    maxLength[k] = p->distancePoint(tmpVec);
333    minLength[k] = p->distancePoint(tmpVec);
334
335    for( int j = 0; j < length; ++j) {
336      for( int i = 0; i < 3; ++i) {
337        tmpVec = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]];
338        tmpLength = p->distancePoint(tmpVec);
339
340        if( tmpLength > maxLength[k])
341          maxLength[k] = tmpLength;
342        else if( tmpLength < minLength[k])
343          minLength[k] = tmpLength;
344      }
345    }
346  }
347
348
349
350  /* calculate the real centre of the body by using the axis length */
351  for( int i = 0; i < 3; ++i)
352  {
353    if( maxLength[i] > 0.0f && minLength[i] > 0.0f)  // both axis positiv
354      centerOffset[i] = minLength[i] + (maxLength[i] - minLength[i]) / 2.0f;
355    else if( maxLength[i] > 0.0f && maxLength[i] < 0.0f) // positiv and negativ
356      centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f;
357    else //both negativ
358     centerOffset[i] = minLength[i] + (maxLength[i] - minLength[i]) / 2.0f;
359
360    box.halfLength[i] = (maxLength[i] - minLength[i]) / 2.0f;
361  }
362
363  box.center += (box.axis[0] * centerOffset[0]);
364  box.center += (box.axis[1] * centerOffset[1]);
365  box.center += (box.axis[2] * centerOffset[2]);
366
367
368  PRINTF(4)("\n");
369  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]);
370  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] );
371  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]);
372}
373
374
375
376/**
377 *  this separates an ob-box in the middle
378 * @param box: the box to separate
379 *
380 * this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
381 */
382void OBBTreeNode::forkBox(OBB& box)
383{
384
385  PRINTF(4)("Fork Box\n");
386  PRINTF(4)("Calculating the longest Axis\n");
387  /* get the longest axis of the box */
388  float               longestAxis = -1.0f;                 //!< the length of the longest axis
389  int                 longestAxisIndex = 0;                //!< this is the nr of the longest axis
390
391
392  /* now get the longest axis of the three exiting */
393  for( int i = 0; i < 3; ++i)
394  {
395    if( longestAxis < box.halfLength[i])
396    {
397      longestAxis = box.halfLength[i];
398      longestAxisIndex = i;
399    }
400  }
401  PRINTF(4)("\nLongest Axis is: Nr %i with a half-length of:%11.2f\n", longestAxisIndex, longestAxis);
402
403
404  PRINTF(4)("Separating along the longest axis\n");
405  /* get the closest vertex near the center */
406  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
407  float               tmpDist;                             //!< variable to save diverse distances temporarily
408  int                 vertexIndex;                         //!< index of the vertex near the center
409  Plane               middlePlane(box.axis[longestAxisIndex], box.center); //!< the middle plane
410  const sVec3D*       tmpVec;                              //!< temp simple 3D vector
411
412
413  /* now definin the separation plane through this specified nearest point and partition
414  the points depending on which side they are located
415  */
416  std::list<int>           partition1;                           //!< the vertex partition 1
417  std::list<int>           partition2;                           //!< the vertex partition 2
418  float*                   triangleCenter = new float[3];        //!< the center of the triangle
419  const float*             a;                                    //!< triangle  edge a
420  const float*             b;                                    //!< triangle  edge b
421  const float*             c;                                    //!< triangle  edge c
422
423
424  /* find the center of the box */
425  this->separationPlane = Plane(box.axis[longestAxisIndex], box.center);
426  this->sepPlaneCenter[0] = box.center.x;
427  this->sepPlaneCenter[1] = box.center.y;
428  this->sepPlaneCenter[2] = box.center.z;
429  this->longestAxisIndex = longestAxisIndex;
430
431  for( int i = 0; i < box.triangleIndexesLength; ++i)
432  {
433    /* first calculate the middle of the triangle */
434    a = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[0]];
435    b = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[1]];
436    c = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[2]];
437
438    triangleCenter[0] = (a[0] + b[0] + c[0]) / 3.0f;
439    triangleCenter[1] = (a[1] + b[1] + c[1]) / 3.0f;
440    triangleCenter[2] = (a[2] + b[2] + c[2]) / 3.0f;
441    tmpDist = this->separationPlane.distancePoint(*((sVec3D*)triangleCenter));
442
443    if( tmpDist > 0.0f)
444      partition1.push_back(box.triangleIndexes[i]); /* positive numbers plus zero */
445    else if( tmpDist < 0.0f)
446      partition2.push_back(box.triangleIndexes[i]); /* negatice numbers */
447    else {
448      partition1.push_back(box.triangleIndexes[i]); /* 0.0f? unprobable... */
449      partition2.push_back(box.triangleIndexes[i]);
450    }
451  }
452  PRINTF(4)("\nPartition1: got \t%i Vertices \nPartition2: got \t%i Vertices\n", partition1.size(), partition2.size());
453
454
455  /* now comes the separation into two different sVec3D arrays */
456  int                index;                                //!< index storage place
457  int*               triangleIndexList1;                   //!< the vertex list 1
458  int*               triangleIndexList2;                   //!< the vertex list 2
459  std::list<int>::iterator element;                        //!< the list iterator
460
461  triangleIndexList1 = new int[partition1.size()];
462  triangleIndexList2 = new int[partition2.size()];
463
464  for( element = partition1.begin(), index = 0; element != partition1.end(); element++, index++)
465    triangleIndexList1[index] = (*element);
466
467  for( element = partition2.begin(), index = 0; element != partition2.end(); element++, index++)
468    triangleIndexList2[index] = (*element);
469
470  if( this->triangleIndexList1!= NULL)
471    delete[] this->triangleIndexList1;
472  this->triangleIndexList1 = triangleIndexList1;
473  this->triangleIndexLength1 = partition1.size();
474
475  if( this->triangleIndexList2 != NULL)
476    delete[] this->triangleIndexList2;
477  this->triangleIndexList2 = triangleIndexList2;
478  this->triangleIndexLength2 = partition2.size();
479}
480
481
482
483/**
484 * collides one tree with an other
485 *  @param treeNode the other bv tree node
486 *  @param nodeA  the worldentity belonging to this bv
487 *  @param nodeB the worldentity belonging to treeNode
488 */
489void OBBTreeNode::collideWith(BVTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB)
490{
491  if( unlikely(treeNode == NULL || nodeA == NULL || nodeB == NULL))
492    return;
493
494
495  PRINTF(4)("collideWith\n");
496  PRINTF(5)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex());
497
498  // for now only collide with OBBTreeNodes
499  this->collideWithOBB((OBBTreeNode*)treeNode, nodeA, nodeB);
500}
501
502
503
504/**
505 * collides one obb tree with an other
506 *  @param treeNode the other bv tree node
507 *  @param nodeA  the worldentity belonging to this bv
508 *  @param nodeB the worldentity belonging to treeNode
509 */
510void OBBTreeNode::collideWithOBB(OBBTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB)
511{
512
513  if( this->overlapTest(this->bvElement, treeNode->bvElement, nodeA, nodeB))
514  {
515    PRINTF(5)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight);
516
517    /* check if left node overlaps */
518    if( this->nodeLeft != NULL )
519    {
520      PRINTF(5)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode->getIndex());
521      if( this->overlapTest(this->nodeLeft->bvElement, treeNode->bvElement, nodeA, nodeB))
522      {
523        this->nodeLeft->collideWith(treeNode->nodeLeft, nodeA, nodeB);
524        this->nodeLeft->collideWith(treeNode->nodeRight, nodeA, nodeB);
525      }
526    }
527    /* check if right node overlaps */
528    if( likely( this->nodeRight != NULL))
529    {
530      PRINTF(5)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode->getIndex());
531      if(this->overlapTest(this->nodeRight->bvElement, treeNode->bvElement, nodeA, nodeB))
532      {
533        this->nodeRight->collideWith(treeNode->nodeLeft, nodeA, nodeB);
534        this->nodeRight->collideWith(treeNode->nodeRight, nodeA, nodeB);
535      }
536    }
537
538    /* so there is a collision and this is the last box in the tree (i.e. leaf) */
539    if( unlikely((this->nodeRight == NULL || this->nodeLeft == NULL) ||
540                 (treeNode->nodeRight == NULL || treeNode->nodeLeft == NULL)) )
541    {
542      nodeA->collidesWith(nodeB, treeNode->bvElement->center);
543      nodeB->collidesWith(nodeA, this->bvElement->center);
544    }
545
546  }
547}
548
549
550/**
551 * this actualy checks if one obb box touches the other
552 * @param boxA the box from nodeA
553 * @param boxB the box from nodeB
554 * @param nodeA the node itself
555 * @param nodeB the node itself
556 */
557bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, WorldEntity* nodeA, WorldEntity* nodeB)
558{
559  //HACK remove this again
560  this->owner = nodeA;
561  //   if( boxB == NULL || boxA == NULL)
562  //     return false;
563
564  /* first check all axis */
565  Vector      t;
566  float       rA = 0.0f;
567  float       rB = 0.0f;
568  Vector      l;
569  Vector      rotAxisA[3];
570  Vector      rotAxisB[3];
571
572  rotAxisA[0] =  nodeA->getAbsDir().apply(boxA->axis[0]);
573  rotAxisA[1] =  nodeA->getAbsDir().apply(boxA->axis[1]);
574  rotAxisA[2] =  nodeA->getAbsDir().apply(boxA->axis[2]);
575
576  rotAxisB[0] =  nodeB->getAbsDir().apply(boxB->axis[0]);
577  rotAxisB[1] =  nodeB->getAbsDir().apply(boxB->axis[1]);
578  rotAxisB[2] =  nodeB->getAbsDir().apply(boxB->axis[2]);
579
580  t = nodeA->getAbsCoor() + nodeA->getAbsDir().apply(boxA->center) - ( nodeB->getAbsCoor() + nodeB->getAbsDir().apply(boxB->center));
581
582  /* All 3 axis of the object A */
583  for( int j = 0; j < 3; ++j)
584  {
585    rA = 0.0f;
586    rB = 0.0f;
587    l = rotAxisA[j];
588
589    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
590    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
591    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
592
593    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
594    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
595    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
596
597    PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
598
599    if( (rA + rB) < fabs(t.dot(l)))
600    {
601      PRINTF(4)("no Collision\n");
602      return false;
603    }
604  }
605
606  /* All 3 axis of the object B */
607  for( int j = 0; j < 3; ++j)
608  {
609    rA = 0.0f;
610    rB = 0.0f;
611    l = rotAxisB[j];
612
613    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
614    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
615    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
616
617    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
618    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
619    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
620
621    PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
622
623    if( (rA + rB) < fabs(t.dot(l)))
624    {
625      PRINTF(4)("no Collision\n");
626      return false;
627    }
628  }
629
630
631  /* Now check for all face cross products */
632
633  for( int j = 0; j < 3; ++j)
634  {
635    for(int k = 0; k < 3; ++k )
636    {
637      rA = 0.0f;
638      rB = 0.0f;
639      l = rotAxisA[j].cross(rotAxisB[k]);
640
641      rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
642      rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
643      rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
644
645      rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
646      rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
647      rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
648
649      PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
650
651      if( (rA + rB) < fabs(t.dot(l)))
652      {
653        PRINTF(4)("keine Kollision\n");
654        return false;
655      }
656    }
657  }
658
659  /* FIXME: there is no collision mark set now */
660     boxA->bCollided = true; /* use this ONLY(!!!!) for drawing operations */
661     boxB->bCollided = true;
662
663
664  PRINTF(4)("Kollision!\n");
665  return true;
666}
667
668
669/**
670 *
671 * draw the BV tree - debug mode
672 */
673void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color,  bool top) const
674{
675  /* this function can be used to draw the triangles and/or the points only  */
676  if( 1 /*drawMode & DRAW_MODEL || drawMode & DRAW_ALL*/)
677  {
678    if( depth == 0/*!(drawMode & DRAW_SINGLE && depth != 0)*/)
679    {
680      if( 1 /*drawMode & DRAW_POINTS*/)
681      {
682        glBegin(GL_POINTS);
683        glColor3f(0.3, 0.8, 0.54);
684        for( int i = 0; i < this->bvElement->modelInf->numVertices*3; i+=3)
685          glVertex3f(this->bvElement->modelInf->pVertices[i],
686                     this->bvElement->modelInf->pVertices[i+1],
687                     this->bvElement->modelInf->pVertices[i+2]);
688        glEnd();
689      }
690    }
691  }
692
693  if (top)
694  {
695    glPushAttrib(GL_ENABLE_BIT);
696    glDisable(GL_LIGHTING);
697    glDisable(GL_TEXTURE_2D);
698  }
699  glColor3f(color.x, color.y, color.z);
700
701
702  /* draw world axes */
703  if( 1 /*drawMode & DRAW_BV_AXIS*/)
704  {
705    glBegin(GL_LINES);
706    glColor3f(1.0, 0.0, 0.0);
707    glVertex3f(0.0, 0.0, 0.0);
708    glVertex3f(3.0, 0.0, 0.0);
709
710    glColor3f(0.0, 1.0, 0.0);
711    glVertex3f(0.0, 0.0, 0.0);
712    glVertex3f(0.0, 3.0, 0.0);
713
714    glColor3f(0.0, 0.0, 1.0);
715    glVertex3f(0.0, 0.0, 0.0);
716    glVertex3f(0.0, 0.0, 3.0);
717    glEnd();
718  }
719
720
721  if( 1/*drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL*/)
722  {
723    if( 1/*drawMode & DRAW_SINGLE && depth != 0*/)
724    {
725      /* draw the obb axes */
726      glBegin(GL_LINES);
727      glColor3f(1.0, 0.0, 0.0);
728      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
729      glVertex3f(this->bvElement->center.x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
730                 this->bvElement->center.y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
731                 this->bvElement->center.z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
732
733      glColor3f(0.0, 1.0, 0.0);
734      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
735      glVertex3f(this->bvElement->center.x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
736                 this->bvElement->center.y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
737                 this->bvElement->center.z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
738
739      glColor3f(0.0, 0.0, 1.0);
740      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
741      glVertex3f(this->bvElement->center.x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
742                 this->bvElement->center.y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
743                 this->bvElement->center.z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
744      glEnd();
745    }
746  }
747
748
749  /* DRAW POLYGONS */
750  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
751  {
752    if (top)
753    {
754      glEnable(GL_BLEND);
755      glBlendFunc(GL_SRC_ALPHA, GL_ONE);
756    }
757
758    if( this->nodeLeft == NULL && this->nodeRight == NULL)
759      depth = 0;
760
761    if( depth == 0 /*!(drawMode & DRAW_SINGLE && depth != 0)*/)
762    {
763
764
765      Vector cen = this->bvElement->center;
766      Vector* axis = this->bvElement->axis;
767      float* len = this->bvElement->halfLength;
768
769      if( this->bvElement->bCollided)
770      {
771        glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR
772      }
773      else if( drawMode & DRAW_BV_BLENDED)
774      {
775        glColor4f(color.x, color.y, color.z, .5);
776      }
777
778      // debug out
779      if( this->obbTree->getOwner() != NULL)
780      {
781        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());
782      }
783      else
784        PRINTF(4)("debug poly draw: depth: %i, mode: %i\n", depth, drawMode);
785
786
787      /* draw bounding box */
788      if( drawMode & DRAW_BV_BLENDED)
789        glBegin(GL_QUADS);
790      else
791        glBegin(GL_LINE_LOOP);
792      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
793                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
794                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
795      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
796                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
797                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
798      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
799                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
800                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
801      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
802                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
803                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
804      glEnd();
805
806      if( drawMode & DRAW_BV_BLENDED)
807        glBegin(GL_QUADS);
808      else
809        glBegin(GL_LINE_LOOP);
810      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
811                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
812                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
813      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
814                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
815                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
816      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
817                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
818                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
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      glEnd();
823
824      if( drawMode & DRAW_BV_BLENDED)
825        glBegin(GL_QUADS);
826      else
827        glBegin(GL_LINE_LOOP);
828      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
829                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
830                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
831      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
832                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
833                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
834      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
835                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
836                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
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      glEnd();
841
842      if( drawMode & DRAW_BV_BLENDED)
843        glBegin(GL_QUADS);
844      else
845        glBegin(GL_LINE_LOOP);
846      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
847                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
848                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
849      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
850                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
851                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
852      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
853                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
854                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
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      glEnd();
859
860
861      if( drawMode & DRAW_BV_BLENDED)
862      {
863        glBegin(GL_QUADS);
864        glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
865                   cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
866                   cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
867        glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
868                   cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
869                   cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
870        glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
871                   cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
872                   cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
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        glEnd();
877
878        glBegin(GL_QUADS);
879        glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
880                   cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
881                   cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
882        glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
883                   cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
884                   cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
885        glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
886                   cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
887                   cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
888        glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
889                   cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
890                   cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
891        glEnd();
892      }
893
894      if( drawMode & DRAW_BV_BLENDED)
895        glColor3f(color.x, color.y, color.z);
896    }
897  }
898
899  /* DRAW SEPARATING PLANE */
900  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
901  {
902    if( !(drawMode & DRAW_SINGLE && depth != 0))
903    {
904      if( drawMode & DRAW_BV_BLENDED)
905        glColor4f(color.x, color.y, color.z, .6);
906
907      /* now draw the separation plane */
908      Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
909      Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
910      Vector c = this->bvElement->center;
911      float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
912      float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
913      glBegin(GL_QUADS);
914      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);
915      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);
916      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);
917      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);
918      glEnd();
919
920      if( drawMode & DRAW_BV_BLENDED)
921        glColor4f(color.x, color.y, color.z, 1.0);
922
923    }
924  }
925
926
927
928  if (depth > 0)
929  {
930    if( this->nodeLeft != NULL)
931      this->nodeLeft->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(15.0,0.0,0.0)), false);
932    if( this->nodeRight != NULL)
933      this->nodeRight->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(30.0,0.0,0.0)), false);
934  }
935  this->bvElement->bCollided = false;
936
937  if (top)
938    glPopAttrib();
939}
940
941
942
943void OBBTreeNode::debug() const
944{
945  PRINT(0)("========OBBTreeNode::debug()=====\n");
946  PRINT(0)(" Current depth: %i", this->depth);
947  PRINT(0)(" ");
948  PRINT(0)("=================================\n");
949}
Note: See TracBrowser for help on using the repository browser.