Ray Box intersection

将一个Box看作是三对(六个)平面围成的一个盒子。 对于每对平面, 总有一个面与射线起始点较近, 另
一个较远。 射线与较近平面的距离记为Tnear, 射线与较远平面的距离记为Tfar, 因此有三对Tnear, Tfar。
如果最大的Tnear大于最小的Tfar, 则没有相交;反之相交。
Pseudocode :
set Tnear = – infinity, Tfar = infinity

For each pair of planes P associated with X, Y, and Z do:
(example using X planes)
if direction Xd = 0 then the ray is parallel to the X planes, so
if origin Xo is not between the slabs ( Xo Xh) then return false
else, if the ray is not parallel to the plane then
compute the intersection distance of the planes
T1 = (Xl – Xo) / Xd
T2 = (Xh – Xo) / Xd
If T1 > T2 swap (T1, T2) /* since T1 intersection with near plane */
If T1 > Tnear set Tnear =T1 /* want largest Tnear */
If T2 Tfar box is missed so return false
If Tfar < 0 box is behind ray return false end


About seckcoder

People behind the pixels. View all posts by seckcoder

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: