[DirectX] Bounding Box on a Frame Hierachy

Started by
2 comments, last by liamf1986 13 years, 6 months ago
Hey guys,
I've been working on my animated model class and i'm having an issue with calculating the AxisAlignedBoundingBox for the entire frame Hierachy.
I've got the bounding box being calculated upon loading the model, and it is working to an extent. The problem is the resulting box is too large for some reason.
The box is working in that it is the right shape and is adjusting itself as the model moves or rotates but its about 3-4x the size it should be.

The code calculating the Box is as follows,

This first part is at the end of the load function shortly after the call to D3DXLoadMeshHierarchyFromX()
Vector3 min = Vector3(0, 0, 0), max = Vector3(0, 0, 0);FrameCalculateBoundingBox(pFrameRoot, &min, &max);boundsBox.arrBoundsPoints[0] = Vector3(min.x, min.y, min.z);boundsBox.arrBoundsPoints[1] = Vector3(max.x, min.y, min.z);boundsBox.arrBoundsPoints[2] = Vector3(min.x, max.y, min.z);boundsBox.arrBoundsPoints[3] = Vector3(max.x, max.y, min.z);boundsBox.arrBoundsPoints[4] = Vector3(min.x, min.y, max.z);boundsBox.arrBoundsPoints[5] = Vector3(max.x, min.y, max.z);boundsBox.arrBoundsPoints[6] = Vector3(min.x, max.y, max.z);boundsBox.arrBoundsPoints[7] = Vector3(max.x, max.y, max.z);

The following is the function that calls its self recursivly to loop through the hieracrhy and get the min and max vertex positions to use for the BoundingBox.
void AnimatedMesh::FrameCalculateBoundingBox(LPD3DXFRAME frame, Vector3 *min, Vector3 *max){    LPD3DXMESHCONTAINER meshContainer = frame->pMeshContainer;    // If this Frame contains a mesh...    while (meshContainer)    {        BYTE* pData = NULL;        meshContainer->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&pData);        // Compute the Min and Max values for this frames mesh        Vector3 tempMin, tempMax;        D3DXComputeBoundingBox((Vector3*)pData, meshContainer->MeshData.pMesh->GetNumVertices(), D3DXGetFVFVertexSize(meshContainer->MeshData.pMesh->GetFVF()), &tempMin, &tempMax);        meshContainer->MeshData.pMesh->UnlockVertexBuffer();		        // If the min/max values are lower/greater then the current stored values, update them.        min->x = min(min->x, tempMin.x);        min->y = min(min->y, tempMin.y);        min->z = min(min->z, tempMin.z);        max->x = max(max->x, tempMax.x);        max->y = max(max->y, tempMax.y);        max->z = max(max->z, tempMax.z);        meshContainer = meshContainer->pNextMeshContainer;    }    // Recurse for sibblings    if (frame->pFrameSibling != NULL)        FrameCalculateBoundingBox(frame->pFrameSibling, min, max);    // Recurse for children    if (frame->pFrameFirstChild != NULL)        FrameCalculateBoundingBox(frame->pFrameFirstChild, min, max);}

(Just to note the above code isn't handling the axis alignment thats done every frame created from this initial box dependant on the rotation/scaling/positioning of the model in question.)

The bounding Sphere was easy enough with a call to D3DXFrameCalculateBoundingSphere()
However I'm fairly sure there is no such function implemented for Bounding Boxes (I could be wrong).

Thanks in advance for any help.
(edited post to place code in source brackets instead of code)

[Edited by - liamf1986 on October 28, 2010 11:49:46 AM]
Advertisement
I've uploaded an image to show the problem better in case I didn't explain it too well.

Bounding Box problem

As you can see the box is about the right shape just far to large.
It seems there is some kind of scaling being applied to the model itself during loading, though i'm not sure where. My loading code is based on the examples over at Toymaker.com (http://www.toymaker.info/Games/html/load_x_hierarchy.html) if anyone is familiar with it.

Anyway i've got my Hierachy of Bounding boxes working as you can see at the bottom of this post. This is working because the positioning of the AABoundingBoxes is calculated using the World Matrix of the bone it belongs to which is what makes me think the model is being down scaled somewhere in the loading process.

Maybe if i take the world Matrix of just one bone into consideration during loading and scale the BoundingBox by that matrix that should hopefuly work. Just need to make sure its before I personally do any scaling/rotating/positioning myself.

Axis Aligned Bounds Hierachy
I'm thinking now that a boundingBox for the entire model may not be worth it.

My plan was to use the overall bounding box as an initial check and then check with each individual bone if a collision was found, in order to save processing time.

But the initial Bounding Box will be calculated while the model is in bind pose, then when animating the model arms and legs will be moving in and out of the box all the time, so to keep it acurate the overall box would need to be recalculated each frame therefore negating the benefit of less time spent performing collision checks...

So I guess thats my problem solved.

This topic is closed to new replies.

Advertisement