• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Bounding Box Creation

## Recommended Posts

I''ve worked out some stuff with my "3d engine", and now the monster can turn thru 360 degrees, and can move in the direction he is facing. YAHOOOOO! However, I have come upon another problem. Collision detection. No! No! Don''t run away! All I want to know is how to use DirectX 8 to create a simple bounding box for a mesh (so as the monster doesn''t keep walking through a nearby power plant). I''ve looked around the documentation, and have seen functions like D3DXComputeBoundingBox etc, but I can''t figure out how exactly to use them. Can someone give me a couple of lines of source just to put me on the right track? Thanks in advance --------------- I finally got it all together... ...and then forgot where I put it.

##### Share on other sites
Okay, i had this idea last week. Have you ever used RECTs in 2d? I got an idea like that. Here''s my class. I''m making this up right on the spot so if its not totally correct sorry. What it does is alows you to set the values for the top left back corner and bottom right front corner. It also can do tests with points if they are in the box and returns either of the defines at the top.

  #define IS_COLLISION 1 //define testing results for #define NO_COLLISION 2 //collisions. Return valuesclass CCollisionBox3D{public: void SetTopCorner(float x, float y, float z); void SetBottomCorner(float x, float y, float z); int TestCollisions(CollisionBox3D OtherBox);public: float top_x, top_y, top_x; //for the top corner of thebox float bot_x, bot_y, bot_z; //bottom corner};void CCollisionBox3D::SetTopCorner(float x, float y, float z){ top_x=x; top_y=y; top_z=z;}void CCollisionBox3D::SetBottomCorner(float x, float y, float z){ bot_x=x; bot_y=y; bot_z=z;}int CCollisionBox3D::TestCollisions(float x, float y, float z){ if(x > top_x && x < bot_x && y > top_y && y < bot_y && z>top_z && z < bot_z ) { return(IS_COLLISION); } else return(NO_COLLISION); return(NO_COLLISION);}

Hmmm, I think i might even find this useful

##### Share on other sites
Okay, i had this idea last week. Have you ever used RECTs in 2d? I got an idea like that. Here''s my class. I''m making this up right on the spot so if its not totally correct sorry. What it does is alows you to set the values for the top left back corner and bottom right front corner. It also can do tests with points if they are in the box and returns either of the defines at the top.

  #define IS_COLLISION 3#define NO_COLLISION 2 class CCollisionBox3D{public: void SetTopCorner(float x, float y, float z); void SetBottomCorner(float x, float y, float z); int TestCollisions(CollisionBox3D OtherBox);public: float top_x, top_y, top_x; //for the top corner of thebox float bot_x, bot_y, bot_z; //bottom corner};void CCollisionBox3D::SetTopCorner(float x, float y, float z){ top_x=x; top_y=y; top_z=z;}void CCollisionBox3D::SetBottomCorner(float x, float y, float z){ bot_x=x; bot_y=y; bot_z=z;}int CCollisionBox3D::TestCollisions(float x, float y, float z){ if(x > top_x && x < bot_x && y > top_y && y < bot_y && z>top_z && z < bot_z ) { return(IS_COLLISION); } else return(NO_COLLISION); return(NO_COLLISION);}

Hmmm, I think i might even find this useful

##### Share on other sites
Thanx very much for taking the time to reply, but I already have a class almost identical to yours: CD3DRect

  class CD3DRect{public: CD3DRect(); CD3DRect(FLOAT,FLOAT,FLOAT,FLOAT,FLOAT,FLOAT); FLOAT fMinX, fMinY, fMinZ; FLOAT fMaxX, fMaxY, fMaxZ;};

I know, i know...public member data...but for a small class like that i think easy and fast access is more important.

Anyway, what I REALLY need to know is how to compute the bounding box of a 3d mesh in directX 8. Anyone?

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
Doesn''t D3DXComputeBoundingBox solve the problem?

##### Share on other sites
Yeah i tried that, but i couldn''t figure out how to get the vertex buffer to pass to it (i''m using a mesh). I got all confused... And for some strange reason it accepts a BYTE**

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
Are AABBs good enough? If so, then just find the maximum and minimum x, y, and z values.

  float xmax = object.origin.x;for(int i = 0; i < NUM_VERTS; i++){ if(object.verts[i].x > xmax) { xmax = object.verts[i].x; }}//Do same for y and z

OBBS are a little more complicated...

##### Share on other sites
Well, erm, thanx again for the help...I could do that if I had the mesh''s vertices. I don''t have them. I could also use D3DXComputeBoundingBox - IF I had the vertices. Anyway, if anyone out there knows direct3d 8 and wants to help me...it would be appreciated.

P.S. My question is API-specific (Direct3D 8.0 to be precise)

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
Use ID3DXMesh->GetVertexBuffer() and then lock the buffer. Pass the returned pointer to D3DXComputeBoundingBox after casting it to PVOID.

##### Share on other sites
d00d thanx! That helps alot!

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
Sorry to bump this post again but...

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
Above post was a mistake. I meant to add my new question in the same post, but i pressed enter to make a new line and it posted it! Sorry ''bout that...

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
OK, now for the question...

Firstly, if I want to get the current bounding box for a mesh, will I have to do this each frame? (Sounds like the vertex locking and mesh box computation would slow it down considerably).

Secondly, in order to get the right orientation for the bounding box, should I multiply it by the model''s world matrix? I''m kinda confused here, as I''m plodding blindly into the world of 3d without any solid knowledge of the maths involved - I''m hiding behind D3D API

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
No, you don''t need to calculate the bounding box each frame. The bounding box should be calculated once. At each frame you''ll need to transform your bounding box with the world matrix in order to get the right orientation and position of the box. An alternative way is to mutiply the other object by the inverse of the world matrix of your mesh to get that object transformed into the world space of your mesh so that you can use the original bounding box without transformation.

• ### Forum Statistics

• Total Topics
627744
• Total Posts
2978901

• 10
• 10
• 21
• 14
• 14