Game Development Reference

In-Depth Information

Figure 3.10.
Merging data sets from two SPUs.

for( int i=0;i
<
numRigidBodies ; i ++)

{

Vector direction ;

distance .x = position[i ].x
−
average .x;

distance .y = position[i ].y

average .y;

distance .z = position[i ].z
−
average . z;

total distance .x += distance.x
∗

−

distance .x;

total distance .y += distance.y
∗

distance .y;

total distance .z += distance.z
∗

distance .z;

}

// Select the axis along which all rigid bodies are

// spread most widely .

if(total distance .x
>
total distance .y)

{

if ( total distance .x

>

total distance .z)

{

// Select X axis

}
else

// Select Z axis

}

}
else

if ( total distance .y
>
total distance .z)

{

// Select Y axis

}
else

// Select Z axis

}

}

Listing 3.1.
Calculate variance of rigid bodies to find the axis.

For example, Figure 3.11 shows a scene with seven rigid bodies. The
x
-axis is

selected as all rigid bodies are positioned mostly along this axis. When checking

the overlapping of A with other AABBs (B, F, C, E, D, and G), we note that the

AABBs are sorted in the order A, B, F, C, E, D, G . We can see AABBs of both

A and F are separated along the axis, so we don't need to check later AABBs.