Game Development Reference
In-Depth Information
In the MeshEx constructor, we call the function that calculates the bounding sphere of the mesh being
created, which is CalculateRadius() . (See Listing 5-15.)
The CalculateRadius() function calculates the bounding sphere radius for a 3D object by doing the
following:
1.
Searching through all the object's vertices and determining the smallest and
largest x, y, and z coordinates.
2.
Finding the size of the object along its x, y, and z axes, based on the
minimum and maximum values of the x, y, and z coordinates found in the
step above.
3.
Calculating the collision radius from the largest portion of the object in
the x, y, or z axes direction. Assuming the object is centered at the origin,
the largest size values in either the x, y, or z direction would represent the
object's diameter. The collision radius would be half of that diameter.
4.
Calculating the average radius based on the average of the object's x, y, and
z lengths as the diameter. The final average radius is half of this diameter.
Listing 5-15. Calculating the Radius of an Object's Mesh
void CalculateRadius()
{
float XMin = 100000000;
float YMin = 100000000;
float ZMin = 100000000;
float XMax = -100000000;
float YMax = -100000000;
float ZMax = -100000000;
int ElementPos = m_MeshVerticesDataPosOffset;
// Loop through all vertices and find min and max values of x,y,z
for (int i = 0; i < m_VertexCount; i++)
{
float x = m_VertexBuffer.get(ElementPos);
float y = m_VertexBuffer.get(ElementPos+1);
float z = m_VertexBuffer.get(ElementPos+2);
// Test for Min
if (x < XMin)
{
XMin = x;
}
if (y < YMin)
{
YMin = y;
}
 
Search WWH ::




Custom Search