forked from id-Software/Enemy-Territory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm_trace.c
1675 lines (1448 loc) · 43.4 KB
/
cm_trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===========================================================================
Wolfenstein: Enemy Territory GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Wolfenstein: Enemy Territory GPL Source Code (Wolf ET Source Code).
Wolf ET Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wolf ET Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wolf ET Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Wolf: ET Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Wolf ET Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "cm_local.h"
#include "cm_patch.h"
// always use bbox vs. bbox collision and never capsule vs. bbox or vice versa
#define ALWAYS_BBOX_VS_BBOX
// always use capsule vs. capsule collision and never capsule vs. bbox or vice versa
//#define ALWAYS_CAPSULE_VS_CAPSULE
//#define CAPSULE_DEBUG
/*
===============================================================================
BASIC MATH
===============================================================================
*/
/*
================
RotatePoint
================
*/
// TTimo: const vec_t ** would require explicit casts for ANSI C conformance
// see unix/const-arg.c in Wolf MP source
void RotatePoint( vec3_t point, /*const*/ vec3_t matrix[3] ) {
vec3_t tvec;
VectorCopy( point, tvec );
point[0] = DotProduct( matrix[0], tvec );
point[1] = DotProduct( matrix[1], tvec );
point[2] = DotProduct( matrix[2], tvec );
}
/*
================
TransposeMatrix
================
*/
// TTimo: const vec_t ** would require explicit casts for ANSI C conformance
// see unix/const-arg.c in Wolf MP source
void TransposeMatrix( /*const*/ vec3_t matrix[3], vec3_t transpose[3] ) {
int i, j;
for ( i = 0; i < 3; i++ ) {
for ( j = 0; j < 3; j++ ) {
transpose[i][j] = matrix[j][i];
}
}
}
/*
================
CreateRotationMatrix
================
*/
void CreateRotationMatrix( const vec3_t angles, vec3_t matrix[3] ) {
float angle;
static float sr, sp, sy, cr, cp, cy;
// static to help MS compiler fp bugs
angle = angles[YAW] * ( M_PI * 2 / 360 );
sy = sin( angle );
cy = cos( angle );
angle = angles[PITCH] * ( M_PI * 2 / 360 );
sp = sin( angle );
cp = cos( angle );
angle = angles[ROLL] * ( M_PI * 2 / 360 );
sr = sin( angle );
cr = cos( angle );
matrix[0][0] = cp * cy;
matrix[0][1] = cp * sy;
matrix[0][2] = -sp;
matrix[1][0] = ( sr * sp * cy + cr * -sy );
matrix[1][1] = ( sr * sp * sy + cr * cy );
matrix[1][2] = sr * cp;
matrix[2][0] = ( cr * sp * cy + - sr * -sy );
matrix[2][1] = ( cr * sp * sy + - sr * cy );
matrix[2][2] = cr * cp;
}
/*
================
CM_ProjectPointOntoVector
================
*/
void CM_ProjectPointOntoVector( vec3_t point, vec3_t vStart, vec3_t vDir, vec3_t vProj ) {
vec3_t pVec;
VectorSubtract( point, vStart, pVec );
// project onto the directional vector for this segment
VectorMA( vStart, DotProduct( pVec, vDir ), vDir, vProj );
}
/*
================
CM_DistanceFromLineSquared
================
*/
float CM_DistanceFromLineSquared( vec3_t p, vec3_t lp1, vec3_t lp2, vec3_t dir ) {
vec3_t proj, t;
int j;
CM_ProjectPointOntoVector( p, lp1, dir, proj );
for ( j = 0; j < 3; j++ )
if ( ( proj[j] > lp1[j] && proj[j] > lp2[j] ) ||
( proj[j] < lp1[j] && proj[j] < lp2[j] ) ) {
break;
}
if ( j < 3 ) {
if ( Q_fabs( proj[j] - lp1[j] ) < Q_fabs( proj[j] - lp2[j] ) ) {
VectorSubtract( p, lp1, t );
} else {
VectorSubtract( p, lp2, t );
}
return VectorLengthSquared( t );
}
VectorSubtract( p, proj, t );
return VectorLengthSquared( t );
}
/*
================
CM_VectorDistanceSquared
================
*/
float CM_VectorDistanceSquared( vec3_t p1, vec3_t p2 ) {
vec3_t dir;
VectorSubtract( p2, p1, dir );
return VectorLengthSquared( dir );
}
/*
================
SquareRootFloat
================
*/
float SquareRootFloat( float number ) {
long i;
float x, y;
const float f = 1.5F;
x = number * 0.5F;
y = number;
i = *( long * ) &y;
i = 0x5f3759df - ( i >> 1 );
y = *( float * ) &i;
y = y * ( f - ( x * y * y ) );
y = y * ( f - ( x * y * y ) );
return number * y;
}
/*
===============================================================================
POSITION TESTING
===============================================================================
*/
/*
================
CM_TestBoxInBrush
================
*/
void CM_TestBoxInBrush( traceWork_t *tw, cbrush_t *brush ) {
int i;
cplane_t *plane;
float dist;
float d1;
cbrushside_t *side;
float t;
vec3_t startp;
if ( !brush->numsides ) {
return;
}
// special test for axial
// the first 6 brush planes are always axial
if ( tw->bounds[0][0] > brush->bounds[1][0]
|| tw->bounds[0][1] > brush->bounds[1][1]
|| tw->bounds[0][2] > brush->bounds[1][2]
|| tw->bounds[1][0] < brush->bounds[0][0]
|| tw->bounds[1][1] < brush->bounds[0][1]
|| tw->bounds[1][2] < brush->bounds[0][2]
) {
return;
}
if ( tw->sphere.use ) {
// the first six planes are the axial planes, so we only
// need to test the remainder
for ( i = 6 ; i < brush->numsides ; i++ ) {
side = brush->sides + i;
plane = side->plane;
// adjust the plane distance apropriately for radius
dist = plane->dist + tw->sphere.radius;
// find the closest point on the capsule to the plane
t = DotProduct( plane->normal, tw->sphere.offset );
if ( t > 0 ) {
VectorSubtract( tw->start, tw->sphere.offset, startp );
} else
{
VectorAdd( tw->start, tw->sphere.offset, startp );
}
d1 = DotProduct( startp, plane->normal ) - dist;
// if completely in front of face, no intersection
if ( d1 > 0 ) {
return;
}
}
} else {
// the first six planes are the axial planes, so we only
// need to test the remainder
for ( i = 6 ; i < brush->numsides ; i++ ) {
side = brush->sides + i;
plane = side->plane;
// adjust the plane distance apropriately for mins/maxs
dist = plane->dist - DotProduct( tw->offsets[ plane->signbits ], plane->normal );
d1 = DotProduct( tw->start, plane->normal ) - dist;
// if completely in front of face, no intersection
if ( d1 > 0 ) {
return;
}
}
}
// inside this brush
tw->trace.startsolid = tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
tw->trace.contents = brush->contents;
}
/*
================
CM_TestInLeaf
================
*/
void CM_TestInLeaf( traceWork_t *tw, cLeaf_t *leaf ) {
int k;
int brushnum;
cbrush_t *b;
cPatch_t *patch;
// test box position against all brushes in the leaf
for ( k = 0 ; k < leaf->numLeafBrushes ; k++ ) {
brushnum = cm.leafbrushes[leaf->firstLeafBrush + k];
b = &cm.brushes[brushnum];
if ( b->checkcount == cm.checkcount ) {
continue; // already checked this brush in another leaf
}
b->checkcount = cm.checkcount;
if ( !( b->contents & tw->contents ) ) {
continue;
}
CM_TestBoxInBrush( tw, b );
if ( tw->trace.allsolid ) {
return;
}
}
// test against all patches
#ifdef BSPC
if ( 1 ) {
#else
if ( !cm_noCurves->integer ) {
#endif //BSPC
for ( k = 0 ; k < leaf->numLeafSurfaces ; k++ ) {
patch = cm.surfaces[ cm.leafsurfaces[ leaf->firstLeafSurface + k ] ];
if ( !patch ) {
continue;
}
if ( patch->checkcount == cm.checkcount ) {
continue; // already checked this brush in another leaf
}
patch->checkcount = cm.checkcount;
if ( !( patch->contents & tw->contents ) ) {
continue;
}
if ( CM_PositionTestInPatchCollide( tw, patch->pc ) ) {
tw->trace.startsolid = tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
return;
}
}
}
}
/*
==================
CM_TestCapsuleInCapsule
capsule inside capsule check
==================
*/
void CM_TestCapsuleInCapsule( traceWork_t *tw, clipHandle_t model ) {
int i;
vec3_t mins, maxs;
vec3_t top, bottom;
vec3_t p1, p2, tmp;
vec3_t offset, symetricSize[2];
float radius, halfwidth, halfheight, offs, r;
CM_ModelBounds( model, mins, maxs );
VectorAdd( tw->start, tw->sphere.offset, top );
VectorSubtract( tw->start, tw->sphere.offset, bottom );
for ( i = 0 ; i < 3 ; i++ ) {
offset[i] = ( mins[i] + maxs[i] ) * 0.5;
symetricSize[0][i] = mins[i] - offset[i];
symetricSize[1][i] = maxs[i] - offset[i];
}
halfwidth = symetricSize[ 1 ][ 0 ];
halfheight = symetricSize[ 1 ][ 2 ];
radius = ( halfwidth > halfheight ) ? halfheight : halfwidth;
offs = halfheight - radius;
r = Square( tw->sphere.radius + radius );
// check if any of the spheres overlap
VectorCopy( offset, p1 );
p1[2] += offs;
VectorSubtract( p1, top, tmp );
if ( VectorLengthSquared( tmp ) < r ) {
tw->trace.startsolid = tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
}
VectorSubtract( p1, bottom, tmp );
if ( VectorLengthSquared( tmp ) < r ) {
tw->trace.startsolid = tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
}
VectorCopy( offset, p2 );
p2[2] -= offs;
VectorSubtract( p2, top, tmp );
if ( VectorLengthSquared( tmp ) < r ) {
tw->trace.startsolid = tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
}
VectorSubtract( p2, bottom, tmp );
if ( VectorLengthSquared( tmp ) < r ) {
tw->trace.startsolid = tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
}
// if between cylinder up and lower bounds
if ( ( top[2] >= p1[2] && top[2] <= p2[2] ) ||
( bottom[2] >= p1[2] && bottom[2] <= p2[2] ) ) {
// 2d coordinates
top[2] = p1[2] = 0;
// if the cylinders overlap
VectorSubtract( top, p1, tmp );
if ( VectorLengthSquared( tmp ) < r ) {
tw->trace.startsolid = tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
}
}
}
/*
==================
CM_TestBoundingBoxInCapsule
bounding box inside capsule check
==================
*/
void CM_TestBoundingBoxInCapsule( traceWork_t *tw, clipHandle_t model ) {
vec3_t mins, maxs, offset, size[2];
clipHandle_t h;
cmodel_t *cmod;
int i;
// mins maxs of the capsule
CM_ModelBounds( model, mins, maxs );
// offset for capsule center
for ( i = 0 ; i < 3 ; i++ ) {
offset[i] = ( mins[i] + maxs[i] ) * 0.5;
size[0][i] = mins[i] - offset[i];
size[1][i] = maxs[i] - offset[i];
tw->start[i] -= offset[i];
tw->end[i] -= offset[i];
}
// replace the bounding box with the capsule
tw->sphere.use = qtrue;
tw->sphere.radius = ( size[1][0] > size[1][2] ) ? size[1][2] : size[1][0];
tw->sphere.halfheight = size[1][2];
VectorSet( tw->sphere.offset, 0, 0, size[1][2] - tw->sphere.radius );
// replace the capsule with the bounding box
h = CM_TempBoxModel( tw->size[0], tw->size[1], qfalse );
// calculate collision
cmod = CM_ClipHandleToModel( h );
CM_TestInLeaf( tw, &cmod->leaf );
}
/*
==================
CM_PositionTest
==================
*/
#define MAX_POSITION_LEAFS 1024
void CM_PositionTest( traceWork_t *tw ) {
int leafs[MAX_POSITION_LEAFS];
int i;
leafList_t ll;
// identify the leafs we are touching
VectorAdd( tw->start, tw->size[0], ll.bounds[0] );
VectorAdd( tw->start, tw->size[1], ll.bounds[1] );
for ( i = 0 ; i < 3 ; i++ ) {
ll.bounds[0][i] -= 1;
ll.bounds[1][i] += 1;
}
ll.count = 0;
ll.maxcount = MAX_POSITION_LEAFS;
ll.list = leafs;
ll.storeLeafs = CM_StoreLeafs;
ll.lastLeaf = 0;
ll.overflowed = qfalse;
cm.checkcount++;
CM_BoxLeafnums_r( &ll, 0 );
cm.checkcount++;
// test the contents of the leafs
for ( i = 0 ; i < ll.count ; i++ ) {
CM_TestInLeaf( tw, &cm.leafs[leafs[i]] );
if ( tw->trace.allsolid ) {
break;
}
}
}
/*
===============================================================================
TRACING
===============================================================================
*/
/*
================
CM_TraceThroughPatch
================
*/
static void CM_TraceThroughPatch( traceWork_t *tw, cPatch_t *patch ) {
float oldFrac;
c_patch_traces++;
oldFrac = tw->trace.fraction;
CM_TraceThroughPatchCollide( tw, patch->pc );
if ( tw->trace.fraction < oldFrac ) {
tw->trace.surfaceFlags = patch->surfaceFlags;
tw->trace.contents = patch->contents;
}
}
#ifdef MRE_OPTIMIZE
/*
================
CM_CalcTraceBounds
================
*/
static void CM_CalcTraceBounds( traceWork_t *tw, qboolean expand ) {
int i;
if ( tw->sphere.use ) {
for ( i = 0; i < 3; i++ ) {
if ( tw->start[i] < tw->end[i] ) {
tw->bounds[0][i] = tw->start[i] - Q_fabs( tw->sphere.offset[i] ) - tw->sphere.radius;
tw->bounds[1][i] = tw->start[i] + tw->trace.fraction * tw->dir[i] + Q_fabs( tw->sphere.offset[i] ) + tw->sphere.radius;
} else {
tw->bounds[0][i] = tw->start[i] + tw->trace.fraction * tw->dir[i] - Q_fabs( tw->sphere.offset[i] ) - tw->sphere.radius;
tw->bounds[1][i] = tw->start[i] + Q_fabs( tw->sphere.offset[i] ) + tw->sphere.radius;
}
}
} else {
for ( i = 0; i < 3; i++ ) {
if ( tw->start[i] < tw->end[i] ) {
tw->bounds[0][i] = tw->start[i] + tw->size[0][i];
tw->bounds[1][i] = tw->start[i] + tw->trace.fraction * tw->dir[i] + tw->size[1][i];
} else {
tw->bounds[0][i] = tw->start[i] + tw->trace.fraction * tw->dir[i] + tw->size[0][i];
tw->bounds[1][i] = tw->start[i] + tw->size[1][i];
}
}
}
if ( expand ) {
// expand for epsilon
for ( i = 0; i < 3; i++ ) {
tw->bounds[0][i] -= 1.0f;
tw->bounds[1][i] += 1.0f;
}
}
}
/*
================
CM_BoxDistanceFromPlane
================
*/
static float CM_BoxDistanceFromPlane( vec3_t center, vec3_t extents, cplane_t *plane ) {
float d1, d2;
d1 = DotProduct( center, plane->normal ) - plane->dist;
d2 = Q_fabs( extents[0] * plane->normal[0] ) +
Q_fabs( extents[1] * plane->normal[1] ) +
Q_fabs( extents[2] * plane->normal[2] );
if ( d1 - d2 > 0.0f ) {
return d1 - d2;
}
if ( d1 + d2 < 0.0f ) {
return d1 + d2;
}
return 0.0f;
}
/*
================
CM_BoxDistanceFromPlane
================
*/
static int CM_TraceThroughBounds( traceWork_t *tw, vec3_t mins, vec3_t maxs ) {
int i;
vec3_t center, extents;
for ( i = 0; i < 3; i++ ) {
if ( mins[i] > tw->bounds[1][i] ) {
return qfalse;
}
if ( maxs[i] < tw->bounds[0][i] ) {
return qfalse;
}
}
VectorAdd( mins, maxs, center );
VectorScale( center, 0.5f, center );
VectorSubtract( maxs, center, extents );
if ( Q_fabs( CM_BoxDistanceFromPlane( center, extents, &tw->tracePlane1 ) ) > tw->traceDist1 ) {
return qfalse;
}
if ( Q_fabs( CM_BoxDistanceFromPlane( center, extents, &tw->tracePlane2 ) ) > tw->traceDist2 ) {
return qfalse;
}
// trace might go through the bounds
return qtrue;
}
#endif
/*
================
CM_TraceThroughBrush
================
*/
static void CM_TraceThroughBrush( traceWork_t *tw, cbrush_t *brush ) {
int i;
cplane_t *plane, *clipplane;
float dist;
float enterFrac, leaveFrac;
float d1, d2;
qboolean getout, startout;
float f;
cbrushside_t *side, *leadside;
float t;
vec3_t startp;
vec3_t endp;
enterFrac = -1.0;
leaveFrac = 1.0;
clipplane = NULL;
if ( !brush->numsides ) {
return;
}
c_brush_traces++;
getout = qfalse;
startout = qfalse;
leadside = NULL;
if ( tw->sphere.use ) {
//
// compare the trace against all planes of the brush
// find the latest time the trace crosses a plane towards the interior
// and the earliest time the trace crosses a plane towards the exterior
//
for ( i = 0; i < brush->numsides; i++ ) {
side = brush->sides + i;
plane = side->plane;
// adjust the plane distance apropriately for radius
dist = plane->dist + tw->sphere.radius;
// find the closest point on the capsule to the plane
t = DotProduct( plane->normal, tw->sphere.offset );
if ( t > 0 ) {
VectorSubtract( tw->start, tw->sphere.offset, startp );
VectorSubtract( tw->end, tw->sphere.offset, endp );
} else
{
VectorAdd( tw->start, tw->sphere.offset, startp );
VectorAdd( tw->end, tw->sphere.offset, endp );
}
d1 = DotProduct( startp, plane->normal ) - dist;
d2 = DotProduct( endp, plane->normal ) - dist;
if ( d2 > 0 ) {
getout = qtrue; // endpoint is not in solid
}
if ( d1 > 0 ) {
startout = qtrue;
}
// if completely in front of face, no intersection with the entire brush
if ( d1 > 0 && ( d2 >= SURFACE_CLIP_EPSILON || d2 >= d1 ) ) {
return;
}
// if it doesn't cross the plane, the plane isn't relevent
if ( d1 <= 0 && d2 <= 0 ) {
continue;
}
// crosses face
if ( d1 > d2 ) { // enter
f = ( d1 - SURFACE_CLIP_EPSILON ) / ( d1 - d2 );
if ( f < 0 ) {
f = 0;
}
if ( f > enterFrac ) {
enterFrac = f;
clipplane = plane;
leadside = side;
}
} else { // leave
f = ( d1 + SURFACE_CLIP_EPSILON ) / ( d1 - d2 );
if ( f > 1 ) {
f = 1;
}
if ( f < leaveFrac ) {
leaveFrac = f;
}
}
}
} else {
//
// compare the trace against all planes of the brush
// find the latest time the trace crosses a plane towards the interior
// and the earliest time the trace crosses a plane towards the exterior
//
for ( i = 0; i < brush->numsides; i++ ) {
side = brush->sides + i;
plane = side->plane;
// adjust the plane distance apropriately for mins/maxs
dist = plane->dist - DotProduct( tw->offsets[ plane->signbits ], plane->normal );
d1 = DotProduct( tw->start, plane->normal ) - dist;
d2 = DotProduct( tw->end, plane->normal ) - dist;
if ( d2 > 0 ) {
getout = qtrue; // endpoint is not in solid
}
if ( d1 > 0 ) {
startout = qtrue;
}
// if completely in front of face, no intersection with the entire brush
if ( d1 > 0 && ( d2 >= SURFACE_CLIP_EPSILON || d2 >= d1 ) ) {
return;
}
// if it doesn't cross the plane, the plane isn't relevent
if ( d1 <= 0 && d2 <= 0 ) {
continue;
}
// crosses face
if ( d1 > d2 ) { // enter
f = ( d1 - SURFACE_CLIP_EPSILON ) / ( d1 - d2 );
if ( f < 0 ) {
f = 0;
}
if ( f > enterFrac ) {
enterFrac = f;
clipplane = plane;
leadside = side;
}
} else { // leave
f = ( d1 + SURFACE_CLIP_EPSILON ) / ( d1 - d2 );
if ( f > 1 ) {
f = 1;
}
if ( f < leaveFrac ) {
leaveFrac = f;
}
}
}
}
//
// all planes have been checked, and the trace was not
// completely outside the brush
//
if ( !startout ) { // original point was inside brush
tw->trace.startsolid = qtrue;
if ( !getout ) {
tw->trace.allsolid = qtrue;
tw->trace.fraction = 0;
}
return;
}
if ( enterFrac < leaveFrac ) {
if ( enterFrac > -1 && enterFrac < tw->trace.fraction ) {
if ( enterFrac < 0 ) {
enterFrac = 0;
}
tw->trace.fraction = enterFrac;
tw->trace.plane = *clipplane;
tw->trace.surfaceFlags = leadside->surfaceFlags;
tw->trace.contents = brush->contents;
}
}
}
/*
================
CM_TraceThroughLeaf
================
*/
static void CM_TraceThroughLeaf( traceWork_t *tw, cLeaf_t *leaf ) {
int k;
int brushnum;
cbrush_t *brush;
cPatch_t *patch;
#ifdef MRE_OPTIMIZE
float fraction;
#endif
// trace line against all brushes in the leaf
for ( k = 0 ; k < leaf->numLeafBrushes ; k++ ) {
brushnum = cm.leafbrushes[leaf->firstLeafBrush + k];
brush = &cm.brushes[brushnum];
if ( brush->checkcount == cm.checkcount ) {
continue; // already checked this brush in another leaf
}
brush->checkcount = cm.checkcount;
if ( !( brush->contents & tw->contents ) ) {
continue;
}
#ifdef MRE_OPTIMIZE
#ifndef BSPC
if ( cm_optimize->integer )
#endif
{
if ( !CM_TraceThroughBounds( tw, brush->bounds[0], brush->bounds[1] ) ) {
continue;
}
}
fraction = tw->trace.fraction;
#endif
CM_TraceThroughBrush( tw, brush );
if ( !tw->trace.fraction ) {
return;
}
#ifdef MRE_OPTIMIZE
if ( tw->trace.fraction < fraction ) {
CM_CalcTraceBounds( tw, qtrue );
}
#endif
}
// trace line against all patches in the leaf
#ifdef BSPC
if ( 1 ) {
#else
if ( !cm_noCurves->integer ) {
#endif
for ( k = 0 ; k < leaf->numLeafSurfaces ; k++ ) {
patch = cm.surfaces[ cm.leafsurfaces[ leaf->firstLeafSurface + k ] ];
if ( !patch ) {
continue;
}
if ( patch->checkcount == cm.checkcount ) {
continue; // already checked this patch in another leaf
}
patch->checkcount = cm.checkcount;
if ( !( patch->contents & tw->contents ) ) {
continue;
}
#ifdef MRE_OPTIMIZE
#ifndef BSPC
if ( cm_optimize->integer )
#endif
{
if ( !CM_TraceThroughBounds( tw, patch->pc->bounds[0], patch->pc->bounds[1] ) ) {
continue;
}
}
fraction = tw->trace.fraction;
#endif
CM_TraceThroughPatch( tw, patch );
if ( !tw->trace.fraction ) {
return;
}
#ifdef MRE_OPTIMIZE
if ( tw->trace.fraction < fraction ) {
CM_CalcTraceBounds( tw, qtrue );
}
#endif
}
}
}
#define RADIUS_EPSILON 1.0f
/*
================
CM_TraceThroughSphere
get the first intersection of the ray with the sphere
================
*/
static void CM_TraceThroughSphere( traceWork_t *tw, vec3_t origin, float radius, vec3_t start, vec3_t end ) {
float l1, l2, length, scale, fraction;
float a, b, c, d, sqrtd;
vec3_t v1, dir, intersection;
// if inside the sphere
VectorSubtract( start, origin, dir );
l1 = VectorLengthSquared( dir );
if ( l1 < Square( radius ) ) {
tw->trace.fraction = 0;
tw->trace.startsolid = qtrue;
// test for allsolid
VectorSubtract( end, origin, dir );
l1 = VectorLengthSquared( dir );
if ( l1 < Square( radius ) ) {
tw->trace.allsolid = qtrue;
}
return;
}
//
VectorSubtract( end, start, dir );
length = VectorNormalize( dir );
//
l1 = CM_DistanceFromLineSquared( origin, start, end, dir );
VectorSubtract( end, origin, v1 );
l2 = VectorLengthSquared( v1 );
// if no intersection with the sphere and the end point is at least an epsilon away
if ( l1 >= Square( radius ) && l2 > Square( radius + SURFACE_CLIP_EPSILON ) ) {
return;
}
//
// | origin - (start + t * dir) | = radius
// a = dir[0]^2 + dir[1]^2 + dir[2]^2;
// b = 2 * (dir[0] * (start[0] - origin[0]) + dir[1] * (start[1] - origin[1]) + dir[2] * (start[2] - origin[2]));
// c = (start[0] - origin[0])^2 + (start[1] - origin[1])^2 + (start[2] - origin[2])^2 - radius^2;
//
VectorSubtract( start, origin, v1 );
// dir is normalized so a = 1
a = 1.0f; //dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2];
b = 2.0f * ( dir[0] * v1[0] + dir[1] * v1[1] + dir[2] * v1[2] );
c = v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2] - ( radius + RADIUS_EPSILON ) * ( radius + RADIUS_EPSILON );
d = b * b - 4.0f * c; // * a;
if ( d > 0 ) {
sqrtd = SquareRootFloat( d );
// = (- b + sqrtd) * 0.5f; // / (2.0f * a);
fraction = ( -b - sqrtd ) * 0.5f; // / (2.0f * a);
//
if ( fraction < 0 ) {
fraction = 0;
} else {
fraction /= length;
}
if ( fraction < tw->trace.fraction ) {
tw->trace.fraction = fraction;
VectorSubtract( end, start, dir );
VectorMA( start, fraction, dir, intersection );
VectorSubtract( intersection, origin, dir );
#ifdef CAPSULE_DEBUG
l2 = VectorLength( dir );
if ( l2 < radius ) {
int bah = 1;
}
#endif
scale = 1 / ( radius + RADIUS_EPSILON );
VectorScale( dir, scale, dir );
VectorCopy( dir, tw->trace.plane.normal );
VectorAdd( tw->modelOrigin, intersection, intersection );
tw->trace.plane.dist = DotProduct( tw->trace.plane.normal, intersection );
tw->trace.contents = CONTENTS_BODY;
}
} else if ( d == 0 ) {
//t1 = (- b ) / 2;
// slide along the sphere
}
// no intersection at all
}
/*
================
CM_TraceThroughVerticalCylinder
get the first intersection of the ray with the cylinder
the cylinder extends halfheight above and below the origin
================
*/
static void CM_TraceThroughVerticalCylinder( traceWork_t *tw, vec3_t origin, float radius, float halfheight, vec3_t start, vec3_t end ) {
float length, scale, fraction, l1, l2;
float a, b, c, d, sqrtd;
vec3_t v1, dir, start2d, end2d, org2d, intersection;
// 2d coordinates
VectorSet( start2d, start[0], start[1], 0 );
VectorSet( end2d, end[0], end[1], 0 );
VectorSet( org2d, origin[0], origin[1], 0 );