My Message close
GAME JOBS
Contents
Simple Intersection Tests For Games
 
 
Printer-Friendly VersionPrinter-Friendly Version
 
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
May 22, 2013
 
2K Games
Graphics Programmer - 2K Games
 
2K Games
Engine Programmer - 2K Games
 
2K Games
Tools Programmer - 2K Games
 
GREE International
Senior Product Manager, Growth and Revenue
 
GREE International
Business Intelligence Data Analyst
 
Synergy Blue
3D Artist / Animator
spacer
Latest Blogs
spacer View All     Post     RSS spacer
 
May 22, 2013
 
Using Small Studios As Stepping Stones In Your Career [2]
 
Maturity, Challenge, Art and Games
 
Combat Analysis: Guacamelee
 
Kickstarter Fu
 
Why every developer should play Aliens: Colonial Marines
spacer
About
spacer Editor-In-Chief:
Kris Graft
Blog Director:
Christian Nutt
Senior Contributing Editor:
Brandon Sheffield
News Editors:
Mike Rose, Kris Ligman
Editors-At-Large:
Leigh Alexander, Chris Morris
Advertising:
Jennifer Sulik
Recruitment:
Gina Gross
Education:
Gillian Crowley
 
Contact Gamasutra
 
Report a Problem
 
Submit News
 
Comment Guidelines
Sponsor
Features
  Simple Intersection Tests For Games
by miguel gomez []
1 comments Share on Twitter Share on Facebook RSS
 
 
October 18, 1999 Article Start Previous Page 3 of 7 Next
 

An Axis-Aligned Bounding Box (AABB) Sweep Test

Just like the name says, the faces of an axis-aligned bounding box are aligned with the coordinate axes of its parent frame (see Figure 3). In most cases AABBs can fit an object more tightly than a sphere, and their overlap test is extremely fast.



To see if A and B overlap, a separating axis test is used along the x, y, and z-axes. If the two boxes are disjoint, then at least one of these will form a separating axis. Figure 4 illustrates an overlap test in one dimension.

 

 

In this example the x-axis forms a separating axis because

Note that the separating axis test will return true even if one box fully contains the other. A more general separating axis test is given in the section below on oriented bounding boxes (OBB’s). Listing 3 defines an AABB class that implements this overlap test.

Listing 3. An AABB class.

#include "vector.h"

// An axis-aligned bounding box

class AABB
{

 

public:

VECTOR P; //position
VECTOR E; //x,y,z extents

AABB( const VECTOR& p,
const VECTOR& e ): P(p), E(e)

{}

//returns true if this is overlapping b
const bool overlaps( const AABB& b ) const
{

 

const VECTOR T = b.P - P;//vector from A to B
return fabs(T.x) <= (E.x + b.E.x)

&&

fabs(T.y) <= (E.y + b.E.y)

&&

fabs(T.z) <= (E.z + b.E.z);

}

//NOTE: since the vector indexing operator is not const,
//we must cast away the const of the this pointer in the
//following min() and max() functions
//min x, y, or z

const SCALAR min( long i ) const

{

 

return ((AABB*)this)->P[i] - ((AABB*)this)->E[i];

}

//max x, y, or z
const SCALAR max( long i ) const

{

 

return ((AABB*)this)->P[i] + ((AABB*)this)->E[i];

}

};

For more information on AABBs and their applications, please see [8].

Just like spheres, AABBs can be swept to find the first and last occurrence of overlap. In Figure 5(a), A and B experienced displacements va and vb, respectively, while Figure 5(b) shows B's displacement as observed by A.

 

Figure 6 shows B's displacement as observed by A.

In this example, the normalized times it took for the x-extents and y-extents to overlap are given by

and it can be seen that the x-extents will cross before the y-extents. The two boxes cannot overlap until all the extents are overlapping, and the boxes will cease to overlap when any one of these extents becomes disjoint. If u0,x, u0,y, and u0,z were the times at which the x, y, and z-extents began to overlap, then the earliest time at which the boxes could have begun to overlap was

Likewise, if u1,x, u1,y, and u1,z are the times at which the x, y, and z-extents become disjoint, then the earliest time at which the boxes could have become disjoint was

In order for the two boxes to have overlapped during their displacement, the condition

must have been met. Just like in the sphere sweep test, the positions of first and last overlap can be linearly interpolated with u. Listing 4 gives an implementation of this AABB sweep algorithm.

Listing 4. An AABB sweep algorithm.

#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;

}

 
Article Start Previous Page 3 of 7 Next
 
Top Stories

image
Xbox One is Microsoft's biggest play for living room domination
image
Opinion: Xbox One is a desperate prayer to stop time
image
Indies on Xbone: Where's the beef?
image
'If you're backwards compatible, you're really backwards.'
Comments

Dave Moss
profile image
The scaler u0 and u1 represent the time the objects will collide...


none
 
Comment:
 




UBM Tech