Sign in to follow this  

Ray/Box collision: Intersection point?

This topic is 3488 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


Link to post
Share on other sites
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


Link to 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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites

This topic is 3488 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this