Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5429 was 5429, checked in by patrick, 19 years ago

orxonox/trunk: bounding volumes now toggeable via shell

File size: 35.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION
17
18#include "obb_tree_node.h"
19#include "list.h"
20#include "obb.h"
21#include "obb_tree.h"
22#include "vector.h"
23#include "abstract_model.h"
24#include "world_entity.h"
25
26#include <math.h>
27
28#include "stdincl.h"
29
30#include "lin_alg.h"
31
32
33
34
35using namespace std;
36
37OBBTree*  OBBTreeNode::obbTree = NULL;
38
39float**  OBBTreeNode::coMat = NULL;
40float**  OBBTreeNode::eigvMat = NULL;
41float*   OBBTreeNode::eigvlMat = NULL;
42int*     OBBTreeNode::rotCount = NULL;
43
44/**
45 *  standard constructor
46 */
47OBBTreeNode::OBBTreeNode ()
48{
49  this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode");
50  this->nodeLeft = NULL;
51  this->nodeRight = NULL;
52  this->bvElement = NULL;
53
54  if(coMat == NULL)
55  {
56    coMat = new float*[4];
57    for(int i = 0; i < 4; i++)
58      coMat[i] = new float[4];
59  }
60  if(eigvMat == NULL)
61  {
62    eigvMat = new float*[4];
63    for(int i = 0; i < 4; i++)
64      eigvMat[i] = new float[4];
65  }
66  if( eigvlMat == NULL)
67  {
68    eigvlMat = new float[4];
69  }
70  if( rotCount == NULL)
71    rotCount = new int;
72
73  this->sphereObj = gluNewQuadric();
74}
75
76
77/**
78 *  standard deconstructor
79 */
80OBBTreeNode::~OBBTreeNode ()
81{
82  if( this->nodeLeft)
83  {
84    delete this->nodeLeft;
85    this->nodeLeft = NULL;
86  }
87  if( this->nodeRight)
88  {
89    delete this->nodeRight;
90    this->nodeRight = NULL;
91  }
92  if( this->bvElement)
93    delete this->bvElement;
94  this->bvElement = NULL;
95}
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 verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle
103 */
104void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length)
105{
106  PRINT(3)("\n");
107  this->treeIndex = this->obbTree->getID();
108  PRINTF(3)("OBB Depth: %i, tree index: %i, numVertices: %i\n", depth, treeIndex, length);
109  this->depth = depth;
110
111
112  this->bvElement = new OBB();
113  this->bvElement->vertices = verticesList;
114  this->bvElement->numOfVertices = length;
115  PRINTF(3)("Created OBBox\n");
116  this->calculateBoxCovariance(this->bvElement, verticesList, length);
117  PRINTF(3)("Calculated attributes1\n");
118  this->calculateBoxEigenvectors(this->bvElement, verticesList, length);
119  PRINTF(3)("Calculated attributes2\n");
120  this->calculateBoxAxis(this->bvElement, verticesList, length);
121  PRINTF(3)("Calculated attributes3\n");
122
123  /* if this is the first node, the vertices data are the original ones of the model itself, so dont delete them in cleanup */
124  if( this->treeIndex == 1)
125    this->bvElement->bOrigVertices = true;
126
127  if( likely( this->depth > 0))
128  {
129    this->forkBox(this->bvElement);
130
131
132    if(this->tmpLen1 > 2)
133    {
134      OBBTreeNode* node1 = new OBBTreeNode();
135      this->nodeLeft = node1;
136      this->nodeLeft->spawnBVTree(depth - 1, this->tmpVert1, this->tmpLen1);
137    }
138    else
139    {
140      PRINTF(3)("Aboarding tree walk: less than 3 vertices left\n");
141    }
142
143    if( this->tmpLen2 > 2)
144    {
145      OBBTreeNode* node2 = new OBBTreeNode();
146      this->nodeRight = node2;
147      this->nodeRight->spawnBVTree(depth - 1, this->tmpVert2, this->tmpLen2);
148    }
149    else
150    {
151      PRINTF(3)("Abording tree walk: less than 3 vertices left\n");
152    }
153
154  }
155}
156
157
158
159void OBBTreeNode::calculateBoxCovariance(OBB* box, sVec3D* verticesList, int length)
160{
161  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
162  float     face = 0.0f;                             //!< surface area of the entire convex hull
163  Vector    centroid[length];                        //!< centroid of the i'th convex hull
164  Vector    center;                                  //!< the center of the entire hull
165  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
166  Vector    t1, t2;                                  //!< temporary values
167  float     covariance[3][3];                        //!< the covariance matrix
168  int       mode = 0;                                //!< mode = 0: vertex soup, no connections, mode = 1: 3 following verteces build a triangle
169
170  this->numOfVertices = length;
171  this->vertices = verticesList;
172
173
174  if( likely(mode == 0))
175  {
176    /* fist compute all the convex hull face/facelets and centroids */
177    for( int i = 0; i+3 < length ; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
178    {
179      p = verticesList[i];
180      q = verticesList[i + 1];
181      r = verticesList[i + 2];
182
183      t1 = p - q; t2 = p - r;
184
185      /* finding the facelet surface via cross-product */
186      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
187      /* update the entire convex hull surface */
188      face += facelet[i];
189
190      /* calculate the cetroid of the hull triangles */
191      centroid[i] = (p + q + r) * 1/3;
192      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
193      center += centroid[i] * facelet[i];
194    }
195    /* take the average of the centroid sum */
196    center /= face;
197    PRINTF(3)("-- Calculated Center\n");
198
199
200    /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */
201    for( int j = 0; j < 3; ++j)
202    {
203      for( int k = 0; k < 3; ++k)
204      {
205        for( int i = 0; i + 3 < length; i+=3)
206        {
207          p = verticesList[i];
208          q = verticesList[i + 1];
209          r = verticesList[i + 2];
210
211          covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] +
212              q[j] * q[k] + r[j] * r[k]) - center[j] * center[k];
213        }
214      }
215    }
216    PRINTF(3)("-- Calculated Covariance\n");
217  }
218  else if( mode == 1)
219  {
220    for( int i = 0; i + 3 < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
221    {
222      p = verticesList[i];
223      q = verticesList[i + 1];
224      r = verticesList[i + 2];
225
226      centroid[i] = (p + q + r) / 3.0f;
227      center += centroid[i];
228    }
229    center /= length;
230
231    for( int j = 0; j < 3; ++j)
232    {
233      for( int k = 0; k < 3; ++k)
234      {
235        for( int i = 0; i + 3 < length; i+=3)
236        {
237          p = verticesList[i];
238          q = verticesList[i +1];
239          r = verticesList[i + 2];
240
241          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
242        }
243        covariance[j][k] /= (3.0f * length);
244      }
245    }
246    PRINTF(3)("-- Calculated Covariance\n");
247  }
248  else if( mode == 2)
249  {
250    /* fist compute all the convex hull face/facelets and centroids */
251    for(int i = 0; i + 3 < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
252    {
253      p = verticesList[i];
254      q = verticesList[i + 1];
255      r = verticesList[i + 2];
256
257      t1 = p - q; t2 = p - r;
258
259      /* finding the facelet surface via cross-product */
260      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
261      /* update the entire convex hull surface */
262      face += facelet[i];
263
264      /* calculate the cetroid of the hull triangles */
265      centroid[i] = (p + q + r) * 1/3;
266      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
267      center += centroid[i] * facelet[i];
268    }
269    /* take the average of the centroid sum */
270    center /= face;
271    PRINTF(3)("-- Calculated Center\n");
272
273    for( int j = 0; j < 3; ++j)
274    {
275      for( int k = 0; k < 3; ++k)
276      {
277        for( int i = 0; i + 3 < length; i+=3)
278        {
279          p = verticesList[i];
280          q = verticesList[i +1];
281          r = verticesList[i + 2];
282
283          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
284        }
285        covariance[j][k] /= (3.0f * length);
286      }
287    }
288    PRINTF(3)("-- Calculated Covariance\n");
289  }
290  else
291  {
292    for( int i = 0; i < length; ++i)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
293    {
294      center += verticesList[i];
295    }
296    center /= length;
297
298    for( int j = 0; j < 3; ++j)
299    {
300      for( int k = 0; k < 3; ++k)
301      {
302        for( int i = 0; i + 3 < length; i+=3)
303        {
304          p = verticesList[i];
305          q = verticesList[i +1];
306          r = verticesList[i + 2];
307
308          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
309        }
310        covariance[j][k] /= (3.0f * length);
311      }
312    }
313    PRINTF(3)("-- Calculated Covariance\n");
314  }
315
316  PRINTF(3)("\nVertex Data:\n");
317  for(int i = 0; i < length; i++)
318  {
319    PRINTF(3)("vertex %i: %f, %f, %f\n", i, box->vertices[i][0], box->vertices[i][1], box->vertices[i][2]);
320  }
321
322
323  PRINTF(3)("\nCovariance Matrix:\n");
324  for(int j = 0; j < 3; ++j)
325  {
326    PRINT(3)(" |");
327    for(int k = 0; k < 3; ++k)
328    {
329      PRINT(3)(" \b%f ", covariance[j][k]);
330    }
331    PRINT(3)(" |\n");
332  }
333
334  PRINTF(3)("center: %f, %f, %f\n", center.x, center.y, center.z);
335
336
337  for(int i = 0; i < 3; ++i)
338  {
339    box->covarianceMatrix[i][0] = covariance[i][0];
340    box->covarianceMatrix[i][1] = covariance[i][1];
341    box->covarianceMatrix[i][2] = covariance[i][2];
342  }
343  *box->center = center;
344  PRINTF(3)("-- Written Result to obb\n");
345}
346
347
348
349void OBBTreeNode::calculateBoxEigenvectors(OBB* box, sVec3D* verticesList, int length)
350{
351
352  /* now getting spanning vectors of the sub-space:
353  the eigenvectors of a symmertric matrix, such as the
354  covarience matrix are mutually orthogonal.
355  after normalizing them, they can be used as a the basis
356  vectors
357  */
358  Vector*              axis = new Vector[3];                //!< the references to the obb axis
359
360  coMat[1][1] = box->covarianceMatrix[0][0]; coMat[1][2] = box->covarianceMatrix[0][1]; coMat[1][3] = box->covarianceMatrix[0][2];
361  coMat[2][1] = box->covarianceMatrix[1][0]; coMat[2][2] = box->covarianceMatrix[1][1]; coMat[2][3] = box->covarianceMatrix[1][2];
362  coMat[3][1] = box->covarianceMatrix[2][0]; coMat[3][2] = box->covarianceMatrix[2][1]; coMat[3][3] = box->covarianceMatrix[2][2];
363
364//   OBBTreeNode::coMat[0][0] = box->covarianceMatrix[0][0];
365//   OBBTreeNode::coMat[0][1] = box->covarianceMatrix[0][1];
366//   OBBTreeNode::coMat[0][2] = box->covarianceMatrix[0][2];
367//
368//   OBBTreeNode::coMat[1][0] = box->covarianceMatrix[1][0];
369//   OBBTreeNode::coMat[1][1] = box->covarianceMatrix[1][1];
370//   OBBTreeNode::coMat[1][3] = box->covarianceMatrix[1][2];
371//
372//   OBBTreeNode::coMat[2][0] = box->covarianceMatrix[2][0];
373//   OBBTreeNode::coMat[2][1] = box->covarianceMatrix[2][1];
374//   OBBTreeNode::coMat[2][2] = box->covarianceMatrix[2][2];
375
376
377  /* new jacobi tests */
378  JacobI(OBBTreeNode::coMat, 3, eigvlMat, eigvMat, rotCount);
379  PRINTF(3)("-- Done Jacobi Decomposition\n");
380
381
382//   PRINTF(3)("Jacobi\n");
383//   for(int j = 1; j < 4; ++j)
384//   {
385//     PRINTF(3)(" |");
386//     for(int k = 1; k < 4; ++k)
387//     {
388//       PRINTF(3)(" \b%f ", eigvMat[j][k]);
389//     }
390//     PRINTF(3)(" |\n");
391//   }
392
393  axis[0].x = eigvMat[1][1]; axis[0].y = eigvMat[2][1]; axis[0].z = eigvMat[3][1];
394  axis[1].x = eigvMat[1][2]; axis[1].y = eigvMat[2][2]; axis[1].z = eigvMat[3][2];
395  axis[2].x = eigvMat[1][3]; axis[2].y = eigvMat[2][3]; axis[2].z = eigvMat[3][3];
396  axis[0].normalize();
397  axis[1].normalize();
398  axis[2].normalize();
399  box->axis = axis;
400
401  PRINTF(3)("-- Got Axis\n");
402
403  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[0].x, box->axis[0].y, box->axis[0].z);
404  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[1].x, box->axis[1].y, box->axis[1].z);
405  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[2].x, box->axis[2].y, box->axis[2].z);
406}
407
408
409void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length)
410{
411
412  /* now get the axis length */
413  Line                ax[3];                                 //!< the axis
414  float*              halfLength = new float[3];             //!< half length of the axis
415  float               tmpLength;                             //!< tmp save point for the length
416  Plane               p0(box->axis[0], *box->center);       //!< the axis planes
417  Plane               p1(box->axis[1], *box->center);
418  Plane               p2(box->axis[2], *box->center);
419  float               maxLength[3];
420  float               minLength[3];
421
422
423  /* get a bad bounding box */
424  halfLength[0] = -1.0f;
425  for(int j = 0; j < length; ++j)
426    {
427      tmpLength = fabs(p0.distancePoint(vertices[j]));
428      if( tmpLength > halfLength[0])
429        halfLength[0] = tmpLength;
430    }
431
432  halfLength[1] = -1.0f;
433  for(int j = 0; j < length; ++j)
434    {
435      tmpLength = fabs(p1.distancePoint(vertices[j]));
436      if( tmpLength > halfLength[1])
437        halfLength[1] = tmpLength;
438    }
439
440  halfLength[2] = -1.0f;
441  for(int j = 0; j < length; ++j)
442    {
443      tmpLength = fabs(p2.distancePoint(vertices[j]));
444      if( tmpLength > halfLength[2])
445        halfLength[2] = tmpLength;
446    }
447
448
449
450  /* get the maximal dimensions of the body in all directions */
451    maxLength[0] = p0.distancePoint(vertices[0]);
452    minLength[0] = p0.distancePoint(vertices[0]);
453   for(int j = 0; j < length; ++j)
454   {
455     tmpLength = p0.distancePoint(vertices[j]);
456     if( tmpLength > maxLength[0])
457       maxLength[0] = tmpLength;
458     else if( tmpLength < minLength[0])
459       minLength[0] = tmpLength;
460   }
461
462   maxLength[1] = p1.distancePoint(vertices[0]);
463   minLength[1] = p1.distancePoint(vertices[0]);
464   for(int j = 0; j < length; ++j)
465   {
466     tmpLength = p1.distancePoint(vertices[j]);
467     if( tmpLength > maxLength[1])
468       maxLength[1] = tmpLength;
469     else if( tmpLength < minLength[1])
470       minLength[1] = tmpLength;
471   }
472
473   maxLength[2] = p2.distancePoint(vertices[0]);
474   minLength[2] = p2.distancePoint(vertices[0]);
475   for(int j = 0; j < length; ++j)
476   {
477     tmpLength = p2.distancePoint(vertices[j]);
478     if( tmpLength > maxLength[2])
479       maxLength[2] = tmpLength;
480     else if( tmpLength < minLength[2])
481       minLength[2] = tmpLength;
482   }
483
484
485   /* calculate the real centre of the body by using the axis length */
486   float centerOffset[3];
487   float newHalfLength[3];
488   for(int i = 0; i < 3; ++i)
489     {
490       PRINTF(3)("max: %f, min: %f \n", maxLength[i], minLength[i]);
491       centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f;       // min length is negatie
492       newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f;      // min length is negative
493       *box->center +=  (box->axis[i] * centerOffset[i]);            // update the new center vector
494       halfLength[i] = newHalfLength[i];
495     }
496
497
498
499  box->halfLength = halfLength;
500  PRINTF(3)("-- Written Axis to obb\n");
501  PRINTF(3)("-- Finished Calculating Attributes\n");
502
503}
504
505
506
507/**
508  \brief this separates an ob-box in the middle
509* @param box: the box to separate
510
511  this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
512 */
513void OBBTreeNode::forkBox(OBB* box)
514{
515  /* get the longest axis of the box */
516  float               aLength = -1.0f;                     //!< the length of the longest axis
517  int                 axisIndex = 0;                       //!< this is the nr of the longest axis
518
519  for(int i = 0; i < 3; ++i)
520  {
521    if( aLength < box->halfLength[i])
522    {
523      aLength = box->halfLength[i];
524      axisIndex = i;
525    }
526  }
527
528   PRINTF(3)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength);
529
530
531  /* get the closest vertex near the center */
532  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
533  float               tmpDist;                             //!< temporary distance
534  int                 vertexIndex;
535  Plane               middlePlane(box->axis[axisIndex], *box->center); //!< the middle plane
536
537  vertexIndex = 0;
538  for(int i = 0; i < box->numOfVertices; ++i)
539  {
540    tmpDist = fabs(middlePlane.distancePoint(box->vertices[i]));
541    if( tmpDist < dist)
542    {
543      dist = tmpDist;
544      vertexIndex = i;
545    }
546  }
547
548  PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist);
549
550
551  /* now definin the separation plane through this specified nearest point and partition
552  the points depending on which side they are located
553  */
554  tList<sVec3D>      partition1;                           //!< the vertex partition 1
555  tList<sVec3D>      partition2;                           //!< the vertex partition 2
556
557
558  PRINTF(3)("vertex index: %i, of %i\n", vertexIndex, box->numOfVertices);
559  this->separationPlane = new Plane(box->axis[axisIndex], box->vertices[vertexIndex]);  //!< separation plane
560  this->sepPlaneCenter = &box->vertices[vertexIndex];
561  this->longestAxisIndex = axisIndex;
562
563  for(int i = 0; i < box->numOfVertices; ++i)
564  {
565    if( i == vertexIndex) continue;
566    tmpDist = this->separationPlane->distancePoint(box->vertices[i]);
567    if( tmpDist > 0.0)
568      partition1.add(&box->vertices[i]); /* positive numbers plus zero */
569    else
570      partition2.add(&box->vertices[i]); /* negatice numbers */
571  }
572  partition1.add(&box->vertices[vertexIndex]);
573  partition2.add(&box->vertices[vertexIndex]);
574
575  PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize());
576
577
578  /* now comes the separation into two different sVec3D arrays */
579  tIterator<sVec3D>* iterator;                             //!< the iterator to go through the lists
580  sVec3D*            element;                              //!< the elements
581  int                index;                                //!< index storage place
582  sVec3D*            vertList1;                            //!< the vertex list 1
583  sVec3D*            vertList2;                            //!< the vertex list 2
584
585  vertList1 = new sVec3D[partition1.getSize()];
586  vertList2 = new sVec3D[partition2.getSize()];
587
588  iterator = partition1.getIterator();
589  element = iterator->firstElement();
590  index = 0;
591  while( element != NULL)
592  {
593    vertList1[index][0] = element[0][0];
594    vertList1[index][1] = element[0][1];
595    vertList1[index][2] = element[0][2];
596    ++index;
597    element = iterator->nextElement();
598  }
599
600//   PRINTF(0)("\npartition 1:\n");
601//   for(int i = 0; i < partition1.getSize(); ++i)
602//   {
603//     PRINTF(0)("v[%i][0] = %f,\tv[%i][1] = %f,\tv[%i][1] = %f\n", i, vertList1[i][0], i, vertList1[i][1], i, vertList1[i][2]);
604//   }
605
606  iterator = partition2.getIterator();
607  element = iterator->firstElement();
608  index = 0;
609  while( element != NULL)
610  {
611    vertList2[index][0] = element[0][0];
612    vertList2[index][1] = element[0][1];
613    vertList2[index][2] = element[0][2];
614    ++index;
615    element = iterator->nextElement();
616  }
617
618  this->tmpVert1 = vertList1;
619  this->tmpVert2 = vertList2;
620  this->tmpLen1 = partition1.getSize();
621  this->tmpLen2 = partition2.getSize();
622
623  delete iterator;
624
625//   PRINTF(0)("\npartition 2:\n");
626//   for(int i = 0; i < partition2.getSize(); ++i)
627//   {
628//     PRINTF(0)("v[%i][0] = %f,\tv[%i][1] = %f,\tv[%i][1] = %f\n", i, vertList2[i][0], i,  vertList2[i][1], i, vertList2[i][2]);
629//   }
630}
631
632
633
634
635void OBBTreeNode::collideWith(BVTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB)
636{
637  PRINTF(3)("collideWith\n");
638  /* if the obb overlap, make subtests: check which node is realy overlaping  */
639  PRINT(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex());
640  if( unlikely(treeNode == NULL)) return;
641
642  if( this->overlapTest(this->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
643  {
644    PRINTF(3)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight);
645
646    /* check if left node overlaps */
647    if( likely( this->nodeLeft != NULL))
648    {
649      PRINT(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode->getIndex());
650      if( this->overlapTest(this->nodeLeft->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
651      {
652        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB);
653        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB);
654      }
655    }
656    /* check if right node overlaps */
657    if( likely( this->nodeRight != NULL))
658    {
659      PRINT(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode->getIndex());
660      if(this->overlapTest(this->nodeRight->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
661      {
662       this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB);
663       this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB);
664      }
665    }
666
667    /* so there is a collision and this is the last box in the tree (i.e. leaf) */
668    if( unlikely(this->nodeRight == NULL && this->nodeLeft == NULL))
669    {
670      nodeA->collidesWith(nodeB, *((OBBTreeNode*)treeNode)->bvElement->center);
671
672      nodeB->collidesWith(nodeA, *this->bvElement->center);
673    }
674
675  }
676}
677
678
679
680bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, WorldEntity* nodeA, WorldEntity* nodeB)
681{
682  /* first check all axis */
683  Vector t;
684  float rA = 0.0f;
685  float rB = 0.0f;
686  Vector l;
687  Vector rotAxisA[3];
688  Vector rotAxisB[3];
689
690  rotAxisA[0] =  nodeA->getAbsDir().apply(boxA->axis[0]);
691  rotAxisA[1] =  nodeA->getAbsDir().apply(boxA->axis[1]);
692  rotAxisA[2] =  nodeA->getAbsDir().apply(boxA->axis[2]);
693
694  rotAxisB[0] =  nodeB->getAbsDir().apply(boxB->axis[0]);
695  rotAxisB[1] =  nodeB->getAbsDir().apply(boxB->axis[1]);
696  rotAxisB[2] =  nodeB->getAbsDir().apply(boxB->axis[2]);
697
698  t = nodeA->getAbsCoor() + nodeA->getAbsDir().apply(*boxA->center) - ( nodeB->getAbsCoor() + nodeB->getAbsDir().apply(*boxB->center));
699
700//   printf("\n");
701//   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);
702//   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);
703//   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);
704//
705//   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);
706//   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);
707//   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);
708
709
710  /* All 3 axis of the object A */
711  for( int j = 0; j < 3; ++j)
712  {
713    rA = 0.0f;
714    rB = 0.0f;
715    l = rotAxisA[j];
716
717    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
718    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
719    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
720
721    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
722    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
723    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
724
725    PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
726
727    if( (rA + rB) < fabs(t.dot(l)))
728    {
729      PRINT(3)("keine Kollision\n");
730      return false;
731    }
732  }
733
734  /* All 3 axis of the object B */
735  for( int j = 0; j < 3; ++j)
736  {
737    rA = 0.0f;
738    rB = 0.0f;
739    l = rotAxisB[j];
740
741    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
742    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
743    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
744
745    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
746    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
747    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
748
749    PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
750
751    if( (rA + rB) < fabs(t.dot(l)))
752    {
753      PRINT(3)("keine Kollision\n");
754      return false;
755    }
756  }
757
758
759  /* Now check for all face cross products */
760
761  for( int j = 0; j < 3; ++j)
762  {
763    for(int k = 0; k < 3; ++k )
764    {
765      rA = 0.0f;
766      rB = 0.0f;
767      l = rotAxisA[j].cross(rotAxisB[k]);
768
769      rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
770      rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
771      rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
772
773      rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
774      rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
775      rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
776
777      PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
778
779      if( (rA + rB) < fabs(t.dot(l)))
780      {
781        PRINT(3)("keine Kollision\n");
782        return false;
783      }
784    }
785  }
786
787
788  boxA->bCollided = true; /* use this ONLY(!!!!) for drawing operations */
789  boxB->bCollided = true;
790  PRINT(3)("Kollision!\n");
791  return true;
792}
793
794
795
796
797
798void OBBTreeNode::drawBV(int depth, int drawMode)
799{
800  this->obbTree->getMaterial(treeIndex)->select();
801
802  /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */
803  if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL)
804  {
805    if( !(drawMode & DRAW_SINGLE && depth != 0))
806    {
807      if( drawMode & DRAW_POINTS)
808        glBegin(GL_POINTS);
809      for(int i = 0; i < this->bvElement->numOfVertices; ++i)
810      {
811        if( drawMode & DRAW_POINTS)
812          glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
813        else
814        {
815          glPushMatrix();
816          glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
817          gluSphere(this->sphereObj, 0.1, 10, 10);
818          glPopMatrix();
819        }
820      }
821      if( drawMode & DRAW_POINTS)
822        glEnd();
823    }
824  }
825
826
827  /* draw world axes */
828  if( drawMode & DRAW_BV_AXIS)
829  {
830    glBegin(GL_LINES);
831    glColor3f(0.0, 0.4, 0.3);
832    glVertex3f(0.0, 0.0, 0.0);
833    glVertex3f(3.0, 0.0, 0.0);
834
835    glVertex3f(0.0, 0.0, 0.0);
836    glVertex3f(0.0, 3.0, 0.0);
837
838    glVertex3f(0.0, 0.0, 0.0);
839    glVertex3f(0.0, 0.0, 3.0);
840    glEnd();
841  }
842
843
844  if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL)
845  {
846    if( !(drawMode & DRAW_SINGLE && depth != 0))
847    {
848      /* draw the obb axes */
849      glBegin(GL_LINES);
850      glColor3f(0.0, 0.4, 0.3);
851      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
852      glVertex3f(this->bvElement->center->x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
853                 this->bvElement->center->y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
854                 this->bvElement->center->z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
855
856      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
857      glVertex3f(this->bvElement->center->x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
858                 this->bvElement->center->y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
859                 this->bvElement->center->z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
860
861      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
862      glVertex3f(this->bvElement->center->x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
863                 this->bvElement->center->y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
864                 this->bvElement->center->z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
865      glEnd();
866    }
867  }
868
869
870  /* DRAW POLYGONS */
871  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
872  {
873    if(this->nodeLeft == NULL || this->nodeRight == NULL)
874      depth = 0;
875    if( !(drawMode & DRAW_SINGLE && depth != 0))
876    {
877    Vector cen = *this->bvElement->center;
878    Vector* axis = this->bvElement->axis;
879    float* len = this->bvElement->halfLength;
880
881    if( this->bvElement->bCollided)
882      this->obbTree->getCollisionMaterial()->select();
883    else if( drawMode & DRAW_BV_BLENDED)
884      this->obbTree->getTransparentMaterial(treeIndex)->select();
885
886
887
888    /* draw bounding box */
889    if( drawMode & DRAW_BV_BLENDED)
890      glBegin(GL_QUADS);
891    else
892      glBegin(GL_LINE_LOOP);
893    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
894               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
895               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
896    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
897               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
898               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
899    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
900               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
901               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
902    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
903               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
904               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
905    glEnd();
906
907    if( drawMode & DRAW_BV_BLENDED)
908      glBegin(GL_QUADS);
909    else
910      glBegin(GL_LINE_LOOP);
911    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
912               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
913               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
914    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
915               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
916               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
917    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
918               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
919               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
920    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
921               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
922               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
923    glEnd();
924
925    if( drawMode & DRAW_BV_BLENDED)
926      glBegin(GL_QUADS);
927    else
928      glBegin(GL_LINE_LOOP);
929    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
930               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
931               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
932    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
933               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
934               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
935    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
936               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
937               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
938    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
939               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
940               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
941    glEnd();
942
943    if( drawMode & DRAW_BV_BLENDED)
944      glBegin(GL_QUADS);
945    else
946      glBegin(GL_LINE_LOOP);
947    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
948               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
949               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
950    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
951               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
952               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
953    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
954               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
955               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
956    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
957               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
958               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
959    glEnd();
960
961
962    if( drawMode & DRAW_BV_BLENDED)
963    {
964      glBegin(GL_QUADS);
965      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
966                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
967                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
968      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
969                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
970                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
971      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
972                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
973                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
974      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
975                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
976                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
977      glEnd();
978
979      glBegin(GL_QUADS);
980      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
981                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
982                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
983      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
984                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
985                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
986      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
987                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
988                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
989      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
990                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
991                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
992      glEnd();
993    }
994
995
996    if( drawMode & DRAW_BV_BLENDED)
997      this->obbTree->getMaterial(treeIndex)->select();
998    }
999
1000  }
1001
1002  /* DRAW SEPARATING PLANE */
1003  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
1004  {
1005    if( !(drawMode & DRAW_SINGLE && depth != 0))
1006    {
1007      if( drawMode & DRAW_BV_BLENDED)
1008        this->obbTree->getTransparentMaterial(treeIndex)->select();
1009
1010    /* now draw the separation plane */
1011    Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
1012    Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
1013    Vector c = *this->bvElement->center;
1014    float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
1015    float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
1016    glBegin(GL_QUADS);
1017    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);
1018    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);
1019    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);
1020    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);
1021    glEnd();
1022
1023    if( drawMode & DRAW_BV_BLENDED)
1024      this->obbTree->getMaterial(treeIndex)->select();
1025
1026    }
1027  }
1028
1029
1030
1031  if( this->nodeLeft != NULL && depth != 0 )
1032    this->nodeLeft->drawBV(depth - 1, drawMode);
1033  if( this->nodeRight != NULL && depth != 0)
1034    this->nodeRight->drawBV(depth - 1, drawMode);
1035
1036  this->bvElement->bCollided = false;
1037}
1038
1039
1040
1041void OBBTreeNode::debug() const
1042{
1043
1044  /*
1045  for(int i = 0; i < length; i++)
1046  {
1047  PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
1048}
1049  */
1050}
Note: See TracBrowser for help on using the repository browser.