Sign in to follow this  

Collision detection code won't work for hierarchical objects

This topic is 2380 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. [code]
bool CMesh::InitBounds()
{

D3DXVECTOR3 vBoxMin, vBoxMax;
float fRadius;
LPD3DXFRAME pCurFrame = mFrameRoot;
LPD3DXMESHCONTAINER pCurMesh;
stack<LPD3DXFRAME> st;
st.push(pCurFrame);
while(!st.empty())
{
pCurFrame = st.top();
st.pop();
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);
pCurMesh->MeshData.pMesh->UnlockVertexBuffer();

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;
}
st.push(pCurFrame->pFrameFirstChild);
st.push(pCurFrame->pFrameSibling);
}
}
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);
mMesh->UnlockVertexBuffer();

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;
}
[/code]

Share this post


Link to post
Share on other sites

This topic is 2380 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this