Game Development Reference
In-Depth Information
The “size vector” s is the vector from p min to p max and contains the
width, height, and length of the box:
s = p max
p min .
We can also refer to the “radius vector” r of the box, which is half of
the size vector s , and can be interpreted as the vector from c to p max :
r = p max c = s /2.
To unambiguously define an AABB requires only two of the five vectors
p min , p max , c , s , and r . Other than the pair s and r , any pair may be
used. Some representation forms are more useful in particular situations
than others. We advise representing a bounding box by using p min and
p max , since in practice these values are needed far more frequently than s ,
c , and r . And, of course, computing any of these three vectors from p min
and p max is very fast. In C, an AABB might be represented by using a
struct like in Listing 9.1.
s t r u c t
AABB3
{
V e c t o r 3
min ;
V e c t o r 3
max ;
} ;
Listing 9.1
The most straightforward way to describe an AABB
9.4.2 Computing AABBs
Computing an AABB for a set of points is a simple process. We first reset
the minimum and maximum values to “infinity,” or what is effectively bigger
than any number we will encounter in practice. Then, we pass through the
list of points, expanding our box as necessary to contain each point.
v o i d
AABB3 : : empty ( )
{
min . x
=
min . y
=
min . z
=
FLT MAX ;
max . x
=
max . y
=
max . z
= FLT MAX ;
}
v o i d
AABB3 : : add ( c o n s t
V e c t o r 3
&p )
{
i f
( p . x < min . x )
min . x
=
p . x ;
i f
( p . x > max . x )
max . x
=
p . x ;
i f
( p . y < min . x )
min . y
=
p . y ;
i f
( p . y > max . x )
max . y
=
p . y ;
i f
( p . z < min . x )
min . z
=
p . z ;
i f
( p . z > max . x )
max . z
=
p . z ;
}
Listing 9.2
Two helpful AABB functions
 
Search WWH ::




Custom Search