• Advertisement
Sign in to follow this  

Collision detection code won't work for hierarchical objects

This topic is 2443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, With the following code snippet, it works fine for static objects which don't have hierarchies. Any ideas why? It works for chunk meshes. Bounding boxes just gone missing when I render the hierarchical objects.
bool CMesh::InitBounds()

D3DXVECTOR3 vBoxMin, vBoxMax;
float fRadius;
LPD3DXFRAME pCurFrame = mFrameRoot;
stack<LPD3DXFRAME> st;
pCurFrame = st.top();
if(pCurFrame != NULL)
pCurMesh = pCurFrame->pMeshContainer;
while(pCurMesh != NULL)
if(pCurMesh->MeshData.pMesh != NULL)
BYTE *pVertices;
pCurMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
D3DXComputeBoundingBox( (D3DXVECTOR3*)pVertices, pCurMesh->MeshData.pMesh->GetNumVertices(),
D3DXGetFVFVertexSize(pCurMesh->MeshData.pMesh->GetFVF()), &vBoxMin, &vBoxMax );
// m_vSphere center is dummy, dx function won't work if not filled
D3DXComputeBoundingSphere((D3DXVECTOR3*)pVertices, pCurMesh->MeshData.pMesh->GetNumVertices(),
D3DXGetFVFVertexSize(pCurMesh->MeshData.pMesh->GetFVF()), &m_vSphereCenter, &fRadius);

m_vBoxMin.x = min(m_vBoxMin.x, vBoxMin.x);
m_vBoxMin.y = min(m_vBoxMin.y, vBoxMin.y);
m_vBoxMin.z = min(m_vBoxMin.z, vBoxMin.z);
m_vBoxMax.x = max(m_vBoxMax.x, vBoxMax.x);
m_vBoxMax.y = max(m_vBoxMax.y, vBoxMax.y);
m_vBoxMax.z = max(m_vBoxMax.z, vBoxMax.z);
m_fSphereRadius = max(m_fSphereRadius, fRadius);
pCurMesh = pCurMesh->pNextMeshContainer;
m_vSphereCenter = (m_vBoxMax + m_vBoxMin) / 2.0f;

BYTE* pVertices;
mMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
D3DXComputeBoundingBox( (D3DXVECTOR3*)pVertices, mMesh->GetNumVertices(),
D3DXGetFVFVertexSize(mMesh->GetFVF()), &m_vMind, &m_vMaxd );
D3DXComputeBoundingSphere((D3DXVECTOR3*)pVertices, mMesh->GetNumVertices(),
D3DXGetFVFVertexSize(mMesh->GetFVF()), &m_vCenterd, &m_fRadiusd);

float x = fabs(m_vBoxMax.x - m_vBoxMin.x);
float y = fabs(m_vBoxMax.y - m_vBoxMin.y);
float z = fabs(m_vBoxMax.z - m_vBoxMin.z);

D3DXCreateBox(m_pDevice, fabs(m_vBoxMax.x - m_vBoxMin.x), fabs(m_vBoxMax.y - m_vBoxMin.y),
fabs(m_vBoxMax.z - m_vBoxMin.z), &m_pBoundingBox, NULL);
D3DXCreateSphere(m_pDevice, m_fSphereRadius, 12, 12, &m_pBoundingSphere, NULL);

D3DXComputeNormals(m_pBoundingBox, NULL);
D3DXComputeNormals(m_pBoundingSphere, NULL);

return true;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement