123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- #pragma hdrstop
- #include "../idlib/precompiled.h"
- #include "CollisionModel_local.h"
- void idCollisionModelManagerLocal::TraceTrmThroughNode( cm_traceWork_t *tw, cm_node_t *node ) {
- cm_polygonRef_t *pref;
- cm_brushRef_t *bref;
-
- if ( tw->positionTest ) {
-
- if ( tw->trace.fraction == 0.0f ) {
- return;
- }
-
- for ( bref = node->brushes; bref; bref = bref->next ) {
- if ( idCollisionModelManagerLocal::TestTrmVertsInBrush( tw, bref->b ) ) {
- return;
- }
- }
-
- if ( tw->pointTrace ) {
- return;
- }
-
- for ( pref = node->polygons; pref; pref = pref->next ) {
- if ( idCollisionModelManagerLocal::TestTrmInPolygon( tw, pref->p ) ) {
- return;
- }
- }
- }
- else if ( tw->rotation ) {
-
- for ( pref = node->polygons; pref; pref = pref->next ) {
- if ( idCollisionModelManagerLocal::RotateTrmThroughPolygon( tw, pref->p ) ) {
- return;
- }
- }
- }
- else {
-
- for ( pref = node->polygons; pref; pref = pref->next ) {
- if ( idCollisionModelManagerLocal::TranslateTrmThroughPolygon( tw, pref->p ) ) {
- return;
- }
- }
- }
- }
- void idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( cm_traceWork_t *tw, cm_node_t *node, float p1f, float p2f, idVec3 &p1, idVec3 &p2) {
- float t1, t2, offset;
- float frac, frac2;
- float idist;
- idVec3 mid;
- int side;
- float midf;
- if ( !node ) {
- return;
- }
- if ( tw->quickExit ) {
- return;
- }
- if ( tw->trace.fraction <= p1f ) {
- return;
- }
-
- if ( node->polygons || (tw->positionTest && node->brushes) ) {
-
- idCollisionModelManagerLocal::TraceTrmThroughNode( tw, node );
- }
-
- if ( tw->positionTest && tw->trace.fraction == 0.0f ) {
- return;
- }
-
- if ( node->planeType == -1 ) {
- return;
- }
- #ifdef NO_SPATIAL_SUBDIVISION
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, node->children[0], p1f, p2f, p1, p2 );
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, node->children[1], p1f, p2f, p1, p2 );
- return;
- #endif
-
- t1 = p1[node->planeType] - node->planeDist;
- t2 = p2[node->planeType] - node->planeDist;
-
- offset = tw->extents[node->planeType];
-
- if ( t1 >= offset && t2 >= offset ) {
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, node->children[0], p1f, p2f, p1, p2 );
- return;
- }
- if ( t1 < -offset && t2 < -offset ) {
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, node->children[1], p1f, p2f, p1, p2 );
- return;
- }
- if ( t1 < t2 ) {
- idist = 1.0f / (t1-t2);
- side = 1;
- frac2 = (t1 + offset) * idist;
- frac = (t1 - offset) * idist;
- } else if (t1 > t2) {
- idist = 1.0f / (t1-t2);
- side = 0;
- frac2 = (t1 - offset) * idist;
- frac = (t1 + offset) * idist;
- } else {
- side = 0;
- frac = 1.0f;
- frac2 = 0.0f;
- }
-
- if ( frac < 0.0f ) {
- frac = 0.0f;
- }
- else if ( frac > 1.0f ) {
- frac = 1.0f;
- }
- midf = p1f + (p2f - p1f)*frac;
- mid[0] = p1[0] + frac*(p2[0] - p1[0]);
- mid[1] = p1[1] + frac*(p2[1] - p1[1]);
- mid[2] = p1[2] + frac*(p2[2] - p1[2]);
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, node->children[side], p1f, midf, p1, mid );
-
- if ( frac2 < 0.0f ) {
- frac2 = 0.0f;
- }
- else if ( frac2 > 1.0f ) {
- frac2 = 1.0f;
- }
-
- midf = p1f + (p2f - p1f)*frac2;
- mid[0] = p1[0] + frac2*(p2[0] - p1[0]);
- mid[1] = p1[1] + frac2*(p2[1] - p1[1]);
- mid[2] = p1[2] + frac2*(p2[2] - p1[2]);
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, node->children[side^1], midf, p2f, mid, p2 );
- }
- void idCollisionModelManagerLocal::TraceThroughModel( cm_traceWork_t *tw ) {
- float d;
- int i, numSteps;
- idVec3 start, end;
- idRotation rot;
- if ( !tw->rotation ) {
-
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, tw->model->node, 0, 1, tw->start, tw->end );
- }
- else {
-
-
- d = tw->radius * DEG2RAD( tw->angle );
-
- if ( d > CIRCLE_APPROXIMATION_LENGTH ) {
-
- numSteps = (int) (CIRCLE_APPROXIMATION_LENGTH / d);
-
- start = tw->start;
-
- for ( i = 0; i < numSteps; i++ ) {
-
- rot.Set( tw->origin, tw->axis, tw->angle * ((float) (i+1) / numSteps) );
- end = start * rot;
-
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, tw->model->node, 0, 1, start, end );
-
- if ( tw->trace.fraction < 1.0f ) {
- return;
- }
- start = end;
- }
- }
- else {
- start = tw->start;
- }
-
- idCollisionModelManagerLocal::TraceThroughAxialBSPTree_r( tw, tw->model->node, 0, 1, start, tw->end );
- }
- }
|