Debian Codegraph @

code/qcommon/cm_trace.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 #include "cm_local.h"
23
24 // always use bbox vs. bbox collision and never capsule vs. bbox or vice versa
25 //#define ALWAYS_BBOX_VS_BBOX
26 // always use capsule vs. capsule collision and never capsule vs. bbox or vice versa
27 //#define ALWAYS_CAPSULE_VS_CAPSULE
28
29 //#define CAPSULE_DEBUG
30
31 /*
32 ===============================================================================
33
34 BASIC MATH
35
36 ===============================================================================
37 */
38
39 /*
40 ================
41 RotatePoint
42 ================
43 */
44 void RotatePoint(vec3_t point, /*const*/ vec3_t matrix[3]) { // FIXME
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 TransposeMatrix
56 ================
57 */
58 void TransposeMatrix(/*const*/ vec3_t matrix[3], vec3_t transpose[3]) { // FIXME
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 CreateRotationMatrix
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 CM_ProjectPointOntoVector
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 // project onto the directional vector for this segment
88 VectorMA( vStart, DotProduct( pVec, vDir ), vDir, vProj );
89 }
90
91 /*
92 ================
93 CM_DistanceFromLineSquared
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 CM_VectorDistanceSquared
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 SquareRootFloat
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 POSITION TESTING
152
153 ===============================================================================
154 */
155
156 /*
157 ================
158 CM_TestBoxInBrush
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 // special test for axial
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 // the first six planes are the axial planes, so we only
187 // need to test the remainder
188 for ( i = 6 ; i < brush->numsides ; i++ ) {
189 side = brush->sides + i;
190 plane = side->plane;
191
192 // adjust the plane distance appropriately for radius
193 dist = plane->dist + tw->sphere.radius;
194 // find the closest point on the capsule to the plane
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 // if completely in front of face, no intersection
206 if ( d1 > 0 ) {
207 return;
208 }
209 }
210 } else {
211 // the first six planes are the axial planes, so we only
212 // need to test the remainder
213 for ( i = 6 ; i < brush->numsides ; i++ ) {
214 side = brush->sides + i;
215 plane = side->plane;
216
217 // adjust the plane distance appropriately for mins/maxs
218 dist = plane->dist - DotProduct( tw->offsets[ plane->signbits ], plane->normal );
219
220 d1 = DotProduct( tw->start, plane->normal ) - dist;
221
222 // if completely in front of face, no intersection
223 if ( d1 > 0 ) {
224 return;
225 }
226 }
227 }
228
229 // inside this brush
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 CM_TestInLeaf
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 // test box position against all brushes in the leaf
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; // already checked this brush in another leaf
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 // test against all patches
268 #ifdef BSPC
269 if (1) {
270 #else
271 if ( !cm_noCurves->integer ) {
272 #endif //BSPC
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; // already checked this brush in another leaf
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 CM_TestCapsuleInCapsule
300
301 capsule inside capsule check
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 // check if any of the spheres overlap
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 // if between cylinder up and lower bounds
353 if ( (top[2] >= p1[2] && top[2] <= p2[2]) ||
354 (bottom[2] >= p1[2] && bottom[2] <= p2[2]) ) {
355 // 2d coordinates
356 top[2] = p1[2] = 0;
357 // if the cylinders overlap
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 CM_TestBoundingBoxInCapsule
369
370 bounding box inside capsule check
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 // mins maxs of the capsule
380 CM_ModelBounds(model, mins, maxs);
381
382 // offset for capsule center
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 // replace the bounding box with the capsule
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 // replace the capsule with the bounding box
398 h = CM_TempBoxModel(tw->size[0], tw->size[1], qfalse);
399 // calculate collision
400 cmod = CM_ClipHandleToModel( h );
401 CM_TestInLeaf( tw, &cmod->leaf );
402 }
403
404 /*
405 ==================
406 CM_PositionTest
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 // identify the leafs we are touching
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 // test the contents of the leafs
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 TRACING
451
452 ===============================================================================
453 */
454
455
456 /*
457 ================
458 CM_TraceThroughPatch
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 CM_TraceThroughBrush
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 // compare the trace against all planes of the brush
513 // find the latest time the trace crosses a plane towards the interior
514 // and the earliest time the trace crosses a plane towards the exterior
515 //
516 for (i = 0; i < brush->numsides; i++) {
517 side = brush->sides + i;
518 plane = side->plane;
519
520 // adjust the plane distance appropriately for radius
521 dist = plane->dist + tw->sphere.radius;
522
523 // find the closest point on the capsule to the plane
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; // endpoint is not in solid
541 }
542 if (d1 > 0) {
543 startout = qtrue;
544 }
545
546 // if completely in front of face, no intersection with the entire brush
547 if (d1 > 0 && ( d2 >= SURFACE_CLIP_EPSILON || d2 >= d1 ) ) {
548 return;
549 }
550
551 // if it doesn't cross the plane, the plane isn't relevant
552 if (d1 <= 0 && d2 <= 0 ) {
553 continue;
554 }
555
556 // crosses face
557 if (d1 > d2) { // enter
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 { // leave
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 // compare the trace against all planes of the brush
580 // find the latest time the trace crosses a plane towards the interior
581 // and the earliest time the trace crosses a plane towards the exterior
582 //
583 for (i = 0; i < brush->numsides; i++) {
584 side = brush->sides + i;
585 plane = side->plane;
586
587 // adjust the plane distance appropriately for mins/maxs
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; // endpoint is not in solid
595 }
596 if (d1 > 0) {
597 startout = qtrue;
598 }
599
600 // if completely in front of face, no intersection with the entire brush
601 if (d1 > 0 && ( d2 >= SURFACE_CLIP_EPSILON || d2 >= d1 ) ) {
602 return;
603 }
604
605 // if it doesn't cross the plane, the plane isn't relevant
606 if (d1 <= 0 && d2 <= 0 ) {
607 continue;
608 }
609
610 // crosses face
611 if (d1 > d2) { // enter
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 { // leave
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 // all planes have been checked, and the trace was not
635 // completely outside the brush
636 //
637 if (!startout) { // original point was inside brush
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 CM_TraceThroughLeaf
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 // trace line against all brushes in the leaf
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; // already checked this brush in another leaf
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 // trace line against all patches in the leaf
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; // already checked this patch in another leaf
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 CM_TraceThroughSphere
733
734 get the first intersection of the ray with the sphere
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 //float a;
740 float b, c, d, sqrtd;
741 vec3_t v1, dir, intersection;
742
743 // if inside the sphere
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 // test for allsolid
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 // if no intersection with the sphere and the end point is at least an epsilon away
765 if (l1 >= Square(radius) && l2 > Square(radius+SURFACE_CLIP_EPSILON)) {
766 return;
767 }
768 //
769 // | origin - (start + t * dir) | = radius
770 // a = dir[0]^2 + dir[1]^2 + dir[2]^2;
771 // b = 2 * (dir[0] * (start[0] - origin[0]) + dir[1] * (start[1] - origin[1]) + dir[2] * (start[2] - origin[2]));
772 // c = (start[0] - origin[0])^2 + (start[1] - origin[1])^2 + (start[2] - origin[2])^2 - radius^2;
773 //
774 VectorSubtract(start, origin, v1);
775 // dir is normalized so a = 1
776 //a = 1.0f;//dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2];
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;// * a;
781 if (d > 0) {
782 sqrtd = SquareRootFloat(d);
783 // = (- b + sqrtd) * 0.5f; // / (2.0f * a);
784 fraction = (- b - sqrtd) * 0.5f; // / (2.0f * a);
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 //t1 = (- b ) / 2;
813 // slide along the sphere
814 }
815 // no intersection at all
816 }
817
818 /*
819 ================
820 CM_TraceThroughVerticalCylinder
821
822 get the first intersection of the ray with the cylinder
823 the cylinder extends halfheight above and below the origin
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 //float a;
829 float b, c, d, sqrtd;
830 vec3_t v1, dir, start2d, end2d, org2d, intersection;
831
832 // 2d coordinates
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 // if between lower and upper cylinder bounds
837 if (start[2] <= origin[2] + halfheight &&
838 start[2] >= origin[2] - halfheight) {
839 // if inside the cylinder
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 // if no intersection with the cylinder and the end point is at least an epsilon away
861 if (l1 >= Square(radius) && l2 > Square(radius+SURFACE_CLIP_EPSILON)) {
862 return;
863 }
864 //
865 //
866 // (start[0] - origin[0] - t * dir[0]) ^ 2 + (start[1] - origin[1] - t * dir[1]) ^ 2 = radius ^ 2
867 // (v1[0] + t * dir[0]) ^ 2 + (v1[1] + t * dir[1]) ^ 2 = radius ^ 2;
868 // v1[0] ^ 2 + 2 * v1[0] * t * dir[0] + (t * dir[0]) ^ 2 +
869 // v1[1] ^ 2 + 2 * v1[1] * t * dir[1] + (t * dir[1]) ^ 2 = radius ^ 2
870 // t ^ 2 * (dir[0] ^ 2 + dir[1] ^ 2) + t * (2 * v1[0] * dir[0] + 2 * v1[1] * dir[1]) +
871 // v1[0] ^ 2 + v1[1] ^ 2 - radius ^ 2 = 0
872 //
873 VectorSubtract(start, origin, v1);
874 // dir is normalized so we can use a = 1
875 //a = 1.0f;// * (dir[0] * dir[0] + dir[1] * dir[1]);
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;// * a;
880 if (d > 0) {
881 sqrtd = SquareRootFloat(d);
882 // = (- b + sqrtd) * 0.5f;// / (2.0f * a);
883 fraction = (- b - sqrtd) * 0.5f;// / (2.0f * a);
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 // if the intersection is between the cylinder lower and upper bound
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 //t[0] = (- b ) / 2 * a;
918 // slide along the cylinder
919 }
920 // no intersection at all
921 }
922
923 /*
924 ================
925 CM_TraceCapsuleThroughCapsule
926
927 capsule vs. capsule collision (not rotated)
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 // test trace bounds vs. capsule bounds
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 // top origin and bottom origin of each sphere at start and end of trace
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 // calculate top and bottom of the capsule spheres to collide with
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 // expand radius of spheres
969 radius += tw->sphere.radius;
970 // if there is horizontal movement
971 if ( tw->start[0] != tw->end[0] || tw->start[1] != tw->end[1] ) {
972 // height of the expanded cylinder is the height of both cylinders minus the radius of both spheres
973 h = halfheight + tw->sphere.halfheight - radius;
974 // if the cylinder has a height
975 if ( h > 0 ) {
976 // test for collisions between the cylinders
977 CM_TraceThroughVerticalCylinder(tw, offset, radius, h, tw->start, tw->end);
978 }
979 }
980 // test for collision between the spheres
981 CM_TraceThroughSphere(tw, top, radius, startbottom, endbottom);
982 CM_TraceThroughSphere(tw, bottom, radius, starttop, endtop);
983 }
984
985 /*
986 ================
987 CM_TraceBoundingBoxThroughCapsule
988
989 bounding box vs. capsule collision
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 // mins maxs of the capsule
999 CM_ModelBounds(model, mins, maxs);
1000
1001 // offset for capsule center
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 // replace the bounding box with the capsule
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 // replace the capsule with the bounding box
1017 h = CM_TempBoxModel(tw->size[0], tw->size[1], qfalse);
1018 // calculate collision
1019 cmod = CM_ClipHandleToModel( h );
1020 CM_TraceThroughLeaf( tw, &cmod->leaf );
1021 }
1022
1023 //=========================================================================================
1024
1025 /*
1026 ==================
1027 CM_TraceThroughTree
1028
1029 Traverse all the contacted leafs from the start to the end position.
1030 If the trace is a point, they will be exactly in order, but for larger
1031 trace volumes it is possible to hit something in a later leaf with
1032 a smaller intercept fraction.
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; // already hit something nearer
1047 }
1048
1049 // if < 0, we are in a leaf node
1050 if (num < 0) {
1051 CM_TraceThroughLeaf( tw, &cm.leafs[-1-num] );
1052 return;
1053 }
1054
1055 //
1056 // find the point distances to the separating plane
1057 // and the offset for the size of the box
1058 //
1059 node = cm.nodes + num;
1060 plane = node->plane;
1061
1062 // adjust the plane distance appropriately for mins/maxs
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 // this is silly
1074 offset = 2048;
1075 }
1076 }
1077
1078 // see which sides we need to consider
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 // put the crosspoint SURFACE_CLIP_EPSILON pixels on the near side
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 // move up to the node
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 // go past the node
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 CM_Trace
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++; // for multi-check avoidance
1158
1159 c_traces++; // for statistics, may be zeroed
1160
1161 // fill in a default trace
1162 Com_Memset( &tw, 0, sizeof(tw) );
1163 tw.trace.fraction = 1; // assume it goes the entire distance until shown otherwise
1164 VectorCopy(origin, tw.modelOrigin);
1165
1166 if (!cm.numNodes) {
1167 *results = tw.trace;
1168
1169 return; // map not loaded, shouldn't happen
1170 }
1171
1172 // allow NULL to be passed in for 0,0,0
1173 if ( !mins ) {
1174 mins = vec3_origin;
1175 }
1176 if ( !maxs ) {
1177 maxs = vec3_origin;
1178 }
1179
1180 // set basic parms
1181 tw.contents = brushmask;
1182
1183 // adjust so that mins and maxs are always symetric, which
1184 // avoids some complications with plane expanding of rotated
1185 // bmodels
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 // if a sphere is already specified
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 // tw.offsets[signbits] = vector to appropriate corner from origin
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 // calculate bounds
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 // check for position test special case
1268 //
1269 if (start[0] == end[0] && start[1] == end[1] && start[2] == end[2]) {
1270 if ( model ) {
1271 #ifdef ALWAYS_BBOX_VS_BBOX // FIXME - compile time flag?
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 // check for point special case
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 // general sweeping through world
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 // generate endpos from the original, unmodified start/end
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 // If allsolid is set (was entirely inside something solid), the plane is not valid.
1353 // If fraction == 1.0, we never hit anything, and thus the plane is not valid.
1354 // Otherwise, the normal on the plane should have unit length
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 CM_BoxTrace
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 CM_TransformedBoxTrace
1375
1376 Handles offseting and rotation of the end points for moving and
1377 rotating entities
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 // adjust so that mins and maxs are always symetric, which
1404 // avoids some complications with plane expanding of rotated
1405 // bmodels
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 // subtract origin offset
1415 VectorSubtract( start_l, origin, start_l );
1416 VectorSubtract( end_l, origin, end_l );
1417
1418 // rotate start and end into the models frame of reference
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 // rotation on trace line (start-end) instead of rotating the bmodel
1436 // NOTE: This is still incorrect for bounding boxes because the actual bounding
1437 // box that is swept through the model is not rotated. We cannot rotate
1438 // the bounding box or the bmodel because that would make all the brush
1439 // bevels invalid.
1440 // However this is correct for capsules since a capsule itself is rotated too.
1441 CreateRotationMatrix(angles, matrix);
1442 RotatePoint(start_l, matrix);
1443 RotatePoint(end_l, matrix);
1444 // rotated sphere offset for capsule
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 // sweep the box through the model
1454 CM_Trace( &trace, start_l, end_l, symetricSize[0], symetricSize[1], model, origin, brushmask, capsule, &sphere );
1455
1456 // if the bmodel was rotated and there was a collision
1457 if ( rotated && trace.fraction != 1.0 ) {
1458 // rotation of bmodel collision plane
1459 TransposeMatrix(matrix, transpose);
1460 RotatePoint(trace.plane.normal, transpose);
1461 }
1462
1463 // re-calculate the end position of the trace because the trace.endpos
1464 // calculated by CM_Trace could be rotated and have an offset
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 }