From 64239037e0566880187d92a09e3a21f4d2ebaa8c Mon Sep 17 00:00:00 2001 From: Tim Angus Date: Fri, 27 Jul 2007 21:52:31 +0000 Subject: [PATCH] * Early out AABB collision optimisation from Robert Beckebans (Xreal) --- code/qcommon/cm_patch.c | 5 +++++ code/qcommon/cm_test.c | 4 ++++ code/qcommon/cm_trace.c | 5 +++++ code/qcommon/q_math.c | 47 +++++++++++++++++++++++++++++++++++++++++ code/qcommon/q_shared.h | 9 +++++++- 5 files changed, 69 insertions(+), 1 deletion(-) diff --git a/code/qcommon/cm_patch.c b/code/qcommon/cm_patch.c index 88e36977..176aac1d 100644 --- a/code/qcommon/cm_patch.c +++ b/code/qcommon/cm_patch.c @@ -1386,6 +1386,11 @@ void CM_TraceThroughPatchCollide( traceWork_t *tw, const struct patchCollide_s * static cvar_t *cv; #endif //BSPC + if ( !BoundsIntersect( tw->bounds[0], tw->bounds[1], + pc->bounds[0], pc->bounds[1] ) ) { + return; + } + if (tw->isPoint) { CM_TracePointThroughPatchCollide( tw, pc ); return; diff --git a/code/qcommon/cm_test.c b/code/qcommon/cm_test.c index 8aeda357..5aee7ee1 100644 --- a/code/qcommon/cm_test.c +++ b/code/qcommon/cm_test.c @@ -250,6 +250,10 @@ int CM_PointContents( const vec3_t p, clipHandle_t model ) { brushnum = cm.leafbrushes[leaf->firstLeafBrush+k]; b = &cm.brushes[brushnum]; + if ( !BoundsIntersectPoint( b->bounds[0], b->bounds[1], p ) ) { + continue; + } + // see if the point is in the brush for ( i = 0 ; i < b->numsides ; i++ ) { d = DotProduct( p, b->sides[i].plane->normal ); diff --git a/code/qcommon/cm_trace.c b/code/qcommon/cm_trace.c index bc52c289..44ca5f82 100644 --- a/code/qcommon/cm_trace.c +++ b/code/qcommon/cm_trace.c @@ -685,6 +685,11 @@ void CM_TraceThroughLeaf( traceWork_t *tw, cLeaf_t *leaf ) { continue; } + if ( !BoundsIntersect( tw->bounds[0], tw->bounds[1], + b->bounds[0], b->bounds[1] ) ) { + continue; + } + CM_TraceThroughBrush( tw, b ); if ( !tw->trace.fraction ) { return; diff --git a/code/qcommon/q_math.c b/code/qcommon/q_math.c index 5ddd81fe..ae2de698 100644 --- a/code/qcommon/q_math.c +++ b/code/qcommon/q_math.c @@ -1037,6 +1037,53 @@ void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) { } } +qboolean BoundsIntersect(const vec3_t mins, const vec3_t maxs, + const vec3_t mins2, const vec3_t maxs2) +{ + if ( maxs[0] < mins2[0] || + maxs[1] < mins2[1] || + maxs[2] < mins2[2] || + mins[0] > maxs2[0] || + mins[1] > maxs2[1] || + mins[2] > maxs2[2]) + { + return qfalse; + } + + return qtrue; +} + +qboolean BoundsIntersectSphere(const vec3_t mins, const vec3_t maxs, + const vec3_t origin, vec_t radius) +{ + if ( origin[0] - radius > maxs[0] || + origin[0] + radius < mins[0] || + origin[1] - radius > maxs[1] || + origin[1] + radius < mins[1] || + origin[2] - radius > maxs[2] || + origin[2] + radius < mins[2]) + { + return qfalse; + } + + return qtrue; +} + +qboolean BoundsIntersectPoint(const vec3_t mins, const vec3_t maxs, + const vec3_t origin) +{ + if ( origin[0] > maxs[0] || + origin[0] < mins[0] || + origin[1] > maxs[1] || + origin[1] < mins[1] || + origin[2] > maxs[2] || + origin[2] < mins[2]) + { + return qfalse; + } + + return qtrue; +} vec_t VectorNormalize( vec3_t v ) { // NOTE: TTimo - Apple G4 altivec source uses double? diff --git a/code/qcommon/q_shared.h b/code/qcommon/q_shared.h index f4b9bd2c..e3a062ac 100644 --- a/code/qcommon/q_shared.h +++ b/code/qcommon/q_shared.h @@ -530,7 +530,7 @@ vec_t VectorLengthSquared( const vec3_t v ); vec_t Distance( const vec3_t p1, const vec3_t p2 ); vec_t DistanceSquared( const vec3_t p1, const vec3_t p2 ); - + void VectorNormalizeFast( vec3_t v ); void VectorInverse( vec3_t v ); @@ -563,6 +563,13 @@ void AxisCopy( vec3_t in[3], vec3_t out[3] ); void SetPlaneSignbits( struct cplane_s *out ); int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *plane); +qboolean BoundsIntersect(const vec3_t mins, const vec3_t maxs, + const vec3_t mins2, const vec3_t maxs2); +qboolean BoundsIntersectSphere(const vec3_t mins, const vec3_t maxs, + const vec3_t origin, vec_t radius); +qboolean BoundsIntersectPoint(const vec3_t mins, const vec3_t maxs, + const vec3_t origin); + float AngleMod(float a); float LerpAngle (float from, float to, float frac); float AngleSubtract( float a1, float a2 );