Jump to content
• Advertisement

# Ray/Box collision: Intersection point?

This topic is 3800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello, I am working on several raytracing algorithms for lightmap computations. I implemented intersections at vertex-level for shadow casting without problems. But this process is very expensive, so I want to test the intersections at bounding-box and bounding-sphere level to avoid the vertex-level in each case. I implemented a ray/bounding sphere intersection algorithm so far, but I have a problem with the ray/bounding box algorithm. I read several articles with pseudocode and finally wrote this function (Pascal):
function v3DRayBoxIntersect(const ARayPos, ARayDir, AMin, AMax: TD3DXVector3; var ADistance: Single): Boolean;
var FInvDirX, FInvDirY, FInvDirZ, FMin, FMax, FMinY, FMaxY, FMinZ, FMaxZ: Single;
begin
FInvDirX:= 1 / ARayDir.x;
if FInvDirX >= 0 then
begin
FMin:= (AMin.x - ARayPos.x) * FInvDirX;
FMax:= (AMax.x - ARayPos.x) * FInvDirX;
end else
begin
FMin:= (AMax.x - ARayPos.x) * FInvDirX;
FMax:= (AMin.x - ARayPos.x) * FInvDirX;
end;

FInvDirY:= 1 / ARayDir.y;
if FInvDirY >= 0 then
begin
FMinY:= (AMin.y - ARayPos.y) * FInvDirY;
FMaxY:= (AMax.y - ARayPos.y) * FInvDirY;
end else
begin
FMinY:= (AMax.y - ARayPos.y) * FInvDirY;
FMaxY:= (AMin.y - ARayPos.y) * FInvDirY;
end;

if (FMin > FMaxY) or (FMinY > FMax) then
begin
Result:= False;
Exit;
end;

if FMinY > FMin then FMin:= FMinY;
if FMaxY > FMax then FMax:= FMaxY;

FInvDirZ:= 1 / ARayDir.z;
if FInvDirZ >= 0 then
begin
FMinZ:= (AMin.z - ARayPos.z) * FInvDirZ;
FMaxZ:= (AMax.z - ARayPos.z) * FInvDirZ;
end else
begin
FMinZ:= (AMax.z - ARayPos.z) * FInvDirZ;
FMaxZ:= (AMin.z - ARayPos.z) * FInvDirZ;
end;

if (FMin > FMaxZ) or (FMinZ > FMax) then
begin
Result:= False;
Exit;
end;
Result:= True;
end;


My problem is: How can I find the intersection point between the ray and the box? I need to know this in order to retrieve the distance between the intersection point and the light source. I hope you can help me... Thanks!

#### Share this post

##### Share on other sites
Advertisement
If you get the depth returned by whatever collision function you use, you can get the intersection point by scaling your initial ray's direction by the depth, and adding that to the origin of your ray.

#### Share this post

##### Share on other sites
Yes, I understand that ... the problem is that I don't know, from the function of the previous post, how to extract the T scalar from the equation v0 + T.v1 for the collision with the Box...

#### Share this post

##### Share on other sites
check out this paper:
Timothy L. Kay and James T. Kajiya. Ray tracing complex scenes. In
Proc. SIGGRAPH, pages 269–278, 1986.

It's called the Slabs method and the algorithm returns the intersection point among other interesting stuff. I have a GLSL implementation somewhere.

#### Share this post

##### Share on other sites
Thanks, I found it, but I have to pay (and I can't) :(
I'm completely lost with this!

#### Share this post

##### Share on other sites
Wow Thanks! I'll get into it right now :)

#### Share this post

##### Share on other sites
And this is my GLSL implementation:

bool IntersectBox(vec3 orig, vec3 dir, vec3 box_max, vec3 box_min){   vec3 tmin = (box_min - orig)/dir;   vec3 tmax = (box_max - orig)/dir;      vec3 real_min = min(tmin, tmax);   vec3 real_max = max(tmin, tmax);      float minmax = min( min(real_max.x, real_max.y), real_max.z);   float maxmin = max( max(real_min.x, real_min.y), real_min.z);   return (minmax >= maxmin);}

it returns true or false, but i believe the T(min/max) is what you're looking for.

#### Share this post

##### Share on other sites
Thanks for the code!

But I don't understand how to get my T. You said "but i believe the T(min/max) is what you're looking for". So I understand that I need to work with tMin and/or tMax.
Should I do T = tMax - tMin?

#### Share this post

##### Share on other sites
Nope, pretty sure you want t = maxmin to get the distance to the nearest intersection with the box.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19
• Advertisement

• 11
• 14
• 9
• 10
• 12
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009830
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!