Game Development Reference
In-Depth Information
Assume the original bounding box is in x
min
, x
max
, y
min
, etc., and
the new bounding box will be computed into x
′
min
, x
′
max
, y
′
min
, etc. Let's
examine how we might more quickly compute x
′
min
as an example. In other
words, we wish to find the minimum value of
m
11
x + m
21
y + m
31
z,
where [x,y,z] is any of the original eight corner points. Our job is to figure
out which of these corner points would have the smallest x value after
transformation. The trick to minimizing the entire sum is to minimize
each of the three products individually. Let's look at the first product,
m
11
x. We must decide which of x
min
or x
max
to substitute for x in order
to minimize the product. Obviously, if m
11
> 0, then the smaller of the
two, x
min
, will result in the smaller product. Conversely, if m
11
< 0, then
x
max
gives smaller product. Conveniently, whichever of x
min
or x
max
we
use for computing x
′
′
max
. We
then apply this process for each of the nine elements in the matrix.
This technique is illustrated in Listing 9.4. The class
Matrix4x3
is a
4 × 3 transform matrix, which can represent any a
ne transform. (It's
a 4 × 4 matrix that acts on row vectors, where the right-most column is
assumed to be [0,0,0,1]
T
.)
min
, we use the other value for computing x
v o i d
AABB3 : : s e t T o T r a n s f o r m e d B o x (
c o n s t
AABB3 &box ,
c o n s t
M a t r i x 4 x 3
&m)
{
/ /
S t a r t
w i t h
t h e
l a s t
row
o f
t h e
m a t r i x ,
which
i s
t h e
t r a n s l a t i o n
/ /
p o r t i o n ,
i . e .
t h e
l o c a t i o n
o f
t h e
o r i g i n
a f t e r
t r a n s f o r m a t i o n .
min
=
max
=
g e t T r a n s l a t i o n (m ) ;
/ /
/ /
Examine
each
o f
t h e
9
m a t r i x
e l e m e n t s
/ /
and
compute
t h e
new AABB
/ /
i f
(m. m11
>
0 . 0 f )
{
min . x
+= m. m11
∗
box . min . x ;
max . x
+= m. m11
∗
box . max . x ;
}
e l s e
{
min . x
+= m. m11
∗
box . max . x ;
max . x
+= m. m11
∗
box . min . x ;
}
(m. m12
>
0 . 0 f )
{
i f
min . y
+= m. m12
∗
box . min . x ;
max . y
+= m. m12
∗
box . max . x ;
}
e l s e
{
min . y
+= m. m12
∗
box . max . x ;
max . y
+= m. m12
∗
box . min . x ;
}
i f
(m. m13
>
0 . 0 f )
{
min . z
+= m. m13
∗
box . min . x ;
max . z
+= m. m13
∗
box . max . x ;
}
e l s e
{
min . z
+= m. m13
∗
box . max . x ;
max . z
+= m. m13
∗
box . min . x ;
}
i f
(m. m21
>
0 . 0 f )
{
min . x
+= m. m21
∗
box . min . y ;
max . x
+= m. m21
∗
box . max . y ;
}
e l s e
{
Search WWH ::
Custom Search