   Contents
Simple Intersection Tests For Games Latest Jobs
March 22, 2019 Latest Blogs Press Releases
March 22, 2019
Games Press About Gama Network
If you enjoy reading this site, you might also want to check out these UBM Tech sites: Features

# Simple Intersection Tests For Games

[]

October 18, 1999  Page 5 of 7 An Oriented Bounding Box (OBB) Intersection Test

A drawback of using an axis-aligned bounding box is that it can’t fit rotating geometry very tightly.

On the other hand, an oriented bounding box can be rotated with the objects, fitting the geometry with less volume than an AABB. This requires that the orientation of the box must also be specified. Figure 8 shows a 2D example, where A1, A2, B1 and B2 are the local axes of boxes A and B. For OBBs, the separating axis test must be generalized to three dimensions. A box's scalar projection onto a unit vector L creates an interval along the axis defined by L. The radius of the projection of box A onto L is The same is true for B, and L forms a separating axis if Note that L does not have to be a unit vector for this test to work. The boxes A and B are disjoint if none of the 6 principal axes and their 9 cross products form a separating axis. These tests are greatly simplified if T and B’s basis vectors (B1, B2, B3) are transformed into A’s coordinate frame.

An OBB class and an implementation of the OBB overlap test is given in Listing 6 below.

 Listing 6. An OBB class. #include "coordinate_frame.h" class OBB : public COORD_FRAME { public:   VeCTOR E; //extents OBB( const VECTOR& e ): E(e) {} }; //check if two oriented bounding boxes overlap const bool OBBOverlap (   //A VECTOR& a, //extents VECTOR& Pa, //position VECTOR* A, //orthonormal basis //B VECTOR& b, //extents VECTOR& Pb, //position VECTOR* B //orthonormal basis ) {   //translation, in parent frame VECTOR v = Pb - Pa; //translation, in A's frame VECTOR T( v.dot(A), v.dot(A), v.dot(A) ); //B's basis with respect to A's local frame SCALAR R; float ra, rb, t; long i, k; //calculate rotation matrix for( i=0 ; i<3 ; i++ )   for( k=0 ; k<3 ; k++ )   R[i][k] = A[i].dot(B[k]); /*ALGORITHM: Use the separating axis test for all 15 potential separating axes. If a separating axis could not be found, the two boxes overlap. */ //A's basis vectors for( i=0 ; i<3 ; i++ ) {   ra = a[i]; rb = b*fabs(R[i]) + b*fabs(R[i]) + b*fabs(R[i]); t = fabs( T[i] ); if( t > ra + rb ) return false; } //B's basis vectors for( k=0 ; k<3 ; k++ ) {   ra = a*fabs(R[k]) + a*fabs(R[k]) + a*fabs(R[k]); rb = b[k]; t = fabs( T*R[k] + T*R[k] + T*R[k] ); if( t > ra + rb ) return false; } //9 cross products //L = A0 x B0 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A0 x B1 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A0 x B2 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A1 x B0 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A1 x B1 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A1 x B2 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A2 x B0 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A2 x B1 ra = a*fabs(R) + a*fabs(R); rb = b *fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; //L = A2 x B2 ra = a*fabs(R) + a*fabs(R); rb = b*fabs(R) + b*fabs(R); t = fabs( T*R - T*R ); if( t > ra + rb ) return false; /*no separating axis found, the two boxes overlap */ return true; }

For a more complete discussion of OBBs and the separating axis test, please see . Some other applications of the separating axis test are given next.  Page 5 of 7 ### Top Stories 