• Advertisement
Sign in to follow this  

Using D3DXComputeBoundingBox()

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

Hi,

I have been trying to use D3DXComputeBoundingBox() to calculate a box width, length and depth.

I have tried the following code, but it's not working:


[CODE]
LPD3DXMESH mesh = GetBoxMesh();
BYTE* pData;
mesh->LockVertexBuffer( NULL, (LPVOID*)pData );

D3DXVECTOR3 vecMin;
D3DXVECTOR3 vecMax;
D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(), mesh->GetNumBytesPerVertex(), &vecMin, &vecMax);
float fWidth = vecMax.x - vecMin.x;
float fLength = vecMax.y - vecMin.y;
float fDepth = vecMax.z - vecMin.z;
mesh->UnlockVertexBuffer();

[/CODE]

I'm not getting a valid values for (fWidth, fLength, fDepth), any idea what's wrong?

Share this post


Link to post
Share on other sites
Advertisement
This is wrong:

[code]
BYTE* pData;
mesh->LockVertexBuffer( NULL, (LPVOID*)pData );
[/code]

You're taking a value of type "BYTE*" and casting it to type "void**". In other words you're casting from a pointer to a double pointer. You need to take the address of your pointer and cast that to (void**).

Share this post


Link to post
Share on other sites
I tried changing the code to:
[CODE]
BYTE *pData;
mesh->LockVertexBuffer( NULL, (void**)&pData );
[/CODE]

I'm still not getting valid values for (fWidth, fLength, fDepth).

Share this post


Link to post
Share on other sites
I even tried to create a box using D3DXCreateBox() and tried to retrieve the values, but can't get it to work:
[CODE]LPD3DXMESH mesh;
D3DXCreateBox(d3ddev, 100.0f, 200.0f, 300.0f, &mesh, NULL);
BYTE *pData = NULL;
mesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&pData);

D3DXVECTOR3 vecMin;
D3DXVECTOR3 vecMax;
D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(), D3DXGetFVFVertexSize(mesh->GetFVF()), &vecMin, &vecMax);
mesh->UnlockVertexBuffer();
float fWidth = vecMax.x - vecMin.x;
float fLength = vecMax.y - vecMin.y;
float fDepth = vecMax.z - vecMin.z;
[/CODE]


fWidth, fLength and fDepth are not 100, 200, 300 as given to D3DXCreateBox().

Share this post


Link to post
Share on other sites
I notice you aren't telling the compute routine which part of the vertex buffer to get it's data from. A vertex buffer holds multiple items such as the vertices, UV coordinates, normals etc...

Instead of using a BYTE for the pData, use a structure matching your vertex layout such as the following...

struct VERTEX { D3DXVECTOR3 p; D3DXVECTOR3 n; FLOAT tu; FLOAT tv; static const DWORD FVF;};

VERTEX *pData;

In the D3DXComputeBoundingBox part change the (D3DXVECTOR3*)pData to &pData[0].p

Let me know if that worked, it works fine for me (in a slightly different way)...

Share this post


Link to post
Share on other sites

I got this problem resolved, but I have another problem that I have been trying to fix since yesterday.

I'm trying to create btBoxShape from the box information (boxWidth, boxHeight, boxDepth), but I see a larger box a result.
 

float boxWidth = (vecMax.x - vecMin.x);
float boxHeight = (vecMax.y - vecMin.y);
float boxDepth = (vecMax.z - vecMin.z);

btVector3 boxHalfExtents(boxWidth, boxHeight, boxDepth);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);
Edited by Michael Tanczos

Share this post


Link to post
Share on other sites

Forgive me if I am stating the obvious but...

 

You do realize that you are giving the boxHalfExtents, don't you? So the width of the box will be 2 * boxWidth, for example.

Share this post


Link to post
Share on other sites
Oh, I want to mention that was a mistake since I wrote the code here instead of copying it.

Yes, I tried:
btVector3 boxHalfExtents(boxWidth*.5f, boxHeight*.5f, boxDepth*.5f);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);

I also tried:
btVector3 boxHalfExtents(boxWidth/2.0f, boxHeight/2.0f, boxDepth/2.0f);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);

Both are not working as expected, the box is larger than the real one. Edited by Medo3337

Share this post


Link to post
Share on other sites
I also want to mention that when I create a box in 3Ds Max with (Length, Width, Height) of 800 and export it to .x file then load it into DirectX 9 program, then I create btBoxShape by using 400 which is half 800 as the following:

btVector3 boxHalfExtents(400, 400, 400);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);

The physical box appears much larger than the rendered one, I am trying to get the physical box to be exactly the same size as the box that I create in 3Ds Max so I can have a box behaving like in physical world. Edited by Medo3337

Share this post


Link to post
Share on other sites

Well, all I can say is that it seems your physical world scale and renderer scale are out of sync. Are you sure you export with the correct units in 3DS Max? If I remember correctly Bullet uses the metric system.

Share this post


Link to post
Share on other sites
I think the problem has nothing to do with 3Ds Max, since I tried to create a box by writing its dimensions according to 3Ds Max instead of loading .x file.

How can I convert 3Ds Max box dimensions to Bullet box dimensions?

I tried:
float 3dsMaxBoxWidth = 800.0f;
float 3dsMaxBoxHeight = 800.0f;
float 3dsMaxBoxDepth = 800.0f;

btVector3 boxHalfExtents(3dsMaxBoxWidth/2.0, 3dsMaxBoxHeight/2.0, 3dsMaxBoxDepth/2.0); // Not working

I guess that's not the correct way to convert 3Ds max box dimensions to Bullet box dimensions, perhaps I should convert 3Ds Max measuring unit to Bullet measuring unit?

Share this post


Link to post
Share on other sites

What if you create the box entirely through code? For something as simple as a box it is easy to just fill out the vertex and index buffers by hand. Make its size (1, 1, 1). Then, create a physics box in bullet with the same size. Do they now align correctly? If that is the case you know that the problem is with importing stuff from 3DS Max. If not, then make sure you fix the problems before even thinking about 3DS Max.

 

If the engine now works as it should but importing from 3DS Max still produces too large objects you can always scale the data down by hand. Though I would think that with a little bit of searching you'll find the problem there too.

Share this post


Link to post
Share on other sites
Yes, I tried that as well.

In 3Ds Max (It will be used for rendering only):
Box size: 10, 10, 10

In physics, I set the numbers by writing them manually instead of extracting them from LPD3DXMESH, so I write:
btBoxShape* boxShape = new btBoxShape(btVector3(10/2, 10/2, 10/2));

The box is not on the ground which means the physics box is larger than the visual box.

BTW, the measuring unit is set to "Generic Units" in 3Ds Max.

Share this post


Link to post
Share on other sites
So you created the box in 3DS Max? I told you not to do that, but instead fill in the buffers yourself through code. That is the easiest way to tell if the problem is in the max import or not.

Merry Xmas! Edited by GuyWithBeard

Share this post


Link to post
Share on other sites
Okay, I tried using D3DXCreateBox() to create the box instead of loading it from external file and it's working perfectly.

Now, I'm not sure how to resolve the issue when using 3Ds Max .x file, maybe I should change 3Ds Max measuring unit?

Merry Christmas and Happy New Year! Edited by Medo3337

Share this post


Link to post
Share on other sites

Because the D3DXComputeBoundingBox has a limitation: it can only computes a bounding box align to the coordinate-axis. When you use D3DXCreateBox or use 3ds max to create a box not align to the coordinate-axis you will find that the bounding box always align to the coordinate-axis, not align to the object axis, so the size is much larger than the size of the box.

 

Alin

Share this post


Link to post
Share on other sites
D3DXCreateBox() works perfectly with bullet.

3Ds Max box doesn't work and the box created with bullet btBoxShape after loading it from box.x is not the same size.

Should I change the settings in 3Ds Max to resolve the issue? Edited by Medo3337

Share this post


Link to post
Share on other sites

Any idea how to solve this problem?

 

I'm trying to create the box in 3Ds Max instead of D3DXCreateBox() and load its information using bullet.

Share this post


Link to post
Share on other sites
Change the size of the box in 3d max export then check box size repeat until you get the correct size then that is the scale you need to creat all you 3dsmax boxes.

Share this post


Link to post
Share on other sites

@ankhd: That's not a good idea, I will be working on dozens of boxes and doing that will be a total waste of time and won't even get the exact size.

Share this post


Link to post
Share on other sites
No you make one in max to the right size then you work out the scale. Like say in max your box is 5* 5 and in your app it's say 10* 10 then you use that scale difference on all your boxes in max

Share this post


Link to post
Share on other sites

@ankhd: Althought that can solve the problem, it doesn't solve the main issue which is making a function that load .x file box into bullet physics.

 

I'm looking for a way to make a method to load the .x file box into bullet physics, I'm using D3DXComputeBoundingBox() to calculate the .x file box size but it doesn't work as expected.

Share this post


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

  • Advertisement