Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Using D3DXComputeBoundingBox

This topic is 1964 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

How do I use D3DXComputeBoundingBox() to calculate the box width/height/depth to create Bullet Physics btBoxShape?

 

I tried different ways but couldn't get it to work.

Share this post


Link to post
Share on other sites
Advertisement

D3DXVECTOR bMin, bMax;
BYTE* pData = 0;
mesh->LockVertexBuffer(0, (LPVOID*)&pData);
D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(),
            mesh->GetNumBytesPerVertex(), &bMin, &bMin);
mesh->UnlockVertexBuffer();
 
D3DXVECTOR3 boxHalfExtent = (bMax - bMin) * 0.5f;

Share this post


Link to post
Share on other sites

I tried your code after correcting the line:

D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(), mesh->GetNumBytesPerVertex(), &bMin, &bMin);

 

To:

D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(),mesh->GetNumBytesPerVertex(), &bMin, &bMax);

 

Now, the physical box appears to be larger than the actual mesh:

[attachment=15593:space.png]

Share this post


Link to post
Share on other sites

 

{
LPD3DXMESH mesh = GetMesh();

D3DXVECTOR3 bMin, bMax;
BYTE* pData = 0;
mesh->LockVertexBuffer(0, (LPVOID*)&pData);
D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(),
            mesh->GetNumBytesPerVertex(), &bMin, &bMax);
mesh->UnlockVertexBuffer();

D3DXVECTOR3 boxHalfExtent = (bMax - bMin) * 0.5f;
btVector3 btBoxHalfExtent(boxHalfExtent.x, boxHalfExtent.y, boxHalfExtent.z);

btBoxShape* boxShape = new btBoxShape(btBoxHalfExtent);

//keep track of the shapes, we release memory at exit.
//make sure to re-use collision shapes among rigid bodies whenever possible!
btAlignedObjectArray<btCollisionShape*> collisionShapes;

collisionShapes.push_back(boxShape);

btTransform trans;
trans.setIdentity();
trans.setOrigin(btVector3(0.0f, 0.0f, 0.0f));
btScalar mass(100.0f);

bool isDynamic = (mass != 0.f);

btVector3 localInertia(0,0,0);
if (isDynamic)
    boxShape->calculateLocalInertia(mass,localInertia);

// using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,boxShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);

// add the body to the dynamics world
mDynamicsWorld->addRigidBody(body);
}

 

Share this post


Link to post
Share on other sites

I noticed that I DON'T get this problem when I use D3DXCreateBox() for creating the box.

 

I only get this problem when I load the box mesh from .x file using D3DXLoadMeshFromX().

Edited by Medo3337

Share this post


Link to post
Share on other sites

Is your mesh centered at origin in modeling program?

If not you must then change btTransform origin, but i don't recommend this, better to fix this in modeling editor.

Share this post


Link to post
Share on other sites

Your box is working well.

 

Here is how I create my box, though it doesn't work:

[attachment=15610:mbox.png]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!