Graphics Reference
In-Depth Information
int cmpAABBs(const void *a, const void *b)
{
// Sort on minimum value along either x, y, or z (specified in gSortAxis)
float minA = (*(AABB **)a)- > min[gSortAxis];
float minB = (*(AABB **)b)- > min[gSortAxis];
if (minA < minB) return -1;
if (minA > minB) return 1;
return 0;
}
After the array has been sorted, it is scanned for overlapping boxes. To avoid
keeping track of currently active intervals as the array is swept, this particular imple-
mentation scans the array forward for overlapping objects to the end of the currently
tested box interval.
void SortAndSweepAABBArray(void)
{
// Sort the array on currently selected sorting axis (gSortAxis)
qsort(gAABBArray, MAX_OBJECTS, sizeof(AABB *), cmpAABBs);
// Sweep the array for collisions
float s[3] = { 0.0f, 0.0f, 0.0f }, s2[3] = { 0.0f, 0.0f, 0.0f }, v[3];
for(inti=0;i<MAX_OBJECTS; i++) {
// Determine AABB center point
Point p = 0.5f * (gAABBArray[i]- > min + gAABBArray[i]- > max);
// Update sum and sum2 for computing variance of AABB centers
for(intc=0;c<3;c++) {
s[c] += p[c];
s2[c] += p[c] * p[c];
}
// Test collisions against all possible overlapping AABBs following current one
for(intj=i+1;j<MAX_OBJECTS; j++) {
// Stop when tested AABBs are beyond the end of current AABB
if (gAABBArray[j]- > min[gSortAxis] > gAABBArray[i]- > max[gSortAxis])
break;
if (AABBOverlap(gAABBArray[i], gAABBArray[j]))
TestCollision(gAABBArray[i], gAABBArray[j]);
}
}
// Compute variance (less a, for comparison unnecessary, constant factor)
 
Search WWH ::




Custom Search