Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5449 was 5449, checked in by bensch, 19 years ago

orxonox/trunk: Jacobi-reloaded

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