Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: added weapons to the fps character

File size: 34.1 KB
RevLine 
[4588]1/*
[4541]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
[4617]11### File Specific:
[4541]12   main-programmer: Patrick Boenzli
13*/
14
[7711]15#define DEBUG_SPECIAL_MODULE 3/* DEBUG_MODULE_COLLISION_DETECTION*/
[4541]16
17#include "obb_tree_node.h"
[7711]18#include "obb_tree.h"
[4542]19#include "obb.h"
[7711]20
[5674]21#include "matrix.h"
[6022]22#include "model.h"
[5028]23#include "world_entity.h"
[6617]24#include "plane.h"
[4541]25
[5481]26#include "color.h"
[7711]27#include "glincl.h"
[4543]28
[7711]29#include <list>
30#include <vector>
[5511]31#include "debug.h"
[4572]32
33
34
[4541]35using namespace std;
36
37
[5430]38GLUquadricObj* OBBTreeNode_sphereObj = NULL;
[4630]39
[7711]40
[4541]41/**
[4836]42 *  standard constructor
[7711]43 * @param tree: reference to the obb tree
44 * @param depth: the depth of the obb tree to generate
[4617]45 */
[7711]46OBBTreeNode::OBBTreeNode (const OBBTree& tree, OBBTreeNode* prev, int depth)
47    : BVTreeNode()
[4541]48{
[4617]49  this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode");
[7711]50
51  this->obbTree = &tree;
52  this->nodePrev = prev;
53  this->depth = depth;
54  this->nextID = 0;
55
[4618]56  this->nodeLeft = NULL;
57  this->nodeRight = NULL;
[4814]58  this->bvElement = NULL;
[4630]59
[7711]60  this->triangleIndexList1 = NULL;
61  this->triangleIndexList2 = NULL;
[4638]62
[7711]63  this->modelInf = NULL;
64  this->triangleIndexes = NULL;
65
[5693]66  if( OBBTreeNode_sphereObj == NULL)
[5430]67    OBBTreeNode_sphereObj = gluNewQuadric();
[7711]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;
[4541]76}
77
78
79/**
[4836]80 *  standard deconstructor
[4617]81 */
[4588]82OBBTreeNode::~OBBTreeNode ()
[4541]83{
[4814]84  if( this->nodeLeft)
85    delete this->nodeLeft;
86  if( this->nodeRight)
87    delete this->nodeRight;
[7711]88
[4814]89  if( this->bvElement)
90    delete this->bvElement;
[4541]91}
92
93
[5684]94/**
95 *  creates a new BVTree or BVTree partition
96 * @param depth: how much more depth-steps to go: if == 1 don't go any deeper!
97 * @param modInfo: model informations from the abstrac model
[5689]98 *
[5684]99 * this function creates the Bounding Volume tree from a modelInfo struct and bases its calculations
100 * on the triangle informations (triangle soup not polygon soup)
101 */
[7711]102void OBBTreeNode::spawnBVTree(const modelInfo& modelInf, const int* triangleIndexes, int length)
[5684]103{
[7734]104  PRINTF(4)("\n==============================Creating OBB Tree Node==================\n");
105  PRINT(4)(" OBB Tree Infos: \n");
106  PRINT(4)("\tDepth: %i \n\tTree Index: %i \n\tNumber of Triangles: %i\n", depth, this->treeIndex, length);
[5684]107  this->depth = depth;
[4542]108
[5684]109  this->bvElement = new OBB();
[7711]110  this->bvElement->modelInf = &modelInf;
111  this->bvElement->triangleIndexes = triangleIndexes;
112  this->bvElement->triangleIndexesLength = length;
[5684]113
[7711]114  /* create the bounding boxes in three steps */
115  this->calculateBoxCovariance(*this->bvElement, modelInf, triangleIndexes, length);
116  this->calculateBoxEigenvectors(*this->bvElement, modelInf, triangleIndexes, length);
117  this->calculateBoxAxis(*this->bvElement, modelInf, triangleIndexes, length);
[4614]118
[7711]119  /* do we need to descent further in the obb tree?*/
[4614]120  if( likely( this->depth > 0))
121  {
[7711]122    this->forkBox(*this->bvElement);
[4626]123
[7711]124    if( this->triangleIndexLength1 >= 3)
[4638]125    {
[7711]126      this->nodeLeft = new OBBTreeNode(*this->obbTree, this, depth - 1);
127      this->nodeLeft->spawnBVTree(modelInf, this->triangleIndexList1, this->triangleIndexLength1);
[4638]128    }
[7711]129    if( this->triangleIndexLength2 >= 3)
[4638]130    {
[7711]131      this->nodeRight = new OBBTreeNode(*this->obbTree, this, depth - 1);
132      this->nodeRight->spawnBVTree(modelInf, this->triangleIndexList2, this->triangleIndexLength2);
[4638]133    }
[4614]134  }
[4557]135}
136
137
138
[7711]139/**
140 *  calculate the box covariance matrix
141 * @param box: reference to the box
142 * @param modelInf: the model info structure of the model
143 * @param tirangleIndexes: an array with the indexes of the triangles inside this
144 * @param length: the length of the indexes array
145 */
146void OBBTreeNode::calculateBoxCovariance(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, int length)
[4557]147{
[4543]148  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
[5428]149  float     face = 0.0f;                             //!< surface area of the entire convex hull
[4588]150  Vector    centroid[length];                        //!< centroid of the i'th convex hull
[4557]151  Vector    center;                                  //!< the center of the entire hull
[4544]152  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
[4545]153  Vector    t1, t2;                                  //!< temporary values
[8316]154  float     covariance[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};//!< the covariance matrix
[4588]155
[7711]156  /* fist compute all the convex hull face/facelets and centroids */
157  for( int i = 0; i < length ; ++i)
[4648]158  {
[7711]159    p = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]];
160    q = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]];
161    r = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]];
[4638]162
[7711]163    /* finding the facelet surface via cross-product */
164    t1 = p - q;
165    t2 = p - r;
166    facelet[i] = 0.5f * /*fabs*/( t1.cross(t2).len() );
167    /* update the entire convex hull surface */
168    face += facelet[i];
[4638]169
[7711]170    /* calculate the cetroid of the hull triangles */
171    centroid[i] = (p + q + r) / 3.0f;
172    /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
173    center += centroid[i] * facelet[i];
174    /* the arithmetical center */
[4648]175  }
[7711]176  /* take the average of the centroid sum */
177  center /= face;
[4588]178
179
[7711]180  /* now calculate the covariance matrix - if not written in three for-loops,
181     it would compute faster: minor */
182  for( int j = 0; j < 3; ++j)
[4648]183  {
[7711]184    for( int k = 0; k < 3; ++k)
[4648]185    {
[7711]186      for( int i = 0; i < length; ++i)
[4648]187      {
[7711]188        p = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]);
189        q = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]);
190        r = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]);
[4648]191
[7711]192        covariance[j][k] = facelet[i] * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] +
193                           q[j] * q[k] + r[j] * r[k]);
[4648]194      }
[7711]195      covariance[j][k] = covariance[j][k] / (12.0f * face) - center[j] * center[k];
[4648]196    }
197  }
[7711]198  for( int i = 0; i < 3; ++i)
[4617]199  {
[7711]200    box.covarianceMatrix[i][0] = covariance[i][0];
201    box.covarianceMatrix[i][1] = covariance[i][1];
202    box.covarianceMatrix[i][2] = covariance[i][2];
[4617]203  }
[7711]204  box.center = center;
[4562]205
[7711]206  /* debug output section*/
[7734]207  PRINTF(4)("\nOBB Covariance Matrix:\n");
[4674]208  for(int j = 0; j < 3; ++j)
209  {
[7734]210    PRINT(4)("\t\t");
[4674]211    for(int k = 0; k < 3; ++k)
212    {
[7734]213      PRINT(4)("%11.4f\t", covariance[j][k]);
[4674]214    }
[7734]215    PRINT(4)("\n");
[4674]216  }
[7734]217  PRINTF(4)("\nWeighteed OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", center.x, center.y, center.z);
[4631]218}
[4557]219
[4631]220
221
[7711]222/**
223 *  calculate the eigenvectors for the object oriented box
224 * @param box: reference to the box
225 * @param modelInf: the model info structure of the model
226 * @param tirangleIndexes: an array with the indexes of the triangles inside this
227 * @param length: the length of the indexes array
228 */
229void OBBTreeNode::calculateBoxEigenvectors(OBB& box, const modelInfo& modelInf,
230    const int* triangleIndexes, int length)
[4631]231{
232
[7711]233  Vector         axis[3];                            //!< the references to the obb axis
234  Matrix         covMat(  box.covarianceMatrix  );   //!< covariance matrix (in the matrix dataform)
235
236  /*
237  now getting spanning vectors of the sub-space:
[4617]238  the eigenvectors of a symmertric matrix, such as the
239  covarience matrix are mutually orthogonal.
240  after normalizing them, they can be used as a the basis
241  vectors
[4557]242  */
[4588]243
[7711]244  /* calculate the axis */
[5674]245  covMat.getEigenVectors(axis[0], axis[1], axis[2] );
[7711]246  box.axis[0] = axis[0];
247  box.axis[1] = axis[1];
248  box.axis[2] = axis[2];
[4627]249
[7735]250  // this is for axis aligned bouning boxes only
[7711]251//   box.axis[0] = Vector(1,0,0);
252//   box.axis[1] = Vector(0,1,0);
253//   box.axis[2] = Vector(0,0,1);
[5449]254
[7734]255  PRINTF(4)("Eigenvectors:\n");
256  PRINT(4)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[0].x, box.axis[0].y, box.axis[0].z);
257  PRINT(4)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[1].x, box.axis[1].y, box.axis[1].z);
258  PRINT(4)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[2].x, box.axis[2].y, box.axis[2].z);
[4632]259}
[4588]260
[4626]261
[5684]262
263
[7711]264/**
265 *  calculate the eigenvectors for the object oriented box
266 * @param box: reference to the box
267 * @param modelInf: the model info structure of the model
268 * @param tirangleIndexes: an array with the indexes of the triangles inside this
269 * @param length: the length of the indexes array
270 */
271void OBBTreeNode::calculateBoxAxis(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, int length)
[4631]272{
[4630]273
[7734]274  PRINTF(4)("Calculate Box Axis\n");
[4576]275  /* now get the axis length */
[4578]276  float               tmpLength;                             //!< tmp save point for the length
[7711]277  Plane               p0(box.axis[0], box.center);           //!< the axis planes
278  Plane               p1(box.axis[1], box.center);           //!< the axis planes
279  Plane               p2(box.axis[2], box.center);           //!< the axis planes
280  float               maxLength[3];                          //!< maximal lenth of the axis
281  float               minLength[3];                          //!< minimal length of the axis
282  const float*        tmpVec;                                //!< variable taking tmp vectors
283  float               centerOffset[3];
[4588]284
[7711]285  /* get the maximal dimensions of the body in all directions */
286  /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */
287  for( int k = 0; k  < 3; k++)
288  {
289    tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]);
290    Plane* p;
291    if( k == 0)
292      p = &p0;
293    else if( k == 1)
294      p = &p1;
295    else
296      p = &p2;
297    maxLength[k] = p->distancePoint(tmpVec);
298    minLength[k] = p->distancePoint(tmpVec);
[4658]299
[7711]300    for( int j = 0; j < length; ++j) {
301      for( int i = 0; i < 3; ++i) {
302        tmpVec = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]];
303        tmpLength = p->distancePoint(tmpVec);
[4658]304
[7711]305        if( tmpLength > maxLength[k])
306          maxLength[k] = tmpLength;
307        else if( tmpLength < minLength[k])
308          minLength[k] = tmpLength;
309      }
[4658]310    }
[7711]311  }
[4658]312
313
314
[7711]315  /* calculate the real centre of the body by using the axis length */
316  for( int i = 0; i < 3; ++i)
317  {
318    if( maxLength[i] > 0.0f && minLength[i] > 0.0f)  // both axis positiv
319      centerOffset[i] = minLength[i] + (maxLength[i] - minLength[i]) / 2.0f;
320    else if( maxLength[i] > 0.0f && maxLength[i] < 0.0f) // positiv and negativ
321      centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f;
322    else //both negativ
323     centerOffset[i] = minLength[i] + (maxLength[i] - minLength[i]) / 2.0f;
[4658]324
[7711]325    box.halfLength[i] = (maxLength[i] - minLength[i]) / 2.0f;
326  }
[4578]327
[7711]328  box.center += (box.axis[0] * centerOffset[0]);
329  box.center += (box.axis[1] * centerOffset[1]);
330  box.center += (box.axis[2] * centerOffset[2]);
[4585]331
332
[7734]333  PRINTF(4)("\n");
334  PRINT(4)("\tAxis halflength x: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[0], maxLength[0], minLength[0], centerOffset[0]);
335  PRINT(4)("\tAxis halflength y: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[1], maxLength[1], minLength[1], centerOffset[1] );
336  PRINT(4)("\tAxis halflength z: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[2], maxLength[2], minLength[2], centerOffset[2]);
[4542]337}
338
339
[4609]340
341/**
[7711]342 *  this separates an ob-box in the middle
343 * @param box: the box to separate
344 *
345 * this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
[4609]346 */
[7711]347void OBBTreeNode::forkBox(OBB& box)
[4557]348{
[7711]349
[7734]350  PRINTF(4)("Fork Box\n");
[7711]351  PRINTF(4)("Calculating the longest Axis\n");
[4557]352  /* get the longest axis of the box */
[7711]353  float               longestAxis = -1.0f;                 //!< the length of the longest axis
354  int                 longestAxisIndex = 0;                //!< this is the nr of the longest axis
[4609]355
[7711]356
357  /* now get the longest axis of the three exiting */
358  for( int i = 0; i < 3; ++i)
[4609]359  {
[7711]360    if( longestAxis < box.halfLength[i])
[4557]361    {
[7711]362      longestAxis = box.halfLength[i];
363      longestAxisIndex = i;
[4557]364    }
[4609]365  }
[7734]366  PRINTF(4)("\nLongest Axis is: Nr %i with a half-length of:%11.2f\n", longestAxisIndex, longestAxis);
[4588]367
[4609]368
[7711]369  PRINTF(4)("Separating along the longest axis\n");
[4557]370  /* get the closest vertex near the center */
[7711]371  float               tmpDist;                             //!< variable to save diverse distances temporarily
372  Plane               middlePlane(box.axis[longestAxisIndex], box.center); //!< the middle plane
[4588]373
[4609]374
[4611]375  /* now definin the separation plane through this specified nearest point and partition
[4617]376  the points depending on which side they are located
[4611]377  */
[7711]378  std::list<int>           partition1;                           //!< the vertex partition 1
379  std::list<int>           partition2;                           //!< the vertex partition 2
380  float*                   triangleCenter = new float[3];        //!< the center of the triangle
381  const float*             a;                                    //!< triangle  edge a
382  const float*             b;                                    //!< triangle  edge b
383  const float*             c;                                    //!< triangle  edge c
[4611]384
[4710]385
[7711]386  /* find the center of the box */
387  this->separationPlane = Plane(box.axis[longestAxisIndex], box.center);
388  this->sepPlaneCenter[0] = box.center.x;
389  this->sepPlaneCenter[1] = box.center.y;
390  this->sepPlaneCenter[2] = box.center.z;
391  this->longestAxisIndex = longestAxisIndex;
[4632]392
[7711]393  for( int i = 0; i < box.triangleIndexesLength; ++i)
[4612]394  {
[7711]395    /* first calculate the middle of the triangle */
396    a = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[0]];
397    b = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[1]];
398    c = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[2]];
399
400    triangleCenter[0] = (a[0] + b[0] + c[0]) / 3.0f;
401    triangleCenter[1] = (a[1] + b[1] + c[1]) / 3.0f;
402    triangleCenter[2] = (a[2] + b[2] + c[2]) / 3.0f;
403    tmpDist = this->separationPlane.distancePoint(*((sVec3D*)triangleCenter));
404
405    if( tmpDist > 0.0f)
406      partition1.push_back(box.triangleIndexes[i]); /* positive numbers plus zero */
407    else if( tmpDist < 0.0f)
408      partition2.push_back(box.triangleIndexes[i]); /* negatice numbers */
409    else {
410      partition1.push_back(box.triangleIndexes[i]); /* 0.0f? unprobable... */
411      partition2.push_back(box.triangleIndexes[i]);
412    }
[4612]413  }
[7734]414  PRINTF(4)("\nPartition1: got \t%i Vertices \nPartition2: got \t%i Vertices\n", partition1.size(), partition2.size());
[4611]415
[4612]416
[4613]417  /* now comes the separation into two different sVec3D arrays */
418  int                index;                                //!< index storage place
[7711]419  int*               triangleIndexList1;                   //!< the vertex list 1
420  int*               triangleIndexList2;                   //!< the vertex list 2
421  std::list<int>::iterator element;                        //!< the list iterator
[4613]422
[7711]423  triangleIndexList1 = new int[partition1.size()];
424  triangleIndexList2 = new int[partition2.size()];
[4613]425
[7711]426  for( element = partition1.begin(), index = 0; element != partition1.end(); element++, index++)
427    triangleIndexList1[index] = (*element);
[4613]428
[7711]429  for( element = partition2.begin(), index = 0; element != partition2.end(); element++, index++)
430    triangleIndexList2[index] = (*element);
[4613]431
[7711]432  if( this->triangleIndexList1!= NULL)
433    delete[] this->triangleIndexList1;
434  this->triangleIndexList1 = triangleIndexList1;
435  this->triangleIndexLength1 = partition1.size();
[4613]436
[7711]437  if( this->triangleIndexList2 != NULL)
438    delete[] this->triangleIndexList2;
439  this->triangleIndexList2 = triangleIndexList2;
440  this->triangleIndexLength2 = partition2.size();
[4557]441}
442
443
[4626]444
[7732]445/**
446 * collides one tree with an other
447 *  @param treeNode the other bv tree node
448 *  @param nodeA  the worldentity belonging to this bv
449 *  @param nodeB the worldentity belonging to treeNode
450 */
[5028]451void OBBTreeNode::collideWith(BVTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB)
[4695]452{
[7732]453  if( unlikely(treeNode == NULL || nodeA == NULL || nodeB == NULL))
[7711]454    return;
455
456  PRINTF(4)("collideWith\n");
[7732]457  PRINTF(5)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex());
[5042]458
[7732]459  // for now only collide with OBBTreeNodes
460  this->collideWithOBB((OBBTreeNode*)treeNode, nodeA, nodeB);
461}
[7711]462
[7732]463
464
465/**
466 * collides one obb tree with an other
467 *  @param treeNode the other bv tree node
468 *  @param nodeA  the worldentity belonging to this bv
469 *  @param nodeB the worldentity belonging to treeNode
470 */
471void OBBTreeNode::collideWithOBB(OBBTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB)
472{
[7734]473
474  if( this->overlapTest(this->bvElement, treeNode->bvElement, nodeA, nodeB))
[4695]475  {
[7732]476    PRINTF(5)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight);
[5038]477
[8190]478
479    // left node
[7735]480    if( this->nodeLeft != NULL )
[4704]481    {
[7732]482      if( this->overlapTest(this->nodeLeft->bvElement, treeNode->bvElement, nodeA, nodeB))
[4704]483      {
[8190]484        if( treeNode->nodeLeft != NULL)
485          this->nodeLeft->collideWith(treeNode->nodeLeft, nodeA, nodeB);
486        if( treeNode->nodeRight != NULL)
487          this->nodeLeft->collideWith(treeNode->nodeRight, nodeA, nodeB);
[4704]488      }
489    }
[8190]490
491    // right node
492    if( this->nodeRight != NULL )
[4704]493    {
[8190]494      if( this->overlapTest(this->nodeRight->bvElement, treeNode->bvElement, nodeA, nodeB))
[4704]495      {
[8190]496        if( treeNode->nodeLeft != NULL)
497          this->nodeRight->collideWith(treeNode->nodeLeft, nodeA, nodeB);
498        if( treeNode->nodeRight != NULL)
499          this->nodeRight->collideWith(treeNode->nodeRight, nodeA, nodeB);
[4704]500      }
[5044]501    }
[5028]502
[8190]503
504    // hybrid mode: we reached the end of this obbtree, now reach the end of the other tree
505    if( this->nodeLeft == NULL && this->nodeRight == NULL)
[5044]506    {
[8190]507      if( treeNode->nodeLeft != NULL)
508        this->collideWith(treeNode->nodeLeft, nodeA, nodeB);
509      if( treeNode->nodeRight != NULL)
510        this->collideWith(treeNode->nodeRight, nodeA, nodeB);
[4704]511    }
[5044]512
[8190]513
514    // now check if we reached the end of both trees
515    if( unlikely((this->nodeRight == NULL && this->nodeLeft == NULL) &&
516        (treeNode->nodeRight == NULL && treeNode->nodeLeft == NULL)) )
517    {
518      nodeA->registerCollision(nodeA, nodeB, (BoundingVolume*)this->bvElement, (BoundingVolume*)treeNode->bvElement);
519    }
520
[4695]521  }
522}
[4542]523
524
[7732]525/**
526 * this actualy checks if one obb box touches the other
527 * @param boxA the box from nodeA
528 * @param boxB the box from nodeB
529 * @param nodeA the node itself
530 * @param nodeB the node itself
531 */
532bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, WorldEntity* nodeA, WorldEntity* nodeB)
[4695]533{
[7711]534  //HACK remove this again
535  this->owner = nodeA;
536  //   if( boxB == NULL || boxA == NULL)
537  //     return false;
538
[4696]539  /* first check all axis */
[7713]540  Vector      t;
541  float       rA = 0.0f;
542  float       rB = 0.0f;
543  Vector      l;
544  Vector      rotAxisA[3];
545  Vector      rotAxisB[3];
[4626]546
[7732]547  rotAxisA[0] =  nodeA->getAbsDir().apply(boxA->axis[0]);
548  rotAxisA[1] =  nodeA->getAbsDir().apply(boxA->axis[1]);
549  rotAxisA[2] =  nodeA->getAbsDir().apply(boxA->axis[2]);
[4708]550
[7732]551  rotAxisB[0] =  nodeB->getAbsDir().apply(boxB->axis[0]);
552  rotAxisB[1] =  nodeB->getAbsDir().apply(boxB->axis[1]);
553  rotAxisB[2] =  nodeB->getAbsDir().apply(boxB->axis[2]);
[4708]554
[7732]555  t = nodeA->getAbsCoor() + nodeA->getAbsDir().apply(boxA->center) - ( nodeB->getAbsCoor() + nodeB->getAbsDir().apply(boxB->center));
[4708]556
[4703]557  /* All 3 axis of the object A */
[4701]558  for( int j = 0; j < 3; ++j)
[4705]559  {
560    rA = 0.0f;
561    rB = 0.0f;
[4708]562    l = rotAxisA[j];
[4705]563
[7732]564    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
565    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
566    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
[4705]567
[7732]568    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
569    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
570    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
[4705]571
[7711]572    PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
[4705]573
574    if( (rA + rB) < fabs(t.dot(l)))
[4700]575    {
[7711]576      PRINTF(4)("no Collision\n");
[4705]577      return false;
578    }
579  }
[4700]580
[4705]581  /* All 3 axis of the object B */
582  for( int j = 0; j < 3; ++j)
583  {
584    rA = 0.0f;
585    rB = 0.0f;
[4708]586    l = rotAxisB[j];
[4701]587
[7732]588    rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
589    rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
590    rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
[4700]591
[7732]592    rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
593    rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
594    rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
[4703]595
[7711]596    PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
[4705]597
598    if( (rA + rB) < fabs(t.dot(l)))
599    {
[7711]600      PRINTF(4)("no Collision\n");
[4705]601      return false;
[4701]602    }
[4705]603  }
[4700]604
[4705]605
606  /* Now check for all face cross products */
607
608  for( int j = 0; j < 3; ++j)
609  {
610    for(int k = 0; k < 3; ++k )
[4701]611    {
612      rA = 0.0f;
613      rB = 0.0f;
[4708]614      l = rotAxisA[j].cross(rotAxisB[k]);
[4701]615
[7732]616      rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l));
617      rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l));
618      rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l));
[4701]619
[7732]620      rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l));
621      rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l));
622      rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l));
[4701]623
[7711]624      PRINTF(5)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
[4703]625
[4701]626      if( (rA + rB) < fabs(t.dot(l)))
627      {
[7711]628        PRINTF(4)("keine Kollision\n");
[4701]629        return false;
630      }
[4703]631    }
[4705]632  }
[4701]633
[7711]634  /* FIXME: there is no collision mark set now */
[7732]635     boxA->bCollided = true; /* use this ONLY(!!!!) for drawing operations */
636     boxB->bCollided = true;
[4701]637
[7711]638
639  PRINTF(4)("Kollision!\n");
[4705]640  return true;
[4695]641}
642
643
[7711]644/**
645 *
646 * draw the BV tree - debug mode
647 */
[5481]648void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color,  bool top) const
[4553]649{
[7711]650  /* this function can be used to draw the triangles and/or the points only  */
651  if( 1 /*drawMode & DRAW_MODEL || drawMode & DRAW_ALL*/)
[4635]652  {
[7711]653    if( depth == 0/*!(drawMode & DRAW_SINGLE && depth != 0)*/)
[4622]654    {
[7711]655      if( 1 /*drawMode & DRAW_POINTS*/)
656      {
[4712]657        glBegin(GL_POINTS);
[7711]658        glColor3f(0.3, 0.8, 0.54);
[8316]659        for(unsigned int i = 0; i < this->bvElement->modelInf->numVertices*3; i+=3)
[7711]660          glVertex3f(this->bvElement->modelInf->pVertices[i],
661                     this->bvElement->modelInf->pVertices[i+1],
662                     this->bvElement->modelInf->pVertices[i+2]);
663        glEnd();
[4638]664      }
[4622]665    }
[4635]666  }
[4542]667
[5481]668  if (top)
669  {
670    glPushAttrib(GL_ENABLE_BIT);
671    glDisable(GL_LIGHTING);
672    glDisable(GL_TEXTURE_2D);
673  }
674  glColor3f(color.x, color.y, color.z);
[4542]675
[5481]676
[4589]677  /* draw world axes */
[8776]678//   if( 1 /*drawMode & DRAW_BV_AXIS*/)
679//   {
680//     glBegin(GL_LINES);
681//     glColor3f(1.0, 0.0, 0.0);
682//     glVertex3f(0.0, 0.0, 0.0);
683//     glVertex3f(3.0, 0.0, 0.0);
684//
685//     glColor3f(0.0, 1.0, 0.0);
686//     glVertex3f(0.0, 0.0, 0.0);
687//     glVertex3f(0.0, 3.0, 0.0);
688//
689//     glColor3f(0.0, 0.0, 1.0);
690//     glVertex3f(0.0, 0.0, 0.0);
691//     glVertex3f(0.0, 0.0, 3.0);
692//     glEnd();
693//   }
[4589]694
695
[7711]696  if( 1/*drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL*/)
[4635]697  {
[7711]698    if( 1/*drawMode & DRAW_SINGLE && depth != 0*/)
[4635]699    {
700      /* draw the obb axes */
701      glBegin(GL_LINES);
[7711]702      glColor3f(1.0, 0.0, 0.0);
703      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
704      glVertex3f(this->bvElement->center.x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
705                 this->bvElement->center.y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
706                 this->bvElement->center.z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
[4589]707
[7711]708      glColor3f(0.0, 1.0, 0.0);
709      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
710      glVertex3f(this->bvElement->center.x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
711                 this->bvElement->center.y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
712                 this->bvElement->center.z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
[4588]713
[7711]714      glColor3f(0.0, 0.0, 1.0);
715      glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z);
716      glVertex3f(this->bvElement->center.x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
717                 this->bvElement->center.y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
718                 this->bvElement->center.z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
[4635]719      glEnd();
720    }
721  }
[4581]722
[4588]723
[4674]724  /* DRAW POLYGONS */
[4673]725  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
[4635]726  {
[5487]727    if (top)
728    {
729      glEnable(GL_BLEND);
730      glBlendFunc(GL_SRC_ALPHA, GL_ONE);
731    }
732
[7711]733    if( this->nodeLeft == NULL && this->nodeRight == NULL)
[4710]734      depth = 0;
[4588]735
[7711]736    if( depth == 0 /*!(drawMode & DRAW_SINGLE && depth != 0)*/)
[5481]737    {
[4670]738
[4588]739
[7711]740      Vector cen = this->bvElement->center;
741      Vector* axis = this->bvElement->axis;
742      float* len = this->bvElement->halfLength;
[4588]743
[7711]744      if( this->bvElement->bCollided)
745      {
746        glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR
747      }
748      else if( drawMode & DRAW_BV_BLENDED)
749      {
750        glColor4f(color.x, color.y, color.z, .5);
751      }
[4588]752
[7711]753      // debug out
754      if( this->obbTree->getOwner() != NULL)
755      {
756        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());
757      }
758      else
759        PRINTF(4)("debug poly draw: depth: %i, mode: %i\n", depth, drawMode);
[4670]760
[4671]761
[7711]762      /* draw bounding box */
763      if( drawMode & DRAW_BV_BLENDED)
764        glBegin(GL_QUADS);
765      else
766        glBegin(GL_LINE_LOOP);
767      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
768                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
769                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
[4671]770      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
771                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
772                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
773      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
774                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
775                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
[7711]776      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
777                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
778                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
779      glEnd();
780
781      if( drawMode & DRAW_BV_BLENDED)
782        glBegin(GL_QUADS);
783      else
784        glBegin(GL_LINE_LOOP);
785      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
786                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
787                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
788      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
789                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
790                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
[4671]791      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
792                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
793                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
[7711]794      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
795                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
796                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
[4671]797      glEnd();
798
[7711]799      if( drawMode & DRAW_BV_BLENDED)
800        glBegin(GL_QUADS);
801      else
802        glBegin(GL_LINE_LOOP);
803      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
804                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
805                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
806      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
807                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
808                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
809      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
810                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
811                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
[4671]812      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
813                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
814                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
[7711]815      glEnd();
816
817      if( drawMode & DRAW_BV_BLENDED)
818        glBegin(GL_QUADS);
819      else
820        glBegin(GL_LINE_LOOP);
821      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
822                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
823                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
824      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
825                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
826                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
[4671]827      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
828                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
829                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
[7711]830      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
831                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
832                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
[4671]833      glEnd();
834
835
[7711]836      if( drawMode & DRAW_BV_BLENDED)
837      {
838        glBegin(GL_QUADS);
839        glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
840                   cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
841                   cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
842        glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
843                   cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
844                   cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
845        glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
846                   cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
847                   cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
848        glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
849                   cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
850                   cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
851        glEnd();
852
853        glBegin(GL_QUADS);
854        glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
855                   cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
856                   cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
857        glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
858                   cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
859                   cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
860        glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
861                   cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
862                   cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
863        glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
864                   cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
865                   cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
866        glEnd();
867      }
868
869      if( drawMode & DRAW_BV_BLENDED)
870        glColor3f(color.x, color.y, color.z);
[4635]871    }
872  }
[4588]873
[4674]874  /* DRAW SEPARATING PLANE */
[4635]875  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
[4632]876  {
[4636]877    if( !(drawMode & DRAW_SINGLE && depth != 0))
[4635]878    {
[4671]879      if( drawMode & DRAW_BV_BLENDED)
[5481]880        glColor4f(color.x, color.y, color.z, .6);
[4671]881
[7711]882      /* now draw the separation plane */
883      Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
884      Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
885      Vector c = this->bvElement->center;
886      float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
887      float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
888      glBegin(GL_QUADS);
889      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);
890      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);
891      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);
892      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);
893      glEnd();
[4671]894
[7711]895      if( drawMode & DRAW_BV_BLENDED)
896        glColor4f(color.x, color.y, color.z, 1.0);
[4671]897
[4635]898    }
[4632]899  }
[4588]900
[4702]901
902
[5481]903  if (depth > 0)
904  {
905    if( this->nodeLeft != NULL)
[5494]906      this->nodeLeft->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(15.0,0.0,0.0)), false);
[5481]907    if( this->nodeRight != NULL)
[5494]908      this->nodeRight->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(30.0,0.0,0.0)), false);
[5481]909  }
910  this->bvElement->bCollided = false;
[4588]911
[5481]912  if (top)
913    glPopAttrib();
[4557]914}
[4542]915
916
[4568]917
[4746]918void OBBTreeNode::debug() const
[4568]919{
[7711]920  PRINT(0)("========OBBTreeNode::debug()=====\n");
921  PRINT(0)(" Current depth: %i", this->depth);
922  PRINT(0)(" ");
923  PRINT(0)("=================================\n");
[4617]924}
Note: See TracBrowser for help on using the repository browser.