Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cd: now the obb fits perfectly for the first time

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