Jump to content

  • Log In with Google      Sign In   
  • Create Account


Bounding Box Calculations


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
11 replies to this topic

#1 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

Posted 07 March 2012 - 09:09 AM

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

Sponsor:

#2 sjaakiejj   Members   -  Reputation: 130

Like
0Likes
Like

Posted 07 March 2012 - 10:40 AM

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.

#3 dpadam450   Members   -  Reputation: 862

Like
0Likes
Like

Posted 07 March 2012 - 10:56 AM

Why are you using OpenGL functions to transform a bounding box?

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.

#4 haegarr   Crossbones+   -  Reputation: 3971

Like
0Likes
Like

Posted 07 March 2012 - 11:15 AM

...
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-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 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

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 haegarr   Crossbones+   -  Reputation: 3971

Like
0Likes
Like

Posted 07 March 2012 - 01:24 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.

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.

#7 mhagain   Crossbones+   -  Reputation: 7609

Like
0Likes
Like

Posted 07 March 2012 - 02:06 PM

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.

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 sjaakiejj   Members   -  Reputation: 130

Like
0Likes
Like

Posted 07 March 2012 - 03:18 PM

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.

#9 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

Posted 07 March 2012 - 03:57 PM

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.

#10 dpadam450   Members   -  Reputation: 862

Like
0Likes
Like

Posted 07 March 2012 - 06:46 PM

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

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?

#11 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

Posted 08 March 2012 - 01:28 PM

I was setting it up for physics, specifically collisions. Most 3D engines I've worked with, (Unreal/3Ds Max) seem to use AABB, in addition to the collision code examples I had been referencing. I could have set it up with OBB, but since that seemed different from every other example I had seen, I figured I might be missing something. Particularly since calculating intersections between two AABB is considerably easier than two OBB, at least as far as I know.

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 elanthis   Members   -  Reputation: 126

Like
0Likes
Like

Posted 10 March 2012 - 06:00 PM

To transform an AABB the simple way, there's only a few simple steps. First of course generate the untransformed AABB from the model's untransformed vertices by calculating the max/min values for each axis. Then, for any transformation matrix you wish to apply to the model, you will apply that matrix to the eight corners of the AABB, and find the new max/min values for each axis.

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.




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