Advertisement

[Bullet] btBvhTriangleMeshShape AABB problem

Started by August 06, 2017 04:59 AM
0 comments, last by lucky6969b 7 years, 6 months ago

In the code snippet below, the static mesh I am adding to the engine always falls below zero. If the transform is at y zero, why would the bounding volume has a negative y value in it?

Thanks

Jack


	//////////////////////////////
/// AddStaticMesh
void PhysicsMover::addStaticMesh(LPD3DXMESH pMesh)
{
    btBvhTriangleMeshShape* staticShape = NULL;
	    Transform* l_transform = m_simObject->FindComponentByType<Transform>();
	    DWORD numFaces = pMesh->GetNumFaces();
    DWORD numIndices = numFaces * 3;
	    if (numIndices < 65535)
    {
        typedef unsigned short IndexFormat;
    
        btTriangleIndexVertexArray*
            indexVertexArrays = new btTriangleIndexVertexArray();
	        LPDIRECT3DVERTEXBUFFER9 pVertices;
        pMesh->GetVertexBuffer(&pVertices);
        DWORD dwVertexSize = pMesh->GetNumBytesPerVertex();
        DWORD dwNumVerts = pMesh->GetNumVertices();
        BYTE* pVertexData;
        pVertices->Lock(0, 0, (void**)&pVertexData, 0);
	        LPDIRECT3DINDEXBUFFER9 pIndices;
        pMesh->GetIndexBuffer(&pIndices);
        BYTE* pIndexData;
        pIndices->Lock(0, 0, (void**)&pIndexData, 0);
	        btIndexedMesh iMesh;
        iMesh.m_numTriangles        = pMesh->GetNumFaces();
        iMesh.m_triangleIndexBase   = (const unsigned char *)pIndexData;
        iMesh.m_triangleIndexStride = 3 * sizeof(IndexFormat);
        iMesh.m_numVertices         = dwNumVerts;
        iMesh.m_vertexBase          = (const unsigned char *)pVertexData;
        iMesh.m_vertexStride        = sizeof(D3DXVECTOR3);
        indexVertexArrays->addIndexedMesh( iMesh, PHY_SHORT );
	        // For non-moving objects only
        staticShape = new btBvhTriangleMeshShape(indexVertexArrays, true);
    
        m_collisionShapes.push_back(staticShape);
 
        pIndices->Unlock();
        pVertices->Unlock();
    }
    else
    {
        typedef DWORD IndexFormat;
    
        btTriangleIndexVertexArray*
            indexVertexArrays = new btTriangleIndexVertexArray();
	        LPDIRECT3DVERTEXBUFFER9 pVertices;
        pMesh->GetVertexBuffer(&pVertices);
        DWORD dwVertexSize = pMesh->GetNumBytesPerVertex();
        DWORD dwNumVerts = pMesh->GetNumVertices();
        BYTE* pVertexData;
        pVertices->Lock(0, 0, (void**)&pVertexData, 0);
	        LPDIRECT3DINDEXBUFFER9 pIndices;
        pMesh->GetIndexBuffer(&pIndices);
        BYTE* pIndexData;
        pIndices->Lock(0, 0, (void**)&pIndexData, 0);
	        indexVertexArrays =
            new btTriangleIndexVertexArray(pMesh->GetNumFaces(),
            (int*)pIndexData, 3 * sizeof(int), pMesh->GetNumVertices(),
            (btScalar*)pVertexData, dwVertexSize);
	        // For non-moving objects only
        staticShape = new btBvhTriangleMeshShape(indexVertexArrays, true);
    
        m_collisionShapes.push_back(staticShape);
 
        pIndices->Unlock();
        pVertices->Unlock();
    }
    
    // collision shapes are attached to transforms and not coupled to meshes themselves
	    btTransform startTransform;
    startTransform.setIdentity();
    D3DXVECTOR3 ori = l_transform->getPosition();
    if (m_simObject->m_name=="Warehouse3")
    {
        startTransform.setOrigin(btVector3(ori.x,
            ori.y, ori.z));
    }
    else
    {
        //startTransform.setOrigin(btVector3(ori.x,
        //    0, ori.z));
	        // the floor of the warehouse1 has fallen by 0.17 meters
        startTransform.setOrigin(btVector3(ori.x,
            ori.y, ori.z));
    }
	AgentMotionState* myMotionState = new AgentMotionState(m_simObject);
    myMotionState->setWorldTransform(startTransform);
	btRigidBody::btRigidBodyConstructionInfo cInfo(0, myMotionState, staticShape);
    btRigidBody* body = new btRigidBody(cInfo);
    body->setContactProcessingThreshold(m_defaultContactProcessingThreshold);
	btVector3 bmin, bmax;
    staticShape->getAabb(startTransform, bmin, bmax);
	m_dynamicsWorld->addRigidBody(body);
    
}
	

This topic is closed to new replies.

Advertisement