• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gbMike

Bounding Box Calculations

11 posts in this topic

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

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
[quote]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.
0

Share this post


Link to post
Share on other sites
[quote name='gbMike' timestamp='1331132997' post='4920076']
...
Is there a way to calculate coordinates in world space, based on the current modelview matrix? ...
[/quote]
[quote name='dpadam450' timestamp='1331139396' post='4920115']
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.
[/quote]
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
[b]V[/b][sup]-1[/sup] * ( [b]V[/b] * [b]M [/b]) = [b]M[/b]
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.
0

Share this post


Link to post
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.
[/quote]
[quote name='dpadam450' timestamp='1331139396' post='4920115']
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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='gbMike' timestamp='1331147201' post='4920154']
[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.
[/quote]
[quote name='dpadam450' timestamp='1331139396' post='4920115']
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.
[/quote]

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

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
[quote name='gbMike' timestamp='1331147201' post='4920154']
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]
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.

[quote name='mhagain' timestamp='1331150761' post='4920179']
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.
[/quote]
A sensible approach, and it'll help simplify collision detection as checking for intersection with a sphere is trivial.
0

Share this post


Link to post
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.
0

Share this post


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

[quote]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?
0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0