Jump to content

  • Log In with Google      Sign In   
  • Create Account

Using D3DXComputeBoundingBox()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 20 December 2012 - 12:36 PM

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:


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();


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

Sponsor:

#2 MJP   Moderators   -  Reputation: 11774

Like
0Likes
Like

Posted 20 December 2012 - 02:21 PM

This is wrong:

BYTE* pData;
mesh->LockVertexBuffer( NULL, (LPVOID*)pData );

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**).

#3 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 20 December 2012 - 02:59 PM

I tried changing the code to:
BYTE *pData;
mesh->LockVertexBuffer( NULL, (void**)&pData );

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

#4 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 20 December 2012 - 04:19 PM

I even tried to create a box using D3DXCreateBox() and tried to retrieve the values, but can't get it to work:
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;


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

#5 LancerSolurus   Members   -  Reputation: 616

Like
0Likes
Like

Posted 20 December 2012 - 05:21 PM

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)...

******************************************************************************************
Youtube Channel


#6 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 21 December 2012 - 08:45 AM

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, 24 December 2012 - 10:23 PM.


#7 GuyWithBeard   Members   -  Reputation: 890

Like
0Likes
Like

Posted 21 December 2012 - 11:53 AM

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.



#8 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 21 December 2012 - 12:34 PM

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, 21 December 2012 - 05:54 PM.


#9 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 21 December 2012 - 06:02 PM

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, 21 December 2012 - 06:04 PM.


#10 GuyWithBeard   Members   -  Reputation: 890

Like
0Likes
Like

Posted 22 December 2012 - 02:15 AM

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.



#11 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 22 December 2012 - 10:54 AM

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?

#12 GuyWithBeard   Members   -  Reputation: 890

Like
0Likes
Like

Posted 23 December 2012 - 02:24 AM

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.



#13 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 23 December 2012 - 10:12 PM

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.

#14 GuyWithBeard   Members   -  Reputation: 890

Like
0Likes
Like

Posted 24 December 2012 - 04:04 AM

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, 24 December 2012 - 04:51 AM.


#15 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 24 December 2012 - 04:19 PM

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, 24 December 2012 - 04:45 PM.


#16 Alin   Members   -  Reputation: 391

Like
1Likes
Like

Posted 31 December 2012 - 01:18 PM

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



#17 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 02 January 2013 - 01:01 AM

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, 02 January 2013 - 01:02 AM.


#18 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 05 January 2013 - 10:10 PM

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.



#19 ankhd   Members   -  Reputation: 1359

Like
0Likes
Like

Posted 07 January 2013 - 06:49 AM

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.

#20 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 07 January 2013 - 04:08 PM

@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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS