Bounding Box Calculations

This topic is 2474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I feel like I might be missing something obvious, but I can't quite figure this out. I'm trying to calculate a bounding box, for a 3d object, after it has been rotated. I have all the vertex positions, but since I use glTranslatef, glScalef, & glRotatef and then draw, the points locations that I have are relative to the object at (0,0,0) with no rotation. Is there a way to calculate coordinates in world space, based on the current modelview matrix? Or is there a better way to do bounding box calculations?

Thanks in advance. Sorry if I'm missing something obvious. I'm pretty new to working in 3D, so go easy on me.

Share on other sites
Why are you using OpenGL functions to transform a bounding box? The way I generally approach it is to give the box its own coordinates, and apply transformations to those manually using vectors, Matrices and quaternions. The OpenGL functions will only apply to a matrix in the OpenGL state, which even if you could access it would be bad practise to use for this kind of application.

Share on other sites
Why are you using OpenGL functions to transform a bounding box?[/quote]
He's not.

Right after setting up your transforms, you can call glGetDoublev(GL_MODELVIEW_MATRIX, double[16] array). With that matrix you will have to manually loop through your Box vertices and do matrix multiplication.

Share on other sites

...
Is there a way to calculate coordinates in world space, based on the current modelview matrix? ...

Right after setting up your transforms, you can call glGetDoublev(GL_MODELVIEW_MATRIX, double[16] array). With that matrix you will have to manually loop through your Box vertices and do matrix multiplication.

Please notice that using the MODELVIEW matrix means that the bbox is transformed into view space but not into world space. There is nothing wrong with this in general, because collision detection can be done in any space as long as all participating bounding volumes (or whatever is used) are given in the same space. However, bounding volumes of static objects are usually given in world space, and transforming them into view space may be needless effort.

That said, one needs to determine the VIEW portion, invert it, and multiply the inverse on the left of the MODELVIEW matrix, so that
V[sup]-1[/sup] * ( V * M ) = M
results in the model matrix alone. This can be done by extracting the MODELVIEW matrix once per frame just after the VIEW portion was build and before the first model transformation is multiplied onto it.

However, inversion still needs to be done. IMHO (and those of sjaakiejj if I interpret his post correctly) it is definitely better to roll your own matrix / vector computation for such things instead of using OpenGL's matrix stack.

Share on other sites

The way I generally approach it is to give the box its own coordinates, and apply transformations to those manually using vectors, Matrices and quaternions.

Right after setting up your transforms, you can call glGetDoublev(GL_MODELVIEW_MATRIX, double[16] array). With that matrix you will have to manually loop through your Box vertices and do matrix multiplication.

I had considered this, but if I were to generate the bbox, and then apply the transformation/rotation/scale to it manually, a rotated model, would result in a rotated bbox. I was under the impression that this was bad practice. Unless I am mistaken.

Alternatively, I could use the apply the matrix to the points in the model, and then calculate a bounding box, but since applying rotation requires trig, and it would be run on every single vertex on every model, each frame, I imagined it would absorb ridiculous resources. Again though, I could be mistaken here as well.

Share on other sites

[quote name='sjaakiejj' timestamp='1331138404' post='4920100']
The way I generally approach it is to give the box its own coordinates, and apply transformations to those manually using vectors, Matrices and quaternions.

Right after setting up your transforms, you can call glGetDoublev(GL_MODELVIEW_MATRIX, double[16] array). With that matrix you will have to manually loop through your Box vertices and do matrix multiplication.

I had considered this, but if I were to generate the bbox, and then apply the transformation/rotation/scale to it manually, a rotated model, would result in a rotated bbox. I was under the impression that this was bad practice. Unless I am mistaken.

Alternatively, I could use the apply the matrix to the points in the model, and then calculate a bounding box, but since applying rotation requires trig, and it would be run on every single vertex on every model, each frame, I imagined it would absorb ridiculous resources. Again though, I could be mistaken here as well.
[/quote]
Besides the fact that using the MODELVIEW matrix is wrong if you want to go to world space (see my post above) ... it depends on which kind of bbox you want. A so-called axis aligned bounding box (AABB) is, as the name says, axis aligned, usually with the world axes. This kind of bbox undergoes no rotation. Instead it has to be recomputed by finding the maxima and minima after scaling and rotation has been done to the model. On the other hand, the object bounding box (OBB) will be scaled and rotated like the model. Both are ways are valid.

Trigonometry is required just when computing the transformation matrix in the case that the orientation is given in angles. After that, rotation is part of a matrix / vector product which involves no trigonometry.

There are other kinds of bounding volumes that may serve your needs as well. E.g. a cylindrical bounding volume may be used if the model will mainly be rotated around one axis only (e.g. character avatars). Or you may want to approximate an AABB by building an axis-aligned box from an OBB.

Share on other sites
A bounding sphere may be an option for you here. It will certainly be easier to calculate - just position it's center at the object's position and calculate the radius from the object size and largest scale factor, done - no need to bother with rotations. They're also faster to test against the frustum, but the tradeoff is that a sphere may not be an optimal shape for all objects.

Share on other sites

I had considered this, but if I were to generate the bbox, and then apply the transformation/rotation/scale to it manually, a rotated model, would result in a rotated bbox. I was under the impression that this was bad practice. Unless I am mistaken.

Alternatively, I could use the apply the matrix to the points in the model, and then calculate a bounding box, but since applying rotation requires trig, and it would be run on every single vertex on every model, each frame, I imagined it would absorb ridiculous resources. Again though, I could be mistaken here as well.

Depends on how you build it. Either use AABBs like haegarr mentioned, or if you want rotated bounding boxes (probably mostly used in hitboxes) I would simply have a class that contains the model and its orientation, location and scale, and use that to set the state of both the GL objects and the Bounding box. That way you have logically seperated the two very different types of functionality, and you ensure that you're always running your calculations in the correct space.

A bounding sphere may be an option for you here. It will certainly be easier to calculate - just position it's center at the object's position and calculate the radius from the object size and largest scale factor, done - no need to bother with rotations. They're also faster to test against the frustum, but the tradeoff is that a sphere may not be an optimal shape for all objects.

A sensible approach, and it'll help simplify collision detection as checking for intersection with a sphere is trivial.

Share on other sites
Thanks everyone for your replies. I've got a pretty good idea what my options are now, so I can start testing them out. I'll probably try out spherical collision boxes for now. Easier math, and circumvents the rotation issue completely. This has been a huge help.

Share on other sites
Yea most likely you will not want the view put in their so you just want the model matrix.

would result in a rotated bbox[/quote]
Right, what exactly do you want? If you don't want a rotated box then what else would you want? You could take your AABB and simply translate it instead of rotate it, but if your AABB is rectangular, then you cant just translate it. It really depends what you want the AABB for. Physics or Frustum culling?

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633767
• Total Posts
3013739
×