**0**

# Bounding Box Calculations

###
#1
Members - Reputation: **152**

Posted 07 March 2012 - 09:09 AM

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

###
#2
Members - Reputation: **130**

Posted 07 March 2012 - 10:40 AM

###
#3
Members - Reputation: **842**

Posted 07 March 2012 - 10:56 AM

He's not.Why are you using OpenGL functions to transform a bounding box?

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.

###
#4
Crossbones+ - Reputation: **3779**

Posted 07 March 2012 - 11:15 AM

...

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

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

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

^{-1}* (

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

###
#5
Members - Reputation: **152**

Posted 07 March 2012 - 01:06 PM

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.

###
#6
Crossbones+ - Reputation: **3779**

Posted 07 March 2012 - 01:24 PM

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.

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.

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.

###
#7
Crossbones+ - Reputation: **7462**

Posted 07 March 2012 - 02:06 PM

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

###
#8
Members - Reputation: **130**

Posted 07 March 2012 - 03:18 PM

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

A sensible approach, and it'll help simplify collision detection as checking for intersection with a sphere is trivial.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.

###
#9
Members - Reputation: **152**

Posted 07 March 2012 - 03:57 PM

###
#10
Members - Reputation: **842**

Posted 07 March 2012 - 06:46 PM

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?would result in a rotated bbox

###
#11
Members - Reputation: **152**

Posted 08 March 2012 - 01:28 PM

In this case, I hadn't even considered spherical collision boxes, but they work out perfectly. (Most objects with collision, are more or less circular)

###
#12
Members - Reputation: **126**

Posted 10 March 2012 - 06:00 PM

If you have more constrained transformations you can simplify this somewhat. Constraining rotations can do a lot.

If you represent your AABBs as a point and a vector of half lengths, you only need to do 2 matrix multiplications instead of 8, but remember that you still need to decompose the half length vector and calculate a new one after transformation.