Your variables suck.
I’m going to steal a function from Miguel Gomez’s article on intersection tests for games as an example of what NOT to do. Yes this code is 12 years old, but it’s still the number one result when you google ‘aabb sweep test.’ Also, it’s not like you haven’t seen code written in 2011 that looks like this.
#include "aabb.h"
//Sweep two AABB's to see if and when they first
//and last were overlapping
const bool AABBSweep(
const VECTOR& Ea, //extents of AABB A
const VECTOR& A0, //its previous position
const VECTOR& A1, //its current position
const VECTOR& Eb, //extents of AABB B
const VECTOR& B0, //its previous position
const VECTOR& B1, //its current position
SCALAR& u0, //normalized time of first collision
SCALAR& u1 //normalized time of second collision
)
{
const AABB A( A0, Ea );//previous state of AABB A
const AABB B( B0, Eb );//previous state of AABB B
const VECTOR va = A1 - A0;//displacement of A
const VECTOR vb = B1 - B0;//displacement of B
//the problem is solved in A's frame of reference
VECTOR v = vb - va;
//relative velocity (in normalized time)
VECTOR u_0(0,0,0);
//first times of overlap along each axis
VECTOR u_1(1,1,1);
//last times of overlap along each axis
//check if they were overlapping
// on the previous frame
if( A.overlaps(B) )
{
u0 = u1 = 0;
return true;
}
//find the possible first and last times
//of overlap along each axis
for( long i=0 ; i<3 ; i++ )
{
if( A.max(i)<b .min(i) && v[i]<0 )
{
u_0[i] = (A.max(i) - B.min(i)) / v[i];
}
else if( B.max(i)<A.min(i) && v[i]>0 )
{
u_0[i] = (A.min(i) - B.max(i)) / v[i];
}
if( B.max(i)>A.min(i) && v[i]<0 )
{
u_1[i] = (A.min(i) - B.max(i)) / v[i];
}
else if( A.max(i)>B.min(i) && v[i]>0 )
{
u_1[i] = (A.max(i) - B.min(i)) / v[i];
}
}
//possible first time of overlap
u0 = MAX( u_0.x, MAX(u_0.y, u_0.z) );
//possible last time of overlap
u1 = MIN( u_1.x, MIN(u_1.y, u_1.z) );
//they could have only collided if
//the first time of overlap occurred
//before the last time of overlap
return u0 < = u1;
}
AI/Graphics/iOS Engineer at