Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Gamasutra: The Art & Business of Making Gamesspacer
Simple Intersection Tests For Games
View All     RSS
May 21, 2019
arrowPress Releases
May 21, 2019
Games Press
View All     RSS








If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

Simple Intersection Tests For Games

by miguel gomez []

October 18, 1999 Article Start Previous Page 4 of 7 Next
 

A Box-Sphere Intersection Test

A very elegant box-sphere intersection test is described in [1]. Figure 7 shows two configurations of a sphere and a box in 2D. Sphere A is closest to an edge, whereas sphere B is closest to a corner. The algorithm calculates the square of the distance from the box to the sphere by analyzing the orientation of the sphere relative to the box in a single loop.

If the box is not axis aligned, simply transform the center of the sphere to the box's local coordinate frame. Listing 5 gives an implementation of Arvo's algorithm.

Listing 5. Arvo's algorithm.

#include "aabb.h"

//Check to see if the sphere overlaps the AABB
const bool AABBOverlapsSphere ( const AABB& B, const SCALAR r, VECTOR& C )
{

float s, d = 0;

//find the square of the distance
//from the sphere to the box
for( long i=0 ; i<3 ; i++ )
{

if( C[i] < B.min(i) )
{

 

s = C[i] - B.min(i);
d += s*s;

}

else if( C[i] > B.max(i) )
{

s = C[i] - B.max(i);
d += s*s;

}

}
return d <= r*r;

}


Article Start Previous Page 4 of 7 Next

Related Jobs

Insomniac Games
Insomniac Games — Burbank, California, United States
[05.21.19]

User Experience Researcher
Qualcomm
Qualcomm — San Diego, California, United States
[05.21.19]

PBR Environment Game Artist
Infinity Ward / Activision
Infinity Ward / Activision — Woodland Hills, California, United States
[05.21.19]

UI Engineer (Temporary)
FoxNext Games
FoxNext Games — San Jose , California, United States
[05.20.19]

Senior Character Artist





Loading Comments

loader image