• 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
schupf

Fast (!) and correct Frustum - AABB Intersection

3 posts in this topic

Hello, I needed an intersection test of the view frustum vs an axis aligned bounding box (AABB). Of course I could check all 8 box vertices against all 6 planes of the frustum, but I found a faster algorithm on this website, which uses a p-vertex and n-vertex: Link (section Geometric Approach - Testing Boxes II) To implement this algorithm I just used this code:
// Returns: INTERSECT : 0 
//          INSIDE : 1 
//          OUTSIDE : 2 
int FrustumAABBIntersect(Plane *planes, Vector &mins, Vector &maxs) { 
   int    ret = INSIDE; 
   Vector vmin, vmax; 

   for(int i = 0; i < 6; ++i) { 
      // X axis 
      if(planes[i].normal.x > 0) { 
         vmin.x = mins.x; 
         vmax.x = maxs.x; 
      } else { 
         vmin.x = maxs.x; 
         vmax.x = mins.x; 
      } 
      // Y axis 
      if(planes[i].normal.y > 0) { 
         vmin.y = mins.y; 
         vmax.y = maxs.y; 
      } else { 
         vmin.y = maxs.y; 
         vmax.y = mins.y; 
      } 
      // Z axis 
      if(planes[i].normal.z > 0) { 
         vmin.z = mins.z; 
         vmax.z = maxs.z; 
      } else { 
         vmin.z = maxs.z; 
         vmax.z = mins.z; 
      } 
      if(Vector::DotProduct(planes[i].normal, vmin) + planes[i].d > 0) 
         return OUTSIDE; 
      if(Vector::DotProduct(planes[i].normal, vmax) + planes[i].d >= 0) 
         ret = INTERSECT; 
   } 
   return ret;
} 


I assume that this algorithm is correct. But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes. (Because I don't know if I explained the problem quite well, I will give an example: Imagine you have a far plane of 1000 units and a box which goes from 900 to 1100 units on the z-axis. The x coordinates of the box are huge, so the box is placed at a huge distance on the right side of the frustum and does NOT intersect the real frustum. But the box DOES intersect the far plane, so the algorithm will deliver an intersection). My question: Is there a (relatively) simple way to modify this algorithm (or use an other algorithm) to get only the REAL intersections with the frustum (and not with the infinite planes)?
0

Share this post


Link to post
Share on other sites
Quote:
Original post by schupf
But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes.


It is a normal property of 3D planes that they extend to infinity. A plane can be seen to divide the 3D space into a positive and a negative half-space, where the positive half space is the side of the space towards which the normal of the plane points to.

Now, define a frustum to be the intersection of the negative half-spaces of 6 planes left,right,up,down, near and far. (this is how we usually define arbitrary-shaped convex polyhedrons in 3D)

The code you pasted works by checking one plane at a time, whether the AABB is in the positive half-space of the plane (if so, no intersection, AABB lies outside), or whether the AABB intersects the plane (if so, return intersection). If the AABB doesn't lie in the positive half-space of ANY of the 6 planes, then it is completely in the negative half-spaces of the planes and hence inside the frustum.

The optimization that is being done in that code is that instead of checking all the 8 corner points of the AABB, we can skip some computations by the observation that it is easy to determine which of the corner vertices lie in the "most negative and positive directions" of the plane just by looking at the plane normal.

The code looks fine to me. Perhaps the problem lies in how you extract the planes of the frustum? The code uses the assumption that the normals point outwards from the frustum volume.
0

Share this post


Link to post
Share on other sites
@clb: You did not understand my problem;)
Just forget my question, I have just found the solution (shame on me that I did not see it from the beginning). I just have to skip the early out and always loop through all 6 planes. If one plane test resulted in OUTSIDe then it is outside, else its an intersection.


Anyway, I have an another question regarding the linked paper: click
In the section "Geometric Approach - Extracting the Planes" they show how to extract the 6 frustum planes in a geometrical way, by calculating the normal vector and a point on the plane. For the right plane their formula is this:
a = (nc + right * Wnear / 2) - p
(this is visualized by this picture:

They want to calculate the vector of the right point (a point on the right plane). The term in the parentheses is clear to me: nc + right * Wnear / 2, nc is the center point of the near plane, right is a unit vector to the right, Wnear/2 the half width of the near plane. But why do they substract p?? (p = eyepoint of camera). I mean they calculate a (vector from p to point on the right plane) and nc + right * Wnear / 2 already gives what they want. Anyone knows why they subtract p?
0

Share this post


Link to post
Share on other sites
Quote:
Original post by schupf
Hello,

I needed an intersection test of the view frustum vs an axis aligned bounding box (AABB). Of course I could check all 8 box vertices against all 6 planes of the frustum, but I found a faster algorithm on this website, which uses a p-vertex and n-vertex: Link (section Geometric Approach - Testing Boxes II)

To implement this algorithm I just used this code:
*** Source Snippet Removed ***
I assume that this algorithm is correct.
But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes. (Because I don't know if I explained the problem quite well, I will give an example: Imagine you have a far plane of 1000 units and a box which goes from 900 to 1100 units on the z-axis. The x coordinates of the box are huge, so the box is placed at a huge distance on the right side of the frustum and does NOT intersect the real frustum. But the box DOES intersect the far plane, so the algorithm will deliver an intersection).

My question: Is there a (relatively) simple way to modify this algorithm (or use an other algorithm) to get only the REAL intersections with the frustum (and not with the infinite planes)?
The AABB frustum culling algorithm you describe (which is fairly standard) is conservative; it favors efficiency at the expense of the occasional false positive. It's possible to perform an exact test, but it's unlikely to be worth the extra development effort or run-time cost (under normal circumstances, at least).
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