| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "cm_local.h" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
void RotatePoint(vec3_t point, vec3_t matrix[3]) { |
| 45 |
vec3_t tvec; |
| 46 |
|
| 47 |
VectorCopy(point, tvec); |
| 48 |
point[0] = DotProduct(matrix[0], tvec); |
| 49 |
point[1] = DotProduct(matrix[1], tvec); |
| 50 |
point[2] = DotProduct(matrix[2], tvec); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
void TransposeMatrix( vec3_t matrix[3], vec3_t transpose[3]) { |
| 59 |
int i, j; |
| 60 |
for (i = 0; i < 3; i++) { |
| 61 |
for (j = 0; j < 3; j++) { |
| 62 |
transpose[i][j] = matrix[j][i]; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
void CreateRotationMatrix(const vec3_t angles, vec3_t matrix[3]) { |
| 73 |
AngleVectors(angles, matrix[0], matrix[1], matrix[2]); |
| 74 |
VectorInverse(matrix[1]); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void CM_ProjectPointOntoVector( vec3_t point, vec3_t vStart, vec3_t vDir, vec3_t vProj ) |
| 83 |
{ |
| 84 |
vec3_t pVec; |
| 85 |
|
| 86 |
VectorSubtract( point, vStart, pVec ); |
| 87 |
|
| 88 |
VectorMA( vStart, DotProduct( pVec, vDir ), vDir, vProj ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
float CM_DistanceFromLineSquared(vec3_t p, vec3_t lp1, vec3_t lp2, vec3_t dir) { |
| 97 |
vec3_t proj, t; |
| 98 |
int j; |
| 99 |
|
| 100 |
CM_ProjectPointOntoVector(p, lp1, dir, proj); |
| 101 |
for (j = 0; j < 3; j++) |
| 102 |
if ((proj[j] > lp1[j] && proj[j] > lp2[j]) || |
| 103 |
(proj[j] < lp1[j] && proj[j] < lp2[j])) |
| 104 |
break; |
| 105 |
if (j < 3) { |
| 106 |
if (fabs(proj[j] - lp1[j]) < fabs(proj[j] - lp2[j])) |
| 107 |
VectorSubtract(p, lp1, t); |
| 108 |
else |
| 109 |
VectorSubtract(p, lp2, t); |
| 110 |
return VectorLengthSquared(t); |
| 111 |
} |
| 112 |
VectorSubtract(p, proj, t); |
| 113 |
return VectorLengthSquared(t); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
float CM_VectorDistanceSquared(vec3_t p1, vec3_t p2) { |
| 122 |
vec3_t dir; |
| 123 |
|
| 124 |
VectorSubtract(p2, p1, dir); |
| 125 |
return VectorLengthSquared(dir); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
float SquareRootFloat(float number) { |
| 134 |
floatint_t t; |
| 135 |
float x, y; |
| 136 |
const float f = 1.5F; |
| 137 |
|
| 138 |
x = number * 0.5F; |
| 139 |
t.f = number; |
| 140 |
t.i = 0x5f3759df - ( t.i >> 1 ); |
| 141 |
y = t.f; |
| 142 |
y = y * ( f - ( x * y * y ) ); |
| 143 |
y = y * ( f - ( x * y * y ) ); |
| 144 |
return number * y; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
void CM_TestBoxInBrush( traceWork_t *tw, cbrush_t *brush ) { |
| 162 |
int i; |
| 163 |
cplane_t *plane; |
| 164 |
float dist; |
| 165 |
float d1; |
| 166 |
cbrushside_t *side; |
| 167 |
float t; |
| 168 |
vec3_t startp; |
| 169 |
|
| 170 |
if (!brush->numsides) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
if ( tw->bounds[0][0] > brush->bounds[1][0] |
| 176 |
|| tw->bounds[0][1] > brush->bounds[1][1] |
| 177 |
|| tw->bounds[0][2] > brush->bounds[1][2] |
| 178 |
|| tw->bounds[1][0] < brush->bounds[0][0] |
| 179 |
|| tw->bounds[1][1] < brush->bounds[0][1] |
| 180 |
|| tw->bounds[1][2] < brush->bounds[0][2] |
| 181 |
) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
if ( tw->sphere.use ) { |
| 186 |
|
| 187 |
|
| 188 |
for ( i = 6 ; i < brush->numsides ; i++ ) { |
| 189 |
side = brush->sides + i; |
| 190 |
plane = side->plane; |
| 191 |
|
| 192 |
|
| 193 |
dist = plane->dist + tw->sphere.radius; |
| 194 |
|
| 195 |
t = DotProduct( plane->normal, tw->sphere.offset ); |
| 196 |
if ( t > 0 ) |
| 197 |
{ |
| 198 |
VectorSubtract( tw->start, tw->sphere.offset, startp ); |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
VectorAdd( tw->start, tw->sphere.offset, startp ); |
| 203 |
} |
| 204 |
d1 = DotProduct( startp, plane->normal ) - dist; |
| 205 |
|
| 206 |
if ( d1 > 0 ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
} |
| 210 |
} else { |
| 211 |
|
| 212 |
|
| 213 |
for ( i = 6 ; i < brush->numsides ; i++ ) { |
| 214 |
side = brush->sides + i; |
| 215 |
plane = side->plane; |
| 216 |
|
| 217 |
|
| 218 |
dist = plane->dist - DotProduct( tw->offsets[ plane->signbits ], plane->normal ); |
| 219 |
|
| 220 |
d1 = DotProduct( tw->start, plane->normal ) - dist; |
| 221 |
|
| 222 |
|
| 223 |
if ( d1 > 0 ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
tw->trace.startsolid = tw->trace.allsolid = qtrue; |
| 231 |
tw->trace.fraction = 0; |
| 232 |
tw->trace.contents = brush->contents; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
void CM_TestInLeaf( traceWork_t *tw, cLeaf_t *leaf ) { |
| 243 |
int k; |
| 244 |
int brushnum; |
| 245 |
cbrush_t *b; |
| 246 |
cPatch_t *patch; |
| 247 |
|
| 248 |
|
| 249 |
for (k=0 ; k<leaf->numLeafBrushes ; k++) { |
| 250 |
brushnum = cm.leafbrushes[leaf->firstLeafBrush+k]; |
| 251 |
b = &cm.brushes[brushnum]; |
| 252 |
if (b->checkcount == cm.checkcount) { |
| 253 |
continue; |
| 254 |
} |
| 255 |
b->checkcount = cm.checkcount; |
| 256 |
|
| 257 |
if ( !(b->contents & tw->contents)) { |
| 258 |
continue; |
| 259 |
} |
| 260 |
|
| 261 |
CM_TestBoxInBrush( tw, b ); |
| 262 |
if ( tw->trace.allsolid ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
#ifdef BSPC |
| 269 |
if (1) { |
| 270 |
#else |
| 271 |
if ( !cm_noCurves->integer ) { |
| 272 |
#endif |
| 273 |
for ( k = 0 ; k < leaf->numLeafSurfaces ; k++ ) { |
| 274 |
patch = cm.surfaces[ cm.leafsurfaces[ leaf->firstLeafSurface + k ] ]; |
| 275 |
if ( !patch ) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
if ( patch->checkcount == cm.checkcount ) { |
| 279 |
continue; |
| 280 |
} |
| 281 |
patch->checkcount = cm.checkcount; |
| 282 |
|
| 283 |
if ( !(patch->contents & tw->contents)) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
if ( CM_PositionTestInPatchCollide( tw, patch->pc ) ) { |
| 288 |
tw->trace.startsolid = tw->trace.allsolid = qtrue; |
| 289 |
tw->trace.fraction = 0; |
| 290 |
tw->trace.contents = patch->contents; |
| 291 |
return; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
void CM_TestCapsuleInCapsule( traceWork_t *tw, clipHandle_t model ) { |
| 305 |
int i; |
| 306 |
vec3_t mins, maxs; |
| 307 |
vec3_t top, bottom; |
| 308 |
vec3_t p1, p2, tmp; |
| 309 |
vec3_t offset, symetricSize[2]; |
| 310 |
float radius, halfwidth, halfheight, offs, r; |
| 311 |
|
| 312 |
CM_ModelBounds(model, mins, maxs); |
| 313 |
|
| 314 |
VectorAdd(tw->start, tw->sphere.offset, top); |
| 315 |
VectorSubtract(tw->start, tw->sphere.offset, bottom); |
| 316 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 317 |
offset[i] = ( mins[i] + maxs[i] ) * 0.5; |
| 318 |
symetricSize[0][i] = mins[i] - offset[i]; |
| 319 |
symetricSize[1][i] = maxs[i] - offset[i]; |
| 320 |
} |
| 321 |
halfwidth = symetricSize[ 1 ][ 0 ]; |
| 322 |
halfheight = symetricSize[ 1 ][ 2 ]; |
| 323 |
radius = ( halfwidth > halfheight ) ? halfheight : halfwidth; |
| 324 |
offs = halfheight - radius; |
| 325 |
|
| 326 |
r = Square(tw->sphere.radius + radius); |
| 327 |
|
| 328 |
VectorCopy(offset, p1); |
| 329 |
p1[2] += offs; |
| 330 |
VectorSubtract(p1, top, tmp); |
| 331 |
if ( VectorLengthSquared(tmp) < r ) { |
| 332 |
tw->trace.startsolid = tw->trace.allsolid = qtrue; |
| 333 |
tw->trace.fraction = 0; |
| 334 |
} |
| 335 |
VectorSubtract(p1, bottom, tmp); |
| 336 |
if ( VectorLengthSquared(tmp) < r ) { |
| 337 |
tw->trace.startsolid = tw->trace.allsolid = qtrue; |
| 338 |
tw->trace.fraction = 0; |
| 339 |
} |
| 340 |
VectorCopy(offset, p2); |
| 341 |
p2[2] -= offs; |
| 342 |
VectorSubtract(p2, top, tmp); |
| 343 |
if ( VectorLengthSquared(tmp) < r ) { |
| 344 |
tw->trace.startsolid = tw->trace.allsolid = qtrue; |
| 345 |
tw->trace.fraction = 0; |
| 346 |
} |
| 347 |
VectorSubtract(p2, bottom, tmp); |
| 348 |
if ( VectorLengthSquared(tmp) < r ) { |
| 349 |
tw->trace.startsolid = tw->trace.allsolid = qtrue; |
| 350 |
tw->trace.fraction = 0; |
| 351 |
} |
| 352 |
|
| 353 |
if ( (top[2] >= p1[2] && top[2] <= p2[2]) || |
| 354 |
(bottom[2] >= p1[2] && bottom[2] <= p2[2]) ) { |
| 355 |
|
| 356 |
top[2] = p1[2] = 0; |
| 357 |
|
| 358 |
VectorSubtract(top, p1, tmp); |
| 359 |
if ( VectorLengthSquared(tmp) < r ) { |
| 360 |
tw->trace.startsolid = tw->trace.allsolid = qtrue; |
| 361 |
tw->trace.fraction = 0; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
void CM_TestBoundingBoxInCapsule( traceWork_t *tw, clipHandle_t model ) { |
| 374 |
vec3_t mins, maxs, offset, size[2]; |
| 375 |
clipHandle_t h; |
| 376 |
cmodel_t *cmod; |
| 377 |
int i; |
| 378 |
|
| 379 |
|
| 380 |
CM_ModelBounds(model, mins, maxs); |
| 381 |
|
| 382 |
|
| 383 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 384 |
offset[i] = ( mins[i] + maxs[i] ) * 0.5; |
| 385 |
size[0][i] = mins[i] - offset[i]; |
| 386 |
size[1][i] = maxs[i] - offset[i]; |
| 387 |
tw->start[i] -= offset[i]; |
| 388 |
tw->end[i] -= offset[i]; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
tw->sphere.use = qtrue; |
| 393 |
tw->sphere.radius = ( size[1][0] > size[1][2] ) ? size[1][2]: size[1][0]; |
| 394 |
tw->sphere.halfheight = size[1][2]; |
| 395 |
VectorSet( tw->sphere.offset, 0, 0, size[1][2] - tw->sphere.radius ); |
| 396 |
|
| 397 |
|
| 398 |
h = CM_TempBoxModel(tw->size[0], tw->size[1], qfalse); |
| 399 |
|
| 400 |
cmod = CM_ClipHandleToModel( h ); |
| 401 |
CM_TestInLeaf( tw, &cmod->leaf ); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
#define MAX_POSITION_LEAFS 1024 |
| 410 |
void CM_PositionTest( traceWork_t *tw ) { |
| 411 |
int leafs[MAX_POSITION_LEAFS]; |
| 412 |
int i; |
| 413 |
leafList_t ll; |
| 414 |
|
| 415 |
|
| 416 |
VectorAdd( tw->start, tw->size[0], ll.bounds[0] ); |
| 417 |
VectorAdd( tw->start, tw->size[1], ll.bounds[1] ); |
| 418 |
|
| 419 |
for (i=0 ; i<3 ; i++) { |
| 420 |
ll.bounds[0][i] -= 1; |
| 421 |
ll.bounds[1][i] += 1; |
| 422 |
} |
| 423 |
|
| 424 |
ll.count = 0; |
| 425 |
ll.maxcount = MAX_POSITION_LEAFS; |
| 426 |
ll.list = leafs; |
| 427 |
ll.storeLeafs = CM_StoreLeafs; |
| 428 |
ll.lastLeaf = 0; |
| 429 |
ll.overflowed = qfalse; |
| 430 |
|
| 431 |
cm.checkcount++; |
| 432 |
|
| 433 |
CM_BoxLeafnums_r( &ll, 0 ); |
| 434 |
|
| 435 |
|
| 436 |
cm.checkcount++; |
| 437 |
|
| 438 |
|
| 439 |
for (i=0 ; i < ll.count ; i++) { |
| 440 |
CM_TestInLeaf( tw, &cm.leafs[leafs[i]] ); |
| 441 |
if ( tw->trace.allsolid ) { |
| 442 |
break; |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
void CM_TraceThroughPatch( traceWork_t *tw, cPatch_t *patch ) { |
| 463 |
float oldFrac; |
| 464 |
|
| 465 |
c_patch_traces++; |
| 466 |
|
| 467 |
oldFrac = tw->trace.fraction; |
| 468 |
|
| 469 |
CM_TraceThroughPatchCollide( tw, patch->pc ); |
| 470 |
|
| 471 |
if ( tw->trace.fraction < oldFrac ) { |
| 472 |
tw->trace.surfaceFlags = patch->surfaceFlags; |
| 473 |
tw->trace.contents = patch->contents; |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
void CM_TraceThroughBrush( traceWork_t *tw, cbrush_t *brush ) { |
| 483 |
int i; |
| 484 |
cplane_t *plane, *clipplane; |
| 485 |
float dist; |
| 486 |
float enterFrac, leaveFrac; |
| 487 |
float d1, d2; |
| 488 |
qboolean getout, startout; |
| 489 |
float f; |
| 490 |
cbrushside_t *side, *leadside; |
| 491 |
float t; |
| 492 |
vec3_t startp; |
| 493 |
vec3_t endp; |
| 494 |
|
| 495 |
enterFrac = -1.0; |
| 496 |
leaveFrac = 1.0; |
| 497 |
clipplane = NULL; |
| 498 |
|
| 499 |
if ( !brush->numsides ) { |
| 500 |
return; |
| 501 |
} |
| 502 |
|
| 503 |
c_brush_traces++; |
| 504 |
|
| 505 |
getout = qfalse; |
| 506 |
startout = qfalse; |
| 507 |
|
| 508 |
leadside = NULL; |
| 509 |
|
| 510 |
if ( tw->sphere.use ) { |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
for (i = 0; i < brush->numsides; i++) { |
| 517 |
side = brush->sides + i; |
| 518 |
plane = side->plane; |
| 519 |
|
| 520 |
|
| 521 |
dist = plane->dist + tw->sphere.radius; |
| 522 |
|
| 523 |
|
| 524 |
t = DotProduct( plane->normal, tw->sphere.offset ); |
| 525 |
if ( t > 0 ) |
| 526 |
{ |
| 527 |
VectorSubtract( tw->start, tw->sphere.offset, startp ); |
| 528 |
VectorSubtract( tw->end, tw->sphere.offset, endp ); |
| 529 |
} |
| 530 |
else |
| 531 |
{ |
| 532 |
VectorAdd( tw->start, tw->sphere.offset, startp ); |
| 533 |
VectorAdd( tw->end, tw->sphere.offset, endp ); |
| 534 |
} |
| 535 |
|
| 536 |
d1 = DotProduct( startp, plane->normal ) - dist; |
| 537 |
d2 = DotProduct( endp, plane->normal ) - dist; |
| 538 |
|
| 539 |
if (d2 > 0) { |
| 540 |
getout = qtrue; |
| 541 |
} |
| 542 |
if (d1 > 0) { |
| 543 |
startout = qtrue; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
if (d1 > 0 && ( d2 >= SURFACE_CLIP_EPSILON || d2 >= d1 ) ) { |
| 548 |
return; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
if (d1 <= 0 && d2 <= 0 ) { |
| 553 |
continue; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
if (d1 > d2) { |
| 558 |
f = (d1-SURFACE_CLIP_EPSILON) / (d1-d2); |
| 559 |
if ( f < 0 ) { |
| 560 |
f = 0; |
| 561 |
} |
| 562 |
if (f > enterFrac) { |
| 563 |
enterFrac = f; |
| 564 |
clipplane = plane; |
| 565 |
leadside = side; |
| 566 |
} |
| 567 |
} else { |
| 568 |
f = (d1+SURFACE_CLIP_EPSILON) / (d1-d2); |
| 569 |
if ( f > 1 ) { |
| 570 |
f = 1; |
| 571 |
} |
| 572 |
if (f < leaveFrac) { |
| 573 |
leaveFrac = f; |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
} else { |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
for (i = 0; i < brush->numsides; i++) { |
| 584 |
side = brush->sides + i; |
| 585 |
plane = side->plane; |
| 586 |
|
| 587 |
|
| 588 |
dist = plane->dist - DotProduct( tw->offsets[ plane->signbits ], plane->normal ); |
| 589 |
|
| 590 |
d1 = DotProduct( tw->start, plane->normal ) - dist; |
| 591 |
d2 = DotProduct( tw->end, plane->normal ) - dist; |
| 592 |
|
| 593 |
if (d2 > 0) { |
| 594 |
getout = qtrue; |
| 595 |
} |
| 596 |
if (d1 > 0) { |
| 597 |
startout = qtrue; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
if (d1 > 0 && ( d2 >= SURFACE_CLIP_EPSILON || d2 >= d1 ) ) { |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
if (d1 <= 0 && d2 <= 0 ) { |
| 607 |
continue; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
if (d1 > d2) { |
| 612 |
f = (d1-SURFACE_CLIP_EPSILON) / (d1-d2); |
| 613 |
if ( f < 0 ) { |
| 614 |
f = 0; |
| 615 |
} |
| 616 |
if (f > enterFrac) { |
| 617 |
enterFrac = f; |
| 618 |
clipplane = plane; |
| 619 |
leadside = side; |
| 620 |
} |
| 621 |
} else { |
| 622 |
f = (d1+SURFACE_CLIP_EPSILON) / (d1-d2); |
| 623 |
if ( f > 1 ) { |
| 624 |
f = 1; |
| 625 |
} |
| 626 |
if (f < leaveFrac) { |
| 627 |
leaveFrac = f; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
if (!startout) { |
| 638 |
tw->trace.startsolid = qtrue; |
| 639 |
if (!getout) { |
| 640 |
tw->trace.allsolid = qtrue; |
| 641 |
tw->trace.fraction = 0; |
| 642 |
tw->trace.contents = brush->contents; |
| 643 |
} |
| 644 |
return; |
| 645 |
} |
| 646 |
|
| 647 |
if (enterFrac < leaveFrac) { |
| 648 |
if (enterFrac > -1 && enterFrac < tw->trace.fraction) { |
| 649 |
if (enterFrac < 0) { |
| 650 |
enterFrac = 0; |
| 651 |
} |
| 652 |
tw->trace.fraction = enterFrac; |
| 653 |
if (clipplane != NULL) { |
| 654 |
tw->trace.plane = *clipplane; |
| 655 |
} |
| 656 |
if (leadside != NULL) { |
| 657 |
tw->trace.surfaceFlags = leadside->surfaceFlags; |
| 658 |
} |
| 659 |
tw->trace.contents = brush->contents; |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
void CM_TraceThroughLeaf( traceWork_t *tw, cLeaf_t *leaf ) { |
| 670 |
int k; |
| 671 |
int brushnum; |
| 672 |
cbrush_t *b; |
| 673 |
cPatch_t *patch; |
| 674 |
|
| 675 |
|
| 676 |
for ( k = 0 ; k < leaf->numLeafBrushes ; k++ ) { |
| 677 |
brushnum = cm.leafbrushes[leaf->firstLeafBrush+k]; |
| 678 |
|
| 679 |
b = &cm.brushes[brushnum]; |
| 680 |
if ( b->checkcount == cm.checkcount ) { |
| 681 |
continue; |
| 682 |
} |
| 683 |
b->checkcount = cm.checkcount; |
| 684 |
|
| 685 |
if ( !(b->contents & tw->contents) ) { |
| 686 |
continue; |
| 687 |
} |
| 688 |
|
| 689 |
if ( !CM_BoundsIntersect( tw->bounds[0], tw->bounds[1], |
| 690 |
b->bounds[0], b->bounds[1] ) ) { |
| 691 |
continue; |
| 692 |
} |
| 693 |
|
| 694 |
CM_TraceThroughBrush( tw, b ); |
| 695 |
if ( !tw->trace.fraction ) { |
| 696 |
return; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
#ifdef BSPC |
| 702 |
if (1) { |
| 703 |
#else |
| 704 |
if ( !cm_noCurves->integer ) { |
| 705 |
#endif |
| 706 |
for ( k = 0 ; k < leaf->numLeafSurfaces ; k++ ) { |
| 707 |
patch = cm.surfaces[ cm.leafsurfaces[ leaf->firstLeafSurface + k ] ]; |
| 708 |
if ( !patch ) { |
| 709 |
continue; |
| 710 |
} |
| 711 |
if ( patch->checkcount == cm.checkcount ) { |
| 712 |
continue; |
| 713 |
} |
| 714 |
patch->checkcount = cm.checkcount; |
| 715 |
|
| 716 |
if ( !(patch->contents & tw->contents) ) { |
| 717 |
continue; |
| 718 |
} |
| 719 |
|
| 720 |
CM_TraceThroughPatch( tw, patch ); |
| 721 |
if ( !tw->trace.fraction ) { |
| 722 |
return; |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
#define RADIUS_EPSILON 1.0f |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
void CM_TraceThroughSphere( traceWork_t *tw, vec3_t origin, float radius, vec3_t start, vec3_t end ) { |
| 738 |
float l1, l2, length, scale, fraction; |
| 739 |
|
| 740 |
float b, c, d, sqrtd; |
| 741 |
vec3_t v1, dir, intersection; |
| 742 |
|
| 743 |
|
| 744 |
VectorSubtract(start, origin, dir); |
| 745 |
l1 = VectorLengthSquared(dir); |
| 746 |
if (l1 < Square(radius)) { |
| 747 |
tw->trace.fraction = 0; |
| 748 |
tw->trace.startsolid = qtrue; |
| 749 |
|
| 750 |
VectorSubtract(end, origin, dir); |
| 751 |
l1 = VectorLengthSquared(dir); |
| 752 |
if (l1 < Square(radius)) { |
| 753 |
tw->trace.allsolid = qtrue; |
| 754 |
} |
| 755 |
return; |
| 756 |
} |
| 757 |
|
| 758 |
VectorSubtract(end, start, dir); |
| 759 |
length = VectorNormalize(dir); |
| 760 |
|
| 761 |
l1 = CM_DistanceFromLineSquared(origin, start, end, dir); |
| 762 |
VectorSubtract(end, origin, v1); |
| 763 |
l2 = VectorLengthSquared(v1); |
| 764 |
|
| 765 |
if (l1 >= Square(radius) && l2 > Square(radius+SURFACE_CLIP_EPSILON)) { |
| 766 |
return; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
VectorSubtract(start, origin, v1); |
| 775 |
|
| 776 |
|
| 777 |
b = 2.0f * (dir[0] * v1[0] + dir[1] * v1[1] + dir[2] * v1[2]); |
| 778 |
c = v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2] - (radius+RADIUS_EPSILON) * (radius+RADIUS_EPSILON); |
| 779 |
|
| 780 |
d = b * b - 4.0f * c; |
| 781 |
if (d > 0) { |
| 782 |
sqrtd = SquareRootFloat(d); |
| 783 |
|
| 784 |
fraction = (- b - sqrtd) * 0.5f; |
| 785 |
|
| 786 |
if (fraction < 0) { |
| 787 |
fraction = 0; |
| 788 |
} |
| 789 |
else { |
| 790 |
fraction /= length; |
| 791 |
} |
| 792 |
if ( fraction < tw->trace.fraction ) { |
| 793 |
tw->trace.fraction = fraction; |
| 794 |
VectorSubtract(end, start, dir); |
| 795 |
VectorMA(start, fraction, dir, intersection); |
| 796 |
VectorSubtract(intersection, origin, dir); |
| 797 |
#ifdef CAPSULE_DEBUG |
| 798 |
l2 = VectorLength(dir); |
| 799 |
if (l2 < radius) { |
| 800 |
int bah = 1; |
| 801 |
} |
| 802 |
#endif |
| 803 |
scale = 1 / (radius+RADIUS_EPSILON); |
| 804 |
VectorScale(dir, scale, dir); |
| 805 |
VectorCopy(dir, tw->trace.plane.normal); |
| 806 |
VectorAdd( tw->modelOrigin, intersection, intersection); |
| 807 |
tw->trace.plane.dist = DotProduct(tw->trace.plane.normal, intersection); |
| 808 |
tw->trace.contents = CONTENTS_BODY; |
| 809 |
} |
| 810 |
} |
| 811 |
else if (d == 0) { |
| 812 |
|
| 813 |
|
| 814 |
} |
| 815 |
|
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
void CM_TraceThroughVerticalCylinder( traceWork_t *tw, vec3_t origin, float radius, float halfheight, vec3_t start, vec3_t end) { |
| 827 |
float length, scale, fraction, l1, l2; |
| 828 |
|
| 829 |
float b, c, d, sqrtd; |
| 830 |
vec3_t v1, dir, start2d, end2d, org2d, intersection; |
| 831 |
|
| 832 |
|
| 833 |
VectorSet(start2d, start[0], start[1], 0); |
| 834 |
VectorSet(end2d, end[0], end[1], 0); |
| 835 |
VectorSet(org2d, origin[0], origin[1], 0); |
| 836 |
|
| 837 |
if (start[2] <= origin[2] + halfheight && |
| 838 |
start[2] >= origin[2] - halfheight) { |
| 839 |
|
| 840 |
VectorSubtract(start2d, org2d, dir); |
| 841 |
l1 = VectorLengthSquared(dir); |
| 842 |
if (l1 < Square(radius)) { |
| 843 |
tw->trace.fraction = 0; |
| 844 |
tw->trace.startsolid = qtrue; |
| 845 |
VectorSubtract(end2d, org2d, dir); |
| 846 |
l1 = VectorLengthSquared(dir); |
| 847 |
if (l1 < Square(radius)) { |
| 848 |
tw->trace.allsolid = qtrue; |
| 849 |
} |
| 850 |
return; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
VectorSubtract(end2d, start2d, dir); |
| 855 |
length = VectorNormalize(dir); |
| 856 |
|
| 857 |
l1 = CM_DistanceFromLineSquared(org2d, start2d, end2d, dir); |
| 858 |
VectorSubtract(end2d, org2d, v1); |
| 859 |
l2 = VectorLengthSquared(v1); |
| 860 |
|
| 861 |
if (l1 >= Square(radius) && l2 > Square(radius+SURFACE_CLIP_EPSILON)) { |
| 862 |
return; |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
VectorSubtract(start, origin, v1); |
| 874 |
|
| 875 |
|
| 876 |
b = 2.0f * (v1[0] * dir[0] + v1[1] * dir[1]); |
| 877 |
c = v1[0] * v1[0] + v1[1] * v1[1] - (radius+RADIUS_EPSILON) * (radius+RADIUS_EPSILON); |
| 878 |
|
| 879 |
d = b * b - 4.0f * c; |
| 880 |
if (d > 0) { |
| 881 |
sqrtd = SquareRootFloat(d); |
| 882 |
|
| 883 |
fraction = (- b - sqrtd) * 0.5f; |
| 884 |
|
| 885 |
if (fraction < 0) { |
| 886 |
fraction = 0; |
| 887 |
} |
| 888 |
else { |
| 889 |
fraction /= length; |
| 890 |
} |
| 891 |
if ( fraction < tw->trace.fraction ) { |
| 892 |
VectorSubtract(end, start, dir); |
| 893 |
VectorMA(start, fraction, dir, intersection); |
| 894 |
|
| 895 |
if (intersection[2] <= origin[2] + halfheight && |
| 896 |
intersection[2] >= origin[2] - halfheight) { |
| 897 |
|
| 898 |
tw->trace.fraction = fraction; |
| 899 |
VectorSubtract(intersection, origin, dir); |
| 900 |
dir[2] = 0; |
| 901 |
#ifdef CAPSULE_DEBUG |
| 902 |
l2 = VectorLength(dir); |
| 903 |
if (l2 <= radius) { |
| 904 |
int bah = 1; |
| 905 |
} |
| 906 |
#endif |
| 907 |
scale = 1 / (radius+RADIUS_EPSILON); |
| 908 |
VectorScale(dir, scale, dir); |
| 909 |
VectorCopy(dir, tw->trace.plane.normal); |
| 910 |
VectorAdd( tw->modelOrigin, intersection, intersection); |
| 911 |
tw->trace.plane.dist = DotProduct(tw->trace.plane.normal, intersection); |
| 912 |
tw->trace.contents = CONTENTS_BODY; |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
else if (d == 0) { |
| 917 |
|
| 918 |
|
| 919 |
} |
| 920 |
|
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
void CM_TraceCapsuleThroughCapsule( traceWork_t *tw, clipHandle_t model ) { |
| 931 |
int i; |
| 932 |
vec3_t mins, maxs; |
| 933 |
vec3_t top, bottom, starttop, startbottom, endtop, endbottom; |
| 934 |
vec3_t offset, symetricSize[2]; |
| 935 |
float radius, halfwidth, halfheight, offs, h; |
| 936 |
|
| 937 |
CM_ModelBounds(model, mins, maxs); |
| 938 |
|
| 939 |
if ( tw->bounds[0][0] > maxs[0] + RADIUS_EPSILON |
| 940 |
|| tw->bounds[0][1] > maxs[1] + RADIUS_EPSILON |
| 941 |
|| tw->bounds[0][2] > maxs[2] + RADIUS_EPSILON |
| 942 |
|| tw->bounds[1][0] < mins[0] - RADIUS_EPSILON |
| 943 |
|| tw->bounds[1][1] < mins[1] - RADIUS_EPSILON |
| 944 |
|| tw->bounds[1][2] < mins[2] - RADIUS_EPSILON |
| 945 |
) { |
| 946 |
return; |
| 947 |
} |
| 948 |
|
| 949 |
VectorAdd(tw->start, tw->sphere.offset, starttop); |
| 950 |
VectorSubtract(tw->start, tw->sphere.offset, startbottom); |
| 951 |
VectorAdd(tw->end, tw->sphere.offset, endtop); |
| 952 |
VectorSubtract(tw->end, tw->sphere.offset, endbottom); |
| 953 |
|
| 954 |
|
| 955 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 956 |
offset[i] = ( mins[i] + maxs[i] ) * 0.5; |
| 957 |
symetricSize[0][i] = mins[i] - offset[i]; |
| 958 |
symetricSize[1][i] = maxs[i] - offset[i]; |
| 959 |
} |
| 960 |
halfwidth = symetricSize[ 1 ][ 0 ]; |
| 961 |
halfheight = symetricSize[ 1 ][ 2 ]; |
| 962 |
radius = ( halfwidth > halfheight ) ? halfheight : halfwidth; |
| 963 |
offs = halfheight - radius; |
| 964 |
VectorCopy(offset, top); |
| 965 |
top[2] += offs; |
| 966 |
VectorCopy(offset, bottom); |
| 967 |
bottom[2] -= offs; |
| 968 |
|
| 969 |
radius += tw->sphere.radius; |
| 970 |
|
| 971 |
if ( tw->start[0] != tw->end[0] || tw->start[1] != tw->end[1] ) { |
| 972 |
|
| 973 |
h = halfheight + tw->sphere.halfheight - radius; |
| 974 |
|
| 975 |
if ( h > 0 ) { |
| 976 |
|
| 977 |
CM_TraceThroughVerticalCylinder(tw, offset, radius, h, tw->start, tw->end); |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
CM_TraceThroughSphere(tw, top, radius, startbottom, endbottom); |
| 982 |
CM_TraceThroughSphere(tw, bottom, radius, starttop, endtop); |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
void CM_TraceBoundingBoxThroughCapsule( traceWork_t *tw, clipHandle_t model ) { |
| 993 |
vec3_t mins, maxs, offset, size[2]; |
| 994 |
clipHandle_t h; |
| 995 |
cmodel_t *cmod; |
| 996 |
int i; |
| 997 |
|
| 998 |
|
| 999 |
CM_ModelBounds(model, mins, maxs); |
| 1000 |
|
| 1001 |
|
| 1002 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 1003 |
offset[i] = ( mins[i] + maxs[i] ) * 0.5; |
| 1004 |
size[0][i] = mins[i] - offset[i]; |
| 1005 |
size[1][i] = maxs[i] - offset[i]; |
| 1006 |
tw->start[i] -= offset[i]; |
| 1007 |
tw->end[i] -= offset[i]; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
tw->sphere.use = qtrue; |
| 1012 |
tw->sphere.radius = ( size[1][0] > size[1][2] ) ? size[1][2]: size[1][0]; |
| 1013 |
tw->sphere.halfheight = size[1][2]; |
| 1014 |
VectorSet( tw->sphere.offset, 0, 0, size[1][2] - tw->sphere.radius ); |
| 1015 |
|
| 1016 |
|
| 1017 |
h = CM_TempBoxModel(tw->size[0], tw->size[1], qfalse); |
| 1018 |
|
| 1019 |
cmod = CM_ClipHandleToModel( h ); |
| 1020 |
CM_TraceThroughLeaf( tw, &cmod->leaf ); |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
void CM_TraceThroughTree( traceWork_t *tw, int num, float p1f, float p2f, vec3_t p1, vec3_t p2) { |
| 1036 |
cNode_t *node; |
| 1037 |
cplane_t *plane; |
| 1038 |
float t1, t2, offset; |
| 1039 |
float frac, frac2; |
| 1040 |
float idist; |
| 1041 |
vec3_t mid; |
| 1042 |
int side; |
| 1043 |
float midf; |
| 1044 |
|
| 1045 |
if (tw->trace.fraction <= p1f) { |
| 1046 |
return; |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
if (num < 0) { |
| 1051 |
CM_TraceThroughLeaf( tw, &cm.leafs[-1-num] ); |
| 1052 |
return; |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
node = cm.nodes + num; |
| 1060 |
plane = node->plane; |
| 1061 |
|
| 1062 |
|
| 1063 |
if ( plane->type < 3 ) { |
| 1064 |
t1 = p1[plane->type] - plane->dist; |
| 1065 |
t2 = p2[plane->type] - plane->dist; |
| 1066 |
offset = tw->extents[plane->type]; |
| 1067 |
} else { |
| 1068 |
t1 = DotProduct (plane->normal, p1) - plane->dist; |
| 1069 |
t2 = DotProduct (plane->normal, p2) - plane->dist; |
| 1070 |
if ( tw->isPoint ) { |
| 1071 |
offset = 0; |
| 1072 |
} else { |
| 1073 |
|
| 1074 |
offset = 2048; |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
if ( t1 >= offset + 1 && t2 >= offset + 1 ) { |
| 1080 |
CM_TraceThroughTree( tw, node->children[0], p1f, p2f, p1, p2 ); |
| 1081 |
return; |
| 1082 |
} |
| 1083 |
if ( t1 < -offset - 1 && t2 < -offset - 1 ) { |
| 1084 |
CM_TraceThroughTree( tw, node->children[1], p1f, p2f, p1, p2 ); |
| 1085 |
return; |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
if ( t1 < t2 ) { |
| 1090 |
idist = 1.0/(t1-t2); |
| 1091 |
side = 1; |
| 1092 |
frac2 = (t1 + offset + SURFACE_CLIP_EPSILON)*idist; |
| 1093 |
frac = (t1 - offset + SURFACE_CLIP_EPSILON)*idist; |
| 1094 |
} else if (t1 > t2) { |
| 1095 |
idist = 1.0/(t1-t2); |
| 1096 |
side = 0; |
| 1097 |
frac2 = (t1 - offset - SURFACE_CLIP_EPSILON)*idist; |
| 1098 |
frac = (t1 + offset + SURFACE_CLIP_EPSILON)*idist; |
| 1099 |
} else { |
| 1100 |
side = 0; |
| 1101 |
frac = 1; |
| 1102 |
frac2 = 0; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
if ( frac < 0 ) { |
| 1107 |
frac = 0; |
| 1108 |
} |
| 1109 |
if ( frac > 1 ) { |
| 1110 |
frac = 1; |
| 1111 |
} |
| 1112 |
|
| 1113 |
midf = p1f + (p2f - p1f)*frac; |
| 1114 |
|
| 1115 |
mid[0] = p1[0] + frac*(p2[0] - p1[0]); |
| 1116 |
mid[1] = p1[1] + frac*(p2[1] - p1[1]); |
| 1117 |
mid[2] = p1[2] + frac*(p2[2] - p1[2]); |
| 1118 |
|
| 1119 |
CM_TraceThroughTree( tw, node->children[side], p1f, midf, p1, mid ); |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
if ( frac2 < 0 ) { |
| 1124 |
frac2 = 0; |
| 1125 |
} |
| 1126 |
if ( frac2 > 1 ) { |
| 1127 |
frac2 = 1; |
| 1128 |
} |
| 1129 |
|
| 1130 |
midf = p1f + (p2f - p1f)*frac2; |
| 1131 |
|
| 1132 |
mid[0] = p1[0] + frac2*(p2[0] - p1[0]); |
| 1133 |
mid[1] = p1[1] + frac2*(p2[1] - p1[1]); |
| 1134 |
mid[2] = p1[2] + frac2*(p2[2] - p1[2]); |
| 1135 |
|
| 1136 |
CM_TraceThroughTree( tw, node->children[side^1], midf, p2f, mid, p2 ); |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
void CM_Trace( trace_t *results, const vec3_t start, const vec3_t end, vec3_t mins, vec3_t maxs, |
| 1149 |
clipHandle_t model, const vec3_t origin, int brushmask, int capsule, sphere_t *sphere ) { |
| 1150 |
int i; |
| 1151 |
traceWork_t tw; |
| 1152 |
vec3_t offset; |
| 1153 |
cmodel_t *cmod; |
| 1154 |
|
| 1155 |
cmod = CM_ClipHandleToModel( model ); |
| 1156 |
|
| 1157 |
cm.checkcount++; |
| 1158 |
|
| 1159 |
c_traces++; |
| 1160 |
|
| 1161 |
|
| 1162 |
Com_Memset( &tw, 0, sizeof(tw) ); |
| 1163 |
tw.trace.fraction = 1; |
| 1164 |
VectorCopy(origin, tw.modelOrigin); |
| 1165 |
|
| 1166 |
if (!cm.numNodes) { |
| 1167 |
*results = tw.trace; |
| 1168 |
|
| 1169 |
return; |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
if ( !mins ) { |
| 1174 |
mins = vec3_origin; |
| 1175 |
} |
| 1176 |
if ( !maxs ) { |
| 1177 |
maxs = vec3_origin; |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
tw.contents = brushmask; |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 1187 |
offset[i] = ( mins[i] + maxs[i] ) * 0.5; |
| 1188 |
tw.size[0][i] = mins[i] - offset[i]; |
| 1189 |
tw.size[1][i] = maxs[i] - offset[i]; |
| 1190 |
tw.start[i] = start[i] + offset[i]; |
| 1191 |
tw.end[i] = end[i] + offset[i]; |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
if ( sphere ) { |
| 1196 |
tw.sphere = *sphere; |
| 1197 |
} |
| 1198 |
else { |
| 1199 |
tw.sphere.use = capsule; |
| 1200 |
tw.sphere.radius = ( tw.size[1][0] > tw.size[1][2] ) ? tw.size[1][2]: tw.size[1][0]; |
| 1201 |
tw.sphere.halfheight = tw.size[1][2]; |
| 1202 |
VectorSet( tw.sphere.offset, 0, 0, tw.size[1][2] - tw.sphere.radius ); |
| 1203 |
} |
| 1204 |
|
| 1205 |
tw.maxOffset = tw.size[1][0] + tw.size[1][1] + tw.size[1][2]; |
| 1206 |
|
| 1207 |
|
| 1208 |
tw.offsets[0][0] = tw.size[0][0]; |
| 1209 |
tw.offsets[0][1] = tw.size[0][1]; |
| 1210 |
tw.offsets[0][2] = tw.size[0][2]; |
| 1211 |
|
| 1212 |
tw.offsets[1][0] = tw.size[1][0]; |
| 1213 |
tw.offsets[1][1] = tw.size[0][1]; |
| 1214 |
tw.offsets[1][2] = tw.size[0][2]; |
| 1215 |
|
| 1216 |
tw.offsets[2][0] = tw.size[0][0]; |
| 1217 |
tw.offsets[2][1] = tw.size[1][1]; |
| 1218 |
tw.offsets[2][2] = tw.size[0][2]; |
| 1219 |
|
| 1220 |
tw.offsets[3][0] = tw.size[1][0]; |
| 1221 |
tw.offsets[3][1] = tw.size[1][1]; |
| 1222 |
tw.offsets[3][2] = tw.size[0][2]; |
| 1223 |
|
| 1224 |
tw.offsets[4][0] = tw.size[0][0]; |
| 1225 |
tw.offsets[4][1] = tw.size[0][1]; |
| 1226 |
tw.offsets[4][2] = tw.size[1][2]; |
| 1227 |
|
| 1228 |
tw.offsets[5][0] = tw.size[1][0]; |
| 1229 |
tw.offsets[5][1] = tw.size[0][1]; |
| 1230 |
tw.offsets[5][2] = tw.size[1][2]; |
| 1231 |
|
| 1232 |
tw.offsets[6][0] = tw.size[0][0]; |
| 1233 |
tw.offsets[6][1] = tw.size[1][1]; |
| 1234 |
tw.offsets[6][2] = tw.size[1][2]; |
| 1235 |
|
| 1236 |
tw.offsets[7][0] = tw.size[1][0]; |
| 1237 |
tw.offsets[7][1] = tw.size[1][1]; |
| 1238 |
tw.offsets[7][2] = tw.size[1][2]; |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
if ( tw.sphere.use ) { |
| 1244 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 1245 |
if ( tw.start[i] < tw.end[i] ) { |
| 1246 |
tw.bounds[0][i] = tw.start[i] - fabs(tw.sphere.offset[i]) - tw.sphere.radius; |
| 1247 |
tw.bounds[1][i] = tw.end[i] + fabs(tw.sphere.offset[i]) + tw.sphere.radius; |
| 1248 |
} else { |
| 1249 |
tw.bounds[0][i] = tw.end[i] - fabs(tw.sphere.offset[i]) - tw.sphere.radius; |
| 1250 |
tw.bounds[1][i] = tw.start[i] + fabs(tw.sphere.offset[i]) + tw.sphere.radius; |
| 1251 |
} |
| 1252 |
} |
| 1253 |
} |
| 1254 |
else { |
| 1255 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 1256 |
if ( tw.start[i] < tw.end[i] ) { |
| 1257 |
tw.bounds[0][i] = tw.start[i] + tw.size[0][i]; |
| 1258 |
tw.bounds[1][i] = tw.end[i] + tw.size[1][i]; |
| 1259 |
} else { |
| 1260 |
tw.bounds[0][i] = tw.end[i] + tw.size[0][i]; |
| 1261 |
tw.bounds[1][i] = tw.start[i] + tw.size[1][i]; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
if (start[0] == end[0] && start[1] == end[1] && start[2] == end[2]) { |
| 1270 |
if ( model ) { |
| 1271 |
#ifdef ALWAYS_BBOX_VS_BBOX |
| 1272 |
if ( model == BOX_MODEL_HANDLE || model == CAPSULE_MODEL_HANDLE) { |
| 1273 |
tw.sphere.use = qfalse; |
| 1274 |
CM_TestInLeaf( &tw, &cmod->leaf ); |
| 1275 |
} |
| 1276 |
else |
| 1277 |
#elif defined(ALWAYS_CAPSULE_VS_CAPSULE) |
| 1278 |
if ( model == BOX_MODEL_HANDLE || model == CAPSULE_MODEL_HANDLE) { |
| 1279 |
CM_TestCapsuleInCapsule( &tw, model ); |
| 1280 |
} |
| 1281 |
else |
| 1282 |
#endif |
| 1283 |
if ( model == CAPSULE_MODEL_HANDLE ) { |
| 1284 |
if ( tw.sphere.use ) { |
| 1285 |
CM_TestCapsuleInCapsule( &tw, model ); |
| 1286 |
} |
| 1287 |
else { |
| 1288 |
CM_TestBoundingBoxInCapsule( &tw, model ); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
else { |
| 1292 |
CM_TestInLeaf( &tw, &cmod->leaf ); |
| 1293 |
} |
| 1294 |
} else { |
| 1295 |
CM_PositionTest( &tw ); |
| 1296 |
} |
| 1297 |
} else { |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
if ( tw.size[0][0] == 0 && tw.size[0][1] == 0 && tw.size[0][2] == 0 ) { |
| 1302 |
tw.isPoint = qtrue; |
| 1303 |
VectorClear( tw.extents ); |
| 1304 |
} else { |
| 1305 |
tw.isPoint = qfalse; |
| 1306 |
tw.extents[0] = tw.size[1][0]; |
| 1307 |
tw.extents[1] = tw.size[1][1]; |
| 1308 |
tw.extents[2] = tw.size[1][2]; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
if ( model ) { |
| 1315 |
#ifdef ALWAYS_BBOX_VS_BBOX |
| 1316 |
if ( model == BOX_MODEL_HANDLE || model == CAPSULE_MODEL_HANDLE) { |
| 1317 |
tw.sphere.use = qfalse; |
| 1318 |
CM_TraceThroughLeaf( &tw, &cmod->leaf ); |
| 1319 |
} |
| 1320 |
else |
| 1321 |
#elif defined(ALWAYS_CAPSULE_VS_CAPSULE) |
| 1322 |
if ( model == BOX_MODEL_HANDLE || model == CAPSULE_MODEL_HANDLE) { |
| 1323 |
CM_TraceCapsuleThroughCapsule( &tw, model ); |
| 1324 |
} |
| 1325 |
else |
| 1326 |
#endif |
| 1327 |
if ( model == CAPSULE_MODEL_HANDLE ) { |
| 1328 |
if ( tw.sphere.use ) { |
| 1329 |
CM_TraceCapsuleThroughCapsule( &tw, model ); |
| 1330 |
} |
| 1331 |
else { |
| 1332 |
CM_TraceBoundingBoxThroughCapsule( &tw, model ); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
else { |
| 1336 |
CM_TraceThroughLeaf( &tw, &cmod->leaf ); |
| 1337 |
} |
| 1338 |
} else { |
| 1339 |
CM_TraceThroughTree( &tw, 0, 0, 1, tw.start, tw.end ); |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
if ( tw.trace.fraction == 1 ) { |
| 1345 |
VectorCopy (end, tw.trace.endpos); |
| 1346 |
} else { |
| 1347 |
for ( i=0 ; i<3 ; i++ ) { |
| 1348 |
tw.trace.endpos[i] = start[i] + tw.trace.fraction * (end[i] - start[i]); |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
assert(tw.trace.allsolid || |
| 1356 |
tw.trace.fraction == 1.0 || |
| 1357 |
VectorLengthSquared(tw.trace.plane.normal) > 0.9999); |
| 1358 |
*results = tw.trace; |
| 1359 |
} |
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
void CM_BoxTrace( trace_t *results, const vec3_t start, const vec3_t end, |
| 1367 |
vec3_t mins, vec3_t maxs, |
| 1368 |
clipHandle_t model, int brushmask, int capsule ) { |
| 1369 |
CM_Trace( results, start, end, mins, maxs, model, vec3_origin, brushmask, capsule, NULL ); |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
void CM_TransformedBoxTrace( trace_t *results, const vec3_t start, const vec3_t end, |
| 1381 |
vec3_t mins, vec3_t maxs, |
| 1382 |
clipHandle_t model, int brushmask, |
| 1383 |
const vec3_t origin, const vec3_t angles, int capsule ) { |
| 1384 |
trace_t trace; |
| 1385 |
vec3_t start_l, end_l; |
| 1386 |
qboolean rotated; |
| 1387 |
vec3_t offset; |
| 1388 |
vec3_t symetricSize[2]; |
| 1389 |
vec3_t matrix[3], transpose[3]; |
| 1390 |
int i; |
| 1391 |
float halfwidth; |
| 1392 |
float halfheight; |
| 1393 |
float t; |
| 1394 |
sphere_t sphere; |
| 1395 |
|
| 1396 |
if ( !mins ) { |
| 1397 |
mins = vec3_origin; |
| 1398 |
} |
| 1399 |
if ( !maxs ) { |
| 1400 |
maxs = vec3_origin; |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 1407 |
offset[i] = ( mins[i] + maxs[i] ) * 0.5; |
| 1408 |
symetricSize[0][i] = mins[i] - offset[i]; |
| 1409 |
symetricSize[1][i] = maxs[i] - offset[i]; |
| 1410 |
start_l[i] = start[i] + offset[i]; |
| 1411 |
end_l[i] = end[i] + offset[i]; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
VectorSubtract( start_l, origin, start_l ); |
| 1416 |
VectorSubtract( end_l, origin, end_l ); |
| 1417 |
|
| 1418 |
|
| 1419 |
if ( model != BOX_MODEL_HANDLE && |
| 1420 |
(angles[0] || angles[1] || angles[2]) ) { |
| 1421 |
rotated = qtrue; |
| 1422 |
} else { |
| 1423 |
rotated = qfalse; |
| 1424 |
} |
| 1425 |
|
| 1426 |
halfwidth = symetricSize[ 1 ][ 0 ]; |
| 1427 |
halfheight = symetricSize[ 1 ][ 2 ]; |
| 1428 |
|
| 1429 |
sphere.use = capsule; |
| 1430 |
sphere.radius = ( halfwidth > halfheight ) ? halfheight : halfwidth; |
| 1431 |
sphere.halfheight = halfheight; |
| 1432 |
t = halfheight - sphere.radius; |
| 1433 |
|
| 1434 |
if (rotated) { |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
CreateRotationMatrix(angles, matrix); |
| 1442 |
RotatePoint(start_l, matrix); |
| 1443 |
RotatePoint(end_l, matrix); |
| 1444 |
|
| 1445 |
sphere.offset[0] = matrix[0][ 2 ] * t; |
| 1446 |
sphere.offset[1] = -matrix[1][ 2 ] * t; |
| 1447 |
sphere.offset[2] = matrix[2][ 2 ] * t; |
| 1448 |
} |
| 1449 |
else { |
| 1450 |
VectorSet( sphere.offset, 0, 0, t ); |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
CM_Trace( &trace, start_l, end_l, symetricSize[0], symetricSize[1], model, origin, brushmask, capsule, &sphere ); |
| 1455 |
|
| 1456 |
|
| 1457 |
if ( rotated && trace.fraction != 1.0 ) { |
| 1458 |
|
| 1459 |
TransposeMatrix(matrix, transpose); |
| 1460 |
RotatePoint(trace.plane.normal, transpose); |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
trace.endpos[0] = start[0] + trace.fraction * (end[0] - start[0]); |
| 1466 |
trace.endpos[1] = start[1] + trace.fraction * (end[1] - start[1]); |
| 1467 |
trace.endpos[2] = start[2] + trace.fraction * (end[2] - start[2]); |
| 1468 |
|
| 1469 |
*results = trace; |
| 1470 |
} |