Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Having trouble understanding bounding box creation and manipulation

Started by deadstar Jan 23, 2008 at 7:57 AM 10 replies 5.4k views
Original Post
deadstar
deadstar
Hi, I've spent the past few days googling and reading books, getting ready to include bounding box collision into my engine, and I'm having trouble understanding some of the details. Here's what I *think* the course of action should be: 1) Find the min and max vertex positions of all models [DONE] 2) Calculate the 8 vertices of the box corners [DONE] 3) Before detection, transform the vertices of the box into world-space coordinates The 3rd step is where I'm troubled. Say I translate and rotate my object using OpenGL's glTranslatef and glRotatef, how would I calculate the new world-space coordinates for the bounding box? The vertices in the vertex array would still be the same. I found a DirectX tutorial containing the following:
Quote:
When we need to check for collisions we transform the 8 corners of the bounding box by the world matrix. In Direct3D we can use the D3DXVec3TransformCoord(...) function e.g. // Transform the 8 corners of our object space bounding box into world space D3DXVECTOR3 worldBounds[8]; for( int i = 0; i < 8; i++ ) D3DXVec3TransformCoord( &worldBounds, &m_objectBounds, &matWorld );
What steps are the equivalent of calling D3DXVec3TransformCoord(), in OpenGL?
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
ndhb
ndhb
You write that you already have the coordinates of your bounding box in object space and then you perform some transformations (with glTranslate and glRotate). These affect the currently selected matrix stack (that is, the glModelViewMatrix). To transform the vertices into world-space, you would simply have to do throw the coordinates at the new transformation matrix (multiply the coordinates with the matrix). Unless I misunderstand what you're writing, it is that simple.
Gage64
Gage64
First, OpenGL doesn't contain any math functionality, so you'll have to use an external library (there are lots) or implement your own.

Second, if you are dealing with an AABB (axis-aligned bounding box), simply transforming the 8 vertices of the box is not enough because the result might not be axis-aligned. After transforming the vertices, you'll need to compute a new AABB from these 8 points. Note that you have to keep a copy of the original box you computed from the model's vertices and apply any transformations to it to get the world space box (in other words, don't modify the original AABB).
deadstar
deadstar
Quote:
Original post by Gage64
First, OpenGL doesn't contain any math functionality, so you'll have to use an external library (there are lots) or implement your own.

Second, if you are dealing with an AABB (axis-aligned bounding box), simply transforming the 8 vertices of the box is not enough because the result might not be axis-aligned. After transforming the vertices, you'll need to compute a new AABB from these 8 points. Note that you have to keep a copy of the original box you computed from the model's vertices and apply any transformations to it to get the world space box (in other words, don't modify the original AABB).


I will eventually be dealing with AABB, but one step at a time. I'm starting with the OBB.

So will I need to create my own matrix class for this? Is there no way to perform a glTranslate, then retrieve the new coordinates?

Would this be a worthwhile approach:

1) Create a 4x4 matrix class
2) Give each model it's own matrix
3) Perform any translation and rotation using the model's matrix instead of using the openGl functions
4) Use the matrix to transform the box's 8 vertices to world-space
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
Gage64
Gage64
Quote:
Original post by deadstar
I will eventually be dealing with AABB, but one step at a time. I'm starting with the OBB.


As far as I know OBBs are much more difficult to deal with (harder to construct, harder to test for collisions...) so starting with an AABB might be better.

Quote:
So will I need to create my own matrix class for this?


No, many people already did this for you. Have a look at the link I provided in my previous reply.

Quote:
Is there no way to perform a glTranslate, then retrieve the new coordinates?


No (at least not in a simple way). These transformations are applied to vertices you send to OpenGL. Unless your graphics card is 10 years old, these vertices will be sent to the video card and transformed there (the graphics card has special hardware to do these transformations very quickly). Sending them back to the CPU will be expensive and complicated (if it's even possible using pre-DX 10 hardware).

Quote:
Perform any translation and rotation using the model's matrix instead of using the openGl functions


The graphics card is much better at this sort of thing than the CPU (that's what it was designed for), so you should leave it to him. Letting the graphics card do it will also free the CPU to do other things (like AI and physics).
deadstar
deadstar
Quote:
Original post by Gage64
No, many people already did this for you. Have a look at the link I provided in my previous reply.


The link was helpful, but I seem to learn more if I create my own functions to do this sort of thing. If it starts to become too much, I'll use a library.

I have the book '3D Math primer for Graphics and Game Development', and I've successfully used it to make a very basic 4x4 matrix class, which can multiply a vector.

I was under the impression you had to make an OBB in order to calculate an AABB?

So, next steps:

1) Multiply each of the OBB's vertices by the world matrix
2) Everytime the model moves/rotates...?
3) Use this data to create an AABB

I just don't understand what to do if the model moves and rotates. All the data mentioned (the model's vertices, the OBB's vertices) have nothing to do with the model's X Y and Z position or rotation.
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
Gage64
Gage64
I have that book too. Chapter 12 has a section which explains AABBs, how to create them, how to transform them properly, and chapter 13 talks about how to do collision detection with them.

Quote:
I was under the impression you had to make an OBB in order to calculate an AABB?


No. The book's code (found here) shows how to create an AABB from a list of vertices.

Quote:
I just don't understand what to do if the model moves and rotates. All the data mentioned (the model's vertices, the OBB's vertices) have nothing to do with the model's X Y and Z position or rotation.


What do you mean by "has nothing to do with the model's position/rotation"? If the model moves and rotates, you have to apply the same movement and rotation to the box.
deadstar
deadstar
Right, after two *very* late nights and several reads through some Maths for Noobs books, I've finally got a working AABB class in my engine!

Symmetry Engine showing a HL2 Dropship and AABB

Thank you both so much, you've been very helpful :)

Some further questions:

1) The terminology. I've been referring to the inside bounding box as InnerAABB. One of my OpenGL books refers to it as an OBB (Object Bounding Box) since it fits snuggly with the object's position and rotation. The outer box, I'm referring to as OuterAABB.

I can't help thinking that the inside is an OBB, and the outside is an AABB, however the book '3D Maths Primer...' is telling me they are BOTH AABB's, one is 'the original' and one is the 'axis aligned' version.

2) My code could be a little messy, as well as a sloppy method of calculating and maintaining the AABB. Here's what I'm doing:

- During the model loading process, I collect the MIN and MAX
- Once the object has finished loading, I calculate the 8 vertices for the InnerAABB, then translate them to world space
- Everytime the model moves, I move the InnerABB's world-space coords with it, then recalculate the OuterABB and translate to world space

All the model's transformations are performed using OpenGL's functions, all the AABB transformation is done using a custom 4x4 matrix class.

Is this a little overkill?
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
Gage64
Gage64
Quote:
I can't help thinking that the inside is an OBB, and the outside is an AABB, however the book '3D Maths Primer...' is telling me they are BOTH AABB's, one is 'the original' and one is the 'axis aligned' version.


Where does it say that? The inner box is not aligned to the world axis, so obviously it's not an AABB.
deadstar
deadstar
Quote:

"However, computing a new AABB for the "transformed AABB" (we should perhaps call it a NNAABNNA - a "not necessarily axially aligned bounding not-necessarily box") should be..."

"So, to compute an AABB for a transformed AABB..."


The book doesn't mention the first box as being an OBB, it seems to imply they are both called AABB's. The first quote seems to acknowledge that it shouldn't really be called an AABB (hence the joke NNAABNNA) but still doesn't mention the inner box by any term other than AABB.

It was just confusing. Is the inner box an OBB?
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
Gage64
Gage64
Quote:
Is the inner box an OBB?


Yes. An AABB is a box whose sides are aligned with the world-space axes. An OBB is a box that has an arbitrary orientation. Of course, any AABB can be considered an OBB, but the opposite is not true. By these definitions, the inner box is an OBB.

The first quote simply says that if you just transform the AABB's points by a matrix, the resulting box will not necessarily be axis-aligned (and if the matrix contains shear, it might not be a box at all).
deadstar
deadstar
Excellent, I'm finally getting the hang of this.

So, the class will be called BOUNDING_BOX, the inner box will be OBB and the outer box is an AABB.

How about my method for making and handling the box? Efficient?
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.