[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 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION |
---|
| 17 | |
---|
| 18 | #include "obb_tree_node.h" |
---|
[4542] | 19 | #include "list.h" |
---|
| 20 | #include "obb.h" |
---|
[4616] | 21 | #include "obb_tree.h" |
---|
[4544] | 22 | #include "vector.h" |
---|
[4550] | 23 | #include "abstract_model.h" |
---|
[5028] | 24 | #include "world_entity.h" |
---|
[4541] | 25 | |
---|
[4543] | 26 | #include <math.h> |
---|
[5481] | 27 | #include "color.h" |
---|
[4543] | 28 | |
---|
[4638] | 29 | #include "stdincl.h" |
---|
[4627] | 30 | #include "lin_alg.h" |
---|
[5431] | 31 | #include "glincl.h" |
---|
[4572] | 32 | |
---|
| 33 | |
---|
| 34 | |
---|
[4541] | 35 | using namespace std; |
---|
| 36 | |
---|
[4622] | 37 | OBBTree* OBBTreeNode::obbTree = NULL; |
---|
[4541] | 38 | |
---|
[5431] | 39 | float** OBBTreeNode::coMat = NULL; |
---|
| 40 | float** OBBTreeNode::eigvMat = NULL; |
---|
| 41 | float* OBBTreeNode::eigvlMat = NULL; |
---|
| 42 | int* OBBTreeNode::rotCount = NULL; |
---|
[5430] | 43 | GLUquadricObj* OBBTreeNode_sphereObj = NULL; |
---|
[4630] | 44 | |
---|
[4541] | 45 | /** |
---|
[4836] | 46 | * standard constructor |
---|
[4617] | 47 | */ |
---|
[4588] | 48 | OBBTreeNode::OBBTreeNode () |
---|
[4541] | 49 | { |
---|
[4617] | 50 | this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); |
---|
[4618] | 51 | this->nodeLeft = NULL; |
---|
| 52 | this->nodeRight = NULL; |
---|
[4814] | 53 | this->bvElement = NULL; |
---|
[4630] | 54 | |
---|
[5431] | 55 | if(OBBTreeNode::coMat == NULL) |
---|
[4630] | 56 | { |
---|
[5431] | 57 | OBBTreeNode::coMat = new float*[4]; |
---|
[4630] | 58 | for(int i = 0; i < 4; i++) |
---|
[5431] | 59 | OBBTreeNode::coMat[i] = new float[4]; |
---|
[4630] | 60 | } |
---|
[5431] | 61 | if(OBBTreeNode::eigvMat == NULL) |
---|
[4630] | 62 | { |
---|
[5431] | 63 | OBBTreeNode::eigvMat = new float*[4]; |
---|
[4630] | 64 | for(int i = 0; i < 4; i++) |
---|
[5431] | 65 | OBBTreeNode::eigvMat[i] = new float[4]; |
---|
[4630] | 66 | } |
---|
[5431] | 67 | if( OBBTreeNode::eigvlMat == NULL) |
---|
[4630] | 68 | { |
---|
[5431] | 69 | OBBTreeNode::eigvlMat = new float[4]; |
---|
[4630] | 70 | } |
---|
[5431] | 71 | if( OBBTreeNode::rotCount == NULL) |
---|
| 72 | OBBTreeNode::rotCount = new int; |
---|
[4638] | 73 | |
---|
[5430] | 74 | if (OBBTreeNode_sphereObj == NULL) |
---|
| 75 | OBBTreeNode_sphereObj = gluNewQuadric(); |
---|
[4541] | 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | /** |
---|
[4836] | 80 | * standard deconstructor |
---|
[4617] | 81 | */ |
---|
[4588] | 82 | OBBTreeNode::~OBBTreeNode () |
---|
[4541] | 83 | { |
---|
[4814] | 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; |
---|
[4541] | 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
[4542] | 100 | |
---|
| 101 | /** |
---|
[4836] | 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 |
---|
[4617] | 105 | */ |
---|
[4544] | 106 | void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length) |
---|
[4542] | 107 | { |
---|
[4717] | 108 | PRINT(3)("\n"); |
---|
[4638] | 109 | this->treeIndex = this->obbTree->getID(); |
---|
[4717] | 110 | PRINTF(3)("OBB Depth: %i, tree index: %i, numVertices: %i\n", depth, treeIndex, length); |
---|
[4614] | 111 | this->depth = depth; |
---|
| 112 | |
---|
[4638] | 113 | |
---|
[4630] | 114 | this->bvElement = new OBB(); |
---|
[4638] | 115 | this->bvElement->vertices = verticesList; |
---|
| 116 | this->bvElement->numOfVertices = length; |
---|
| 117 | PRINTF(3)("Created OBBox\n"); |
---|
[4632] | 118 | this->calculateBoxCovariance(this->bvElement, verticesList, length); |
---|
[4638] | 119 | PRINTF(3)("Calculated attributes1\n"); |
---|
[4632] | 120 | this->calculateBoxEigenvectors(this->bvElement, verticesList, length); |
---|
[4638] | 121 | PRINTF(3)("Calculated attributes2\n"); |
---|
[4632] | 122 | this->calculateBoxAxis(this->bvElement, verticesList, length); |
---|
[4638] | 123 | PRINTF(3)("Calculated attributes3\n"); |
---|
[4617] | 124 | |
---|
[4814] | 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; |
---|
[4632] | 128 | |
---|
[4614] | 129 | if( likely( this->depth > 0)) |
---|
| 130 | { |
---|
| 131 | this->forkBox(this->bvElement); |
---|
[4626] | 132 | |
---|
[4630] | 133 | |
---|
[4710] | 134 | if(this->tmpLen1 > 2) |
---|
[4638] | 135 | { |
---|
| 136 | OBBTreeNode* node1 = new OBBTreeNode(); |
---|
| 137 | this->nodeLeft = node1; |
---|
| 138 | this->nodeLeft->spawnBVTree(depth - 1, this->tmpVert1, this->tmpLen1); |
---|
| 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
[4717] | 142 | PRINTF(3)("Aboarding tree walk: less than 3 vertices left\n"); |
---|
[4638] | 143 | } |
---|
[4630] | 144 | |
---|
[4710] | 145 | if( this->tmpLen2 > 2) |
---|
[4638] | 146 | { |
---|
| 147 | OBBTreeNode* node2 = new OBBTreeNode(); |
---|
| 148 | this->nodeRight = node2; |
---|
| 149 | this->nodeRight->spawnBVTree(depth - 1, this->tmpVert2, this->tmpLen2); |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
[4717] | 153 | PRINTF(3)("Abording tree walk: less than 3 vertices left\n"); |
---|
[4638] | 154 | } |
---|
[4630] | 155 | |
---|
[4614] | 156 | } |
---|
[4557] | 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | |
---|
[4632] | 161 | void OBBTreeNode::calculateBoxCovariance(OBB* box, sVec3D* verticesList, int length) |
---|
[4557] | 162 | { |
---|
[4543] | 163 | float facelet[length]; //!< surface area of the i'th triangle of the convex hull |
---|
[5428] | 164 | float face = 0.0f; //!< surface area of the entire convex hull |
---|
[4588] | 165 | Vector centroid[length]; //!< centroid of the i'th convex hull |
---|
[4557] | 166 | Vector center; //!< the center of the entire hull |
---|
[4544] | 167 | Vector p, q, r; //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d |
---|
[4545] | 168 | Vector t1, t2; //!< temporary values |
---|
[4628] | 169 | float covariance[3][3]; //!< the covariance matrix |
---|
[4674] | 170 | int mode = 0; //!< mode = 0: vertex soup, no connections, mode = 1: 3 following verteces build a triangle |
---|
[4588] | 171 | |
---|
[4553] | 172 | this->numOfVertices = length; |
---|
| 173 | this->vertices = verticesList; |
---|
| 174 | |
---|
[4562] | 175 | |
---|
[4648] | 176 | if( likely(mode == 0)) |
---|
| 177 | { |
---|
| 178 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
[5428] | 179 | for( int i = 0; i+3 < length ; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
[4648] | 180 | { |
---|
| 181 | p = verticesList[i]; |
---|
| 182 | q = verticesList[i + 1]; |
---|
| 183 | r = verticesList[i + 2]; |
---|
[4638] | 184 | |
---|
[4648] | 185 | t1 = p - q; t2 = p - r; |
---|
[4638] | 186 | |
---|
[4648] | 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 */ |
---|
[5428] | 203 | for( int j = 0; j < 3; ++j) |
---|
[4648] | 204 | { |
---|
[5428] | 205 | for( int k = 0; k < 3; ++k) |
---|
[4648] | 206 | { |
---|
[5428] | 207 | for( int i = 0; i + 3 < length; i+=3) |
---|
[4648] | 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) |
---|
[4617] | 221 | { |
---|
[5428] | 222 | for( int i = 0; i + 3 < length; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
[4648] | 223 | { |
---|
| 224 | p = verticesList[i]; |
---|
| 225 | q = verticesList[i + 1]; |
---|
| 226 | r = verticesList[i + 2]; |
---|
[4588] | 227 | |
---|
[4648] | 228 | centroid[i] = (p + q + r) / 3.0f; |
---|
| 229 | center += centroid[i]; |
---|
| 230 | } |
---|
| 231 | center /= length; |
---|
[4588] | 232 | |
---|
[4648] | 233 | for( int j = 0; j < 3; ++j) |
---|
| 234 | { |
---|
| 235 | for( int k = 0; k < 3; ++k) |
---|
| 236 | { |
---|
[5428] | 237 | for( int i = 0; i + 3 < length; i+=3) |
---|
[4648] | 238 | { |
---|
| 239 | p = verticesList[i]; |
---|
| 240 | q = verticesList[i +1]; |
---|
| 241 | r = verticesList[i + 2]; |
---|
[4545] | 242 | |
---|
[4648] | 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"); |
---|
[4617] | 249 | } |
---|
[4648] | 250 | else if( mode == 2) |
---|
| 251 | { |
---|
| 252 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
[5428] | 253 | for(int i = 0; i + 3 < length; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
[4648] | 254 | { |
---|
| 255 | p = verticesList[i]; |
---|
| 256 | q = verticesList[i + 1]; |
---|
| 257 | r = verticesList[i + 2]; |
---|
[4562] | 258 | |
---|
[4648] | 259 | t1 = p - q; t2 = p - r; |
---|
[4562] | 260 | |
---|
[4648] | 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 | { |
---|
[5428] | 279 | for( int i = 0; i + 3 < length; i+=3) |
---|
[4648] | 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 |
---|
[4617] | 293 | { |
---|
[4648] | 294 | for( int i = 0; i < length; ++i) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
[4545] | 295 | { |
---|
[4648] | 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) |
---|
[4617] | 303 | { |
---|
[5428] | 304 | for( int i = 0; i + 3 < length; i+=3) |
---|
[4648] | 305 | { |
---|
| 306 | p = verticesList[i]; |
---|
| 307 | q = verticesList[i +1]; |
---|
| 308 | r = verticesList[i + 2]; |
---|
[4544] | 309 | |
---|
[4648] | 310 | covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k]; |
---|
| 311 | } |
---|
| 312 | covariance[j][k] /= (3.0f * length); |
---|
[4617] | 313 | } |
---|
[4545] | 314 | } |
---|
[4648] | 315 | PRINTF(3)("-- Calculated Covariance\n"); |
---|
[4617] | 316 | } |
---|
[4562] | 317 | |
---|
[4648] | 318 | PRINTF(3)("\nVertex Data:\n"); |
---|
[4638] | 319 | for(int i = 0; i < length; i++) |
---|
| 320 | { |
---|
[4648] | 321 | PRINTF(3)("vertex %i: %f, %f, %f\n", i, box->vertices[i][0], box->vertices[i][1], box->vertices[i][2]); |
---|
[4638] | 322 | } |
---|
[4588] | 323 | |
---|
[4648] | 324 | |
---|
[4675] | 325 | PRINTF(3)("\nCovariance Matrix:\n"); |
---|
[4674] | 326 | for(int j = 0; j < 3; ++j) |
---|
| 327 | { |
---|
[4675] | 328 | PRINT(3)(" |"); |
---|
[4674] | 329 | for(int k = 0; k < 3; ++k) |
---|
| 330 | { |
---|
[4675] | 331 | PRINT(3)(" \b%f ", covariance[j][k]); |
---|
[4674] | 332 | } |
---|
[4675] | 333 | PRINT(3)(" |\n"); |
---|
[4674] | 334 | } |
---|
| 335 | |
---|
[4638] | 336 | PRINTF(3)("center: %f, %f, %f\n", center.x, center.y, center.z); |
---|
[4553] | 337 | |
---|
[4588] | 338 | |
---|
[4674] | 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 | } |
---|
[4560] | 345 | *box->center = center; |
---|
[4638] | 346 | PRINTF(3)("-- Written Result to obb\n"); |
---|
[4631] | 347 | } |
---|
[4557] | 348 | |
---|
[4631] | 349 | |
---|
| 350 | |
---|
[4632] | 351 | void OBBTreeNode::calculateBoxEigenvectors(OBB* box, sVec3D* verticesList, int length) |
---|
[4631] | 352 | { |
---|
| 353 | |
---|
[4557] | 354 | /* now getting spanning vectors of the sub-space: |
---|
[4617] | 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 |
---|
[4557] | 359 | */ |
---|
[4660] | 360 | Vector* axis = new Vector[3]; //!< the references to the obb axis |
---|
[4588] | 361 | |
---|
[5449] | 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]; |
---|
[4627] | 365 | |
---|
[5449] | 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 | |
---|
[5429] | 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 | |
---|
[4630] | 387 | /* new jacobi tests */ |
---|
[5488] | 388 | JacobI(OBBTreeNode::coMat, OBBTreeNode::eigvlMat, OBBTreeNode::eigvMat, OBBTreeNode::rotCount); |
---|
[4638] | 389 | PRINTF(3)("-- Done Jacobi Decomposition\n"); |
---|
[4628] | 390 | |
---|
[4627] | 391 | |
---|
[5491] | 392 | // PRINTF(0)("Jacobi\n"); |
---|
| 393 | // for(int j = 0; j < 3; ++j) |
---|
[4629] | 394 | // { |
---|
[5491] | 395 | // printf(" |"); |
---|
| 396 | // for(int k = 0; k < 3; ++k) |
---|
[4629] | 397 | // { |
---|
[5491] | 398 | // printf(" \t%f ", OBBTreeNode::OBBTreeNode::eigvMat[j][k]); |
---|
[4629] | 399 | // } |
---|
[5491] | 400 | // printf(" |\n"); |
---|
[4629] | 401 | // } |
---|
| 402 | |
---|
[5449] | 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]; |
---|
[4705] | 406 | axis[0].normalize(); |
---|
| 407 | axis[1].normalize(); |
---|
| 408 | axis[2].normalize(); |
---|
[4576] | 409 | box->axis = axis; |
---|
[4660] | 410 | |
---|
[5491] | 411 | PRINTF(0)("-- Got Axis\n"); |
---|
[4588] | 412 | |
---|
[5491] | 413 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[0].x, box->axis[0].y, box->axis[0].z); |
---|
| 414 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[1].x, box->axis[1].y, box->axis[1].z); |
---|
| 415 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[2].x, box->axis[2].y, box->axis[2].z); |
---|
[4632] | 416 | } |
---|
[4588] | 417 | |
---|
[4626] | 418 | |
---|
[4632] | 419 | void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length) |
---|
[4631] | 420 | { |
---|
[4630] | 421 | |
---|
[4576] | 422 | /* now get the axis length */ |
---|
[4578] | 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 |
---|
[4660] | 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); |
---|
[4658] | 429 | float maxLength[3]; |
---|
| 430 | float minLength[3]; |
---|
[4588] | 431 | |
---|
[4658] | 432 | |
---|
| 433 | /* get a bad bounding box */ |
---|
[4589] | 434 | halfLength[0] = -1.0f; |
---|
[4585] | 435 | for(int j = 0; j < length; ++j) |
---|
[4658] | 436 | { |
---|
[4661] | 437 | tmpLength = fabs(p0.distancePoint(vertices[j])); |
---|
[4658] | 438 | if( tmpLength > halfLength[0]) |
---|
[4659] | 439 | halfLength[0] = tmpLength; |
---|
[4658] | 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]) |
---|
[4659] | 447 | halfLength[1] = tmpLength; |
---|
[4658] | 448 | } |
---|
| 449 | |
---|
| 450 | halfLength[2] = -1.0f; |
---|
| 451 | for(int j = 0; j < length; ++j) |
---|
| 452 | { |
---|
[4661] | 453 | tmpLength = fabs(p2.distancePoint(vertices[j])); |
---|
[4658] | 454 | if( tmpLength > halfLength[2]) |
---|
[4659] | 455 | halfLength[2] = tmpLength; |
---|
[4658] | 456 | } |
---|
| 457 | |
---|
| 458 | |
---|
| 459 | |
---|
| 460 | /* get the maximal dimensions of the body in all directions */ |
---|
[4710] | 461 | maxLength[0] = p0.distancePoint(vertices[0]); |
---|
| 462 | minLength[0] = p0.distancePoint(vertices[0]); |
---|
[4660] | 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 | } |
---|
[4578] | 471 | |
---|
[4710] | 472 | maxLength[1] = p1.distancePoint(vertices[0]); |
---|
| 473 | minLength[1] = p1.distancePoint(vertices[0]); |
---|
[4660] | 474 | for(int j = 0; j < length; ++j) |
---|
| 475 | { |
---|
[4661] | 476 | tmpLength = p1.distancePoint(vertices[j]); |
---|
[4660] | 477 | if( tmpLength > maxLength[1]) |
---|
| 478 | maxLength[1] = tmpLength; |
---|
| 479 | else if( tmpLength < minLength[1]) |
---|
| 480 | minLength[1] = tmpLength; |
---|
| 481 | } |
---|
[4585] | 482 | |
---|
[4710] | 483 | maxLength[2] = p2.distancePoint(vertices[0]); |
---|
| 484 | minLength[2] = p2.distancePoint(vertices[0]); |
---|
[4660] | 485 | for(int j = 0; j < length; ++j) |
---|
| 486 | { |
---|
[4661] | 487 | tmpLength = p2.distancePoint(vertices[j]); |
---|
[4660] | 488 | if( tmpLength > maxLength[2]) |
---|
| 489 | maxLength[2] = tmpLength; |
---|
| 490 | else if( tmpLength < minLength[2]) |
---|
| 491 | minLength[2] = tmpLength; |
---|
| 492 | } |
---|
[4585] | 493 | |
---|
[4660] | 494 | |
---|
| 495 | /* calculate the real centre of the body by using the axis length */ |
---|
[4668] | 496 | float centerOffset[3]; |
---|
| 497 | float newHalfLength[3]; |
---|
[4660] | 498 | for(int i = 0; i < 3; ++i) |
---|
| 499 | { |
---|
[4674] | 500 | PRINTF(3)("max: %f, min: %f \n", maxLength[i], minLength[i]); |
---|
[4710] | 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 |
---|
[4668] | 503 | *box->center += (box->axis[i] * centerOffset[i]); // update the new center vector |
---|
| 504 | halfLength[i] = newHalfLength[i]; |
---|
[4660] | 505 | } |
---|
| 506 | |
---|
| 507 | |
---|
| 508 | |
---|
[4586] | 509 | box->halfLength = halfLength; |
---|
[4638] | 510 | PRINTF(3)("-- Written Axis to obb\n"); |
---|
| 511 | PRINTF(3)("-- Finished Calculating Attributes\n"); |
---|
[4585] | 512 | |
---|
[4542] | 513 | } |
---|
| 514 | |
---|
| 515 | |
---|
[4609] | 516 | |
---|
| 517 | /** |
---|
| 518 | \brief this separates an ob-box in the middle |
---|
[4836] | 519 | * @param box: the box to separate |
---|
[4609] | 520 | |
---|
| 521 | this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis |
---|
| 522 | */ |
---|
[4557] | 523 | void OBBTreeNode::forkBox(OBB* box) |
---|
| 524 | { |
---|
| 525 | /* get the longest axis of the box */ |
---|
[4609] | 526 | float aLength = -1.0f; //!< the length of the longest axis |
---|
| 527 | int axisIndex = 0; //!< this is the nr of the longest axis |
---|
| 528 | |
---|
[4557] | 529 | for(int i = 0; i < 3; ++i) |
---|
[4609] | 530 | { |
---|
| 531 | if( aLength < box->halfLength[i]) |
---|
[4557] | 532 | { |
---|
[4609] | 533 | aLength = box->halfLength[i]; |
---|
| 534 | axisIndex = i; |
---|
[4557] | 535 | } |
---|
[4609] | 536 | } |
---|
[4588] | 537 | |
---|
[4688] | 538 | PRINTF(3)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength); |
---|
[4609] | 539 | |
---|
| 540 | |
---|
[4557] | 541 | /* get the closest vertex near the center */ |
---|
[4611] | 542 | float dist = 999999.0f; //!< the smallest distance to each vertex |
---|
[4609] | 543 | float tmpDist; //!< temporary distance |
---|
| 544 | int vertexIndex; |
---|
[4660] | 545 | Plane middlePlane(box->axis[axisIndex], *box->center); //!< the middle plane |
---|
[4588] | 546 | |
---|
[4660] | 547 | vertexIndex = 0; |
---|
[4609] | 548 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
| 549 | { |
---|
[4611] | 550 | tmpDist = fabs(middlePlane.distancePoint(box->vertices[i])); |
---|
| 551 | if( tmpDist < dist) |
---|
| 552 | { |
---|
[4609] | 553 | dist = tmpDist; |
---|
[4611] | 554 | vertexIndex = i; |
---|
| 555 | } |
---|
[4609] | 556 | } |
---|
| 557 | |
---|
[4710] | 558 | PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist); |
---|
[4609] | 559 | |
---|
| 560 | |
---|
[4611] | 561 | /* now definin the separation plane through this specified nearest point and partition |
---|
[4617] | 562 | the points depending on which side they are located |
---|
[4611] | 563 | */ |
---|
| 564 | tList<sVec3D> partition1; //!< the vertex partition 1 |
---|
| 565 | tList<sVec3D> partition2; //!< the vertex partition 2 |
---|
| 566 | |
---|
[4710] | 567 | |
---|
[4695] | 568 | PRINTF(3)("vertex index: %i, of %i\n", vertexIndex, box->numOfVertices); |
---|
[4660] | 569 | this->separationPlane = new Plane(box->axis[axisIndex], box->vertices[vertexIndex]); //!< separation plane |
---|
[4632] | 570 | this->sepPlaneCenter = &box->vertices[vertexIndex]; |
---|
| 571 | this->longestAxisIndex = axisIndex; |
---|
| 572 | |
---|
[4612] | 573 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
| 574 | { |
---|
[4710] | 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 */ |
---|
[4612] | 579 | else |
---|
[4710] | 580 | partition2.add(&box->vertices[i]); /* negatice numbers */ |
---|
[4612] | 581 | } |
---|
[4613] | 582 | partition1.add(&box->vertices[vertexIndex]); |
---|
[4710] | 583 | partition2.add(&box->vertices[vertexIndex]); |
---|
[4611] | 584 | |
---|
[4710] | 585 | PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize()); |
---|
[4612] | 586 | |
---|
[4613] | 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(); |
---|
[5115] | 599 | element = iterator->firstElement(); |
---|
[4613] | 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 | |
---|
[4638] | 610 | // PRINTF(0)("\npartition 1:\n"); |
---|
[4626] | 611 | // for(int i = 0; i < partition1.getSize(); ++i) |
---|
| 612 | // { |
---|
[4638] | 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]); |
---|
[4626] | 614 | // } |
---|
[4613] | 615 | |
---|
| 616 | iterator = partition2.getIterator(); |
---|
[5115] | 617 | element = iterator->firstElement(); |
---|
[4613] | 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 | |
---|
[4630] | 628 | this->tmpVert1 = vertList1; |
---|
| 629 | this->tmpVert2 = vertList2; |
---|
| 630 | this->tmpLen1 = partition1.getSize(); |
---|
| 631 | this->tmpLen2 = partition2.getSize(); |
---|
| 632 | |
---|
[4638] | 633 | delete iterator; |
---|
| 634 | |
---|
| 635 | // PRINTF(0)("\npartition 2:\n"); |
---|
[4626] | 636 | // for(int i = 0; i < partition2.getSize(); ++i) |
---|
| 637 | // { |
---|
[4638] | 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]); |
---|
[4626] | 639 | // } |
---|
[4557] | 640 | } |
---|
| 641 | |
---|
| 642 | |
---|
[4626] | 643 | |
---|
| 644 | |
---|
[5028] | 645 | void OBBTreeNode::collideWith(BVTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB) |
---|
[4695] | 646 | { |
---|
[4705] | 647 | PRINTF(3)("collideWith\n"); |
---|
[4695] | 648 | /* if the obb overlap, make subtests: check which node is realy overlaping */ |
---|
[4705] | 649 | PRINT(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex()); |
---|
[4718] | 650 | if( unlikely(treeNode == NULL)) return; |
---|
[5042] | 651 | |
---|
[4700] | 652 | if( this->overlapTest(this->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB)) |
---|
[4695] | 653 | { |
---|
[5044] | 654 | PRINTF(3)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight); |
---|
[5038] | 655 | |
---|
[4695] | 656 | /* check if left node overlaps */ |
---|
[4704] | 657 | if( likely( this->nodeLeft != NULL)) |
---|
| 658 | { |
---|
[4705] | 659 | PRINT(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode->getIndex()); |
---|
[4700] | 660 | if( this->overlapTest(this->nodeLeft->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB)) |
---|
[4704] | 661 | { |
---|
[4700] | 662 | this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB); |
---|
[4704] | 663 | this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB); |
---|
| 664 | } |
---|
| 665 | } |
---|
[4695] | 666 | /* check if right node overlaps */ |
---|
[4704] | 667 | if( likely( this->nodeRight != NULL)) |
---|
| 668 | { |
---|
[4705] | 669 | PRINT(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode->getIndex()); |
---|
[4700] | 670 | if(this->overlapTest(this->nodeRight->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB)) |
---|
[4704] | 671 | { |
---|
| 672 | this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB); |
---|
| 673 | this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB); |
---|
| 674 | } |
---|
[5044] | 675 | } |
---|
[5028] | 676 | |
---|
[5044] | 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 | { |
---|
[5046] | 680 | nodeA->collidesWith(nodeB, *((OBBTreeNode*)treeNode)->bvElement->center); |
---|
| 681 | |
---|
| 682 | nodeB->collidesWith(nodeA, *this->bvElement->center); |
---|
[4704] | 683 | } |
---|
[5044] | 684 | |
---|
[4695] | 685 | } |
---|
| 686 | } |
---|
[4542] | 687 | |
---|
| 688 | |
---|
[4626] | 689 | |
---|
[5028] | 690 | bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, WorldEntity* nodeA, WorldEntity* nodeB) |
---|
[4695] | 691 | { |
---|
[4696] | 692 | /* first check all axis */ |
---|
[4708] | 693 | Vector t; |
---|
[4700] | 694 | float rA = 0.0f; |
---|
| 695 | float rB = 0.0f; |
---|
| 696 | Vector l; |
---|
[4708] | 697 | Vector rotAxisA[3]; |
---|
| 698 | Vector rotAxisB[3]; |
---|
[4626] | 699 | |
---|
[4708] | 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 | |
---|
[4703] | 720 | /* All 3 axis of the object A */ |
---|
[4701] | 721 | for( int j = 0; j < 3; ++j) |
---|
[4705] | 722 | { |
---|
| 723 | rA = 0.0f; |
---|
| 724 | rB = 0.0f; |
---|
[4708] | 725 | l = rotAxisA[j]; |
---|
[4705] | 726 | |
---|
[4708] | 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)); |
---|
[4705] | 730 | |
---|
[4708] | 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)); |
---|
[4705] | 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))) |
---|
[4700] | 738 | { |
---|
[4705] | 739 | PRINT(3)("keine Kollision\n"); |
---|
| 740 | return false; |
---|
| 741 | } |
---|
| 742 | } |
---|
[4700] | 743 | |
---|
[4705] | 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; |
---|
[4708] | 749 | l = rotAxisB[j]; |
---|
[4701] | 750 | |
---|
[4708] | 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)); |
---|
[4700] | 754 | |
---|
[4708] | 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)); |
---|
[4703] | 758 | |
---|
[4705] | 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; |
---|
[4701] | 765 | } |
---|
[4705] | 766 | } |
---|
[4700] | 767 | |
---|
[4705] | 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 ) |
---|
[4701] | 774 | { |
---|
| 775 | rA = 0.0f; |
---|
| 776 | rB = 0.0f; |
---|
[4708] | 777 | l = rotAxisA[j].cross(rotAxisB[k]); |
---|
[4701] | 778 | |
---|
[4708] | 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)); |
---|
[4701] | 782 | |
---|
[4708] | 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)); |
---|
[4701] | 786 | |
---|
[4703] | 787 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
| 788 | |
---|
[4701] | 789 | if( (rA + rB) < fabs(t.dot(l))) |
---|
| 790 | { |
---|
[4705] | 791 | PRINT(3)("keine Kollision\n"); |
---|
[4701] | 792 | return false; |
---|
| 793 | } |
---|
[4703] | 794 | } |
---|
[4705] | 795 | } |
---|
[4701] | 796 | |
---|
| 797 | |
---|
[4705] | 798 | boxA->bCollided = true; /* use this ONLY(!!!!) for drawing operations */ |
---|
| 799 | boxB->bCollided = true; |
---|
| 800 | PRINT(3)("Kollision!\n"); |
---|
| 801 | return true; |
---|
[4695] | 802 | } |
---|
| 803 | |
---|
| 804 | |
---|
[4696] | 805 | |
---|
[4708] | 806 | |
---|
| 807 | |
---|
[5481] | 808 | void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color, bool top) const |
---|
[4553] | 809 | { |
---|
[4635] | 810 | |
---|
| 811 | /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */ |
---|
| 812 | if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL) |
---|
| 813 | { |
---|
[4638] | 814 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4622] | 815 | { |
---|
[4712] | 816 | if( drawMode & DRAW_POINTS) |
---|
| 817 | glBegin(GL_POINTS); |
---|
[4638] | 818 | for(int i = 0; i < this->bvElement->numOfVertices; ++i) |
---|
| 819 | { |
---|
[4712] | 820 | if( drawMode & DRAW_POINTS) |
---|
| 821 | glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
| 822 | else |
---|
| 823 | { |
---|
| 824 | glPushMatrix(); |
---|
| 825 | glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
[5430] | 826 | gluSphere(OBBTreeNode_sphereObj, 0.1, 10, 10); |
---|
[4712] | 827 | glPopMatrix(); |
---|
| 828 | } |
---|
[4638] | 829 | } |
---|
[4712] | 830 | if( drawMode & DRAW_POINTS) |
---|
| 831 | glEnd(); |
---|
[4622] | 832 | } |
---|
[4635] | 833 | } |
---|
[4542] | 834 | |
---|
[5481] | 835 | if (top) |
---|
| 836 | { |
---|
| 837 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 838 | glDisable(GL_LIGHTING); |
---|
| 839 | glDisable(GL_TEXTURE_2D); |
---|
| 840 | } |
---|
| 841 | glColor3f(color.x, color.y, color.z); |
---|
[4542] | 842 | |
---|
[5481] | 843 | |
---|
[4589] | 844 | /* draw world axes */ |
---|
[4676] | 845 | if( drawMode & DRAW_BV_AXIS) |
---|
| 846 | { |
---|
| 847 | glBegin(GL_LINES); |
---|
[5481] | 848 | glColor3f(1.0, 0.0, 0.0); |
---|
[4676] | 849 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 850 | glVertex3f(3.0, 0.0, 0.0); |
---|
[4589] | 851 | |
---|
[5481] | 852 | glColor3f(0.0, 1.0, 0.0); |
---|
[4676] | 853 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 854 | glVertex3f(0.0, 3.0, 0.0); |
---|
[4589] | 855 | |
---|
[5481] | 856 | glColor3f(0.0, 0.0, 1.0); |
---|
[4676] | 857 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 858 | glVertex3f(0.0, 0.0, 3.0); |
---|
| 859 | glEnd(); |
---|
| 860 | } |
---|
[4674] | 861 | |
---|
[4688] | 862 | |
---|
[4635] | 863 | if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL) |
---|
| 864 | { |
---|
[4636] | 865 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 866 | { |
---|
| 867 | /* draw the obb axes */ |
---|
| 868 | glBegin(GL_LINES); |
---|
| 869 | glColor3f(0.0, 0.4, 0.3); |
---|
| 870 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
[4660] | 871 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[0].x * this->bvElement->halfLength[0], |
---|
| 872 | this->bvElement->center->y + this->bvElement->axis[0].y * this->bvElement->halfLength[0], |
---|
| 873 | this->bvElement->center->z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]); |
---|
[4589] | 874 | |
---|
[4635] | 875 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
[4660] | 876 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[1].x * this->bvElement->halfLength[1], |
---|
| 877 | this->bvElement->center->y + this->bvElement->axis[1].y * this->bvElement->halfLength[1], |
---|
| 878 | this->bvElement->center->z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]); |
---|
[4588] | 879 | |
---|
[4635] | 880 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
[4660] | 881 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[2].x * this->bvElement->halfLength[2], |
---|
| 882 | this->bvElement->center->y + this->bvElement->axis[2].y * this->bvElement->halfLength[2], |
---|
| 883 | this->bvElement->center->z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]); |
---|
[4635] | 884 | glEnd(); |
---|
| 885 | } |
---|
| 886 | } |
---|
[4581] | 887 | |
---|
[4588] | 888 | |
---|
[4674] | 889 | /* DRAW POLYGONS */ |
---|
[4673] | 890 | if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED) |
---|
[4635] | 891 | { |
---|
[5487] | 892 | if (top) |
---|
| 893 | { |
---|
| 894 | glEnable(GL_BLEND); |
---|
| 895 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 896 | } |
---|
| 897 | |
---|
[4711] | 898 | if(this->nodeLeft == NULL || this->nodeRight == NULL) |
---|
[4710] | 899 | depth = 0; |
---|
[4636] | 900 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 901 | { |
---|
[4636] | 902 | Vector cen = *this->bvElement->center; |
---|
[4660] | 903 | Vector* axis = this->bvElement->axis; |
---|
[4636] | 904 | float* len = this->bvElement->halfLength; |
---|
[4588] | 905 | |
---|
[4702] | 906 | if( this->bvElement->bCollided) |
---|
[5481] | 907 | { |
---|
[5487] | 908 | glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR |
---|
[5481] | 909 | } |
---|
[4702] | 910 | else if( drawMode & DRAW_BV_BLENDED) |
---|
[5481] | 911 | { |
---|
| 912 | glColor4f(color.x, color.y, color.z, .5); |
---|
| 913 | } |
---|
[4670] | 914 | |
---|
[4636] | 915 | /* draw bounding box */ |
---|
[4670] | 916 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 917 | glBegin(GL_QUADS); |
---|
| 918 | else |
---|
| 919 | glBegin(GL_LINE_LOOP); |
---|
[4660] | 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 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 924 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 925 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 926 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 927 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 928 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 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]); |
---|
[4636] | 932 | glEnd(); |
---|
[4588] | 933 | |
---|
[4670] | 934 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 935 | glBegin(GL_QUADS); |
---|
| 936 | else |
---|
| 937 | glBegin(GL_LINE_LOOP); |
---|
[4660] | 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 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 942 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 943 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 944 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 945 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 946 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 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]); |
---|
[4636] | 950 | glEnd(); |
---|
[4588] | 951 | |
---|
[4670] | 952 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 953 | glBegin(GL_QUADS); |
---|
| 954 | else |
---|
| 955 | glBegin(GL_LINE_LOOP); |
---|
[4660] | 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 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 960 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 961 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 962 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 963 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 964 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 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]); |
---|
[4636] | 968 | glEnd(); |
---|
[4588] | 969 | |
---|
[4670] | 970 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 971 | glBegin(GL_QUADS); |
---|
| 972 | else |
---|
| 973 | glBegin(GL_LINE_LOOP); |
---|
[4660] | 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 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 978 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 979 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 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]); |
---|
[4636] | 986 | glEnd(); |
---|
[4670] | 987 | |
---|
[4671] | 988 | |
---|
[4670] | 989 | if( drawMode & DRAW_BV_BLENDED) |
---|
[4671] | 990 | { |
---|
| 991 | glBegin(GL_QUADS); |
---|
| 992 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 993 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 994 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 995 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 996 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 997 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 998 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 999 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 1000 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 1001 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 1002 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 1003 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 1004 | glEnd(); |
---|
| 1005 | |
---|
| 1006 | glBegin(GL_QUADS); |
---|
| 1007 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 1008 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 1009 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 1010 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 1011 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 1012 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 1013 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 1014 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 1015 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 1016 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 1017 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 1018 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 1019 | glEnd(); |
---|
| 1020 | } |
---|
| 1021 | |
---|
| 1022 | |
---|
| 1023 | if( drawMode & DRAW_BV_BLENDED) |
---|
[5481] | 1024 | glColor3f(color.x, color.y, color.z); |
---|
[4635] | 1025 | } |
---|
[4636] | 1026 | |
---|
[4635] | 1027 | } |
---|
[4588] | 1028 | |
---|
[4674] | 1029 | /* DRAW SEPARATING PLANE */ |
---|
[4635] | 1030 | if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL) |
---|
[4632] | 1031 | { |
---|
[4636] | 1032 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 1033 | { |
---|
[4671] | 1034 | if( drawMode & DRAW_BV_BLENDED) |
---|
[5481] | 1035 | glColor4f(color.x, color.y, color.z, .6); |
---|
[4671] | 1036 | |
---|
[4636] | 1037 | /* now draw the separation plane */ |
---|
[4660] | 1038 | Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3]; |
---|
| 1039 | Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3]; |
---|
[4636] | 1040 | Vector c = *this->bvElement->center; |
---|
| 1041 | float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3]; |
---|
| 1042 | float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3]; |
---|
| 1043 | glBegin(GL_QUADS); |
---|
| 1044 | 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); |
---|
| 1045 | 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); |
---|
| 1046 | 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); |
---|
| 1047 | 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); |
---|
| 1048 | glEnd(); |
---|
[4671] | 1049 | |
---|
| 1050 | if( drawMode & DRAW_BV_BLENDED) |
---|
[5481] | 1051 | glColor4f(color.x, color.y, color.z, 1.0); |
---|
[4671] | 1052 | |
---|
[4635] | 1053 | } |
---|
[4632] | 1054 | } |
---|
[4588] | 1055 | |
---|
[4702] | 1056 | |
---|
| 1057 | |
---|
[5481] | 1058 | if (depth > 0) |
---|
| 1059 | { |
---|
[5487] | 1060 | Vector childColor1 = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(10.0,0.0,0.0)); |
---|
| 1061 | Vector childColor2 = Color::HSVtoRGB(Color::RGBtoHSV(color)-Vector(10.0,0.0,0.0)); |
---|
[5481] | 1062 | if( this->nodeLeft != NULL) |
---|
[5487] | 1063 | this->nodeLeft->drawBV(depth - 1, drawMode, childColor1, false); |
---|
[5481] | 1064 | if( this->nodeRight != NULL) |
---|
[5487] | 1065 | this->nodeRight->drawBV(depth - 1, drawMode, childColor2, false); |
---|
[5481] | 1066 | } |
---|
| 1067 | this->bvElement->bCollided = false; |
---|
[4588] | 1068 | |
---|
[5481] | 1069 | if (top) |
---|
| 1070 | glPopAttrib(); |
---|
[4557] | 1071 | } |
---|
[4542] | 1072 | |
---|
| 1073 | |
---|
[4568] | 1074 | |
---|
[4746] | 1075 | void OBBTreeNode::debug() const |
---|
[4568] | 1076 | { |
---|
| 1077 | |
---|
| 1078 | /* |
---|
| 1079 | for(int i = 0; i < length; i++) |
---|
[4617] | 1080 | { |
---|
[4638] | 1081 | PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]); |
---|
[4617] | 1082 | } |
---|
[4568] | 1083 | */ |
---|
| 1084 | } |
---|