Graphics Reference
In-Depth Information
coordinate is shown here. The other coordinates can be solved for in a similar
manner.
P
=
C
+
x u 0 +
y u 1 +
z u 2
(original expression)
P
C
=
x u 0 +
y u 1 +
z u 2
(moving C to left side)
( P
C )
·
u 0 =
( x u 0 +
y u 1 +
z u 2 )
·
u 0
(taking dot product with u 0 on both sides)
( P
C )
·
u 0 =
x ( u 0 ·
u 0 )
+
y ( u 1 ·
u 0 )
+
z ( u 2 ·
u 0 )
(expanding dot product expression)
( P
C )
·
u 0 =
x
(simplifying using u 0 ·
u 0 =
1,
u 1 ·
u 0 =
u 2 ·
u 0 =
0 )
The full set of OBB coordinates are thus given by x
=
( P
C )
·
u 0 , y
=
( P
C )
·
u 1 ,
and z
u 2 .
To compute the point R on (or in) B closest to P , the same approach as used for an
AABB can be applied by expressing P in the OBB coordinate system as Q , clamping
Q to the extents e 0 , e 1 , and e 2 , and reexpressing Q in world coordinates. The code for
this follows.
=
( P
C )
·
// Given point p, return point q on (or in) OBB b, closest to p
void ClosestPtPointOBB(Point p, OBB b, Point &q)
{
Vectord=p-b.c;
// Start result at center of box; make steps from there
q = b.c;
// For each OBB axis...
for(inti=0;i<3;i++) {
// ...project d onto that axis to get the distance
// along the axis of d from the box center
float dist = Dot(d, b.u[i]);
// If distance farther than the box extents, clamp to the box
if (dist > b.e[i]) dist = b.e[i];
if (dist < -b.e[i]) dist = -b.e[i];
// Step that distance along the axis to get world coordinate
q += dist * b.u[i];
}
}
Mathematically, the described method is equivalent to transforming the point P
into the local coordinate system of the OBB, computing the point on the OBB (now
effectively an AABB) closest to the transformed point, and transforming the resulting
point back into world coordinates.
 
Search WWH ::




Custom Search