# Closest point on ray to AABB?

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

## Recommended Posts

I'm sorting all my transparent objects by distance from the camera so they can be drawn correctly. Using their central positions doesn't work well enough so I thought I would use their AABBs instead.

I would like to find the closest point on a ray (cast from the camera's position, along its forward vector) to each transparent object's AABB in the scene.

I know how to find the closest point on a AABB to a point but I'm not certain how to expand that to a ray.

Any ideas?

##### Share on other sites
If i understand correctly.......

What you really wanna do here is see which side of the AABB is closest to a ray thats being cast,

An AABB has 6 sides, these 6 sides can be represented by 6 planes with boundaries, you can easily find where the these planes will intersect with the ray, and by finding where these planes intersect with the ray you also get the TIME t, or the DISTANCE d (depending which way you want to look at it) that it takes for the ray to get there, Then you simply compare which value of t, or d is smallest for the 6 sides and tada! you then know which is the closest point to the ray.

Hope this helped.

##### Share on other sites
See this page , section "Distance between lines, rays, or segments and boxes (3D). "

##### Share on other sites

I'm sorting all my transparent objects by distance from the camera so they can be drawn correctly.

Try to use the distance to the near plane, not to the camera position. Often the distance to the object center is good enough, but if it isn't you can use it's bounding volume.

##### Share on other sites

See this page , section "Distance between lines, rays, or segments and boxes (3D). "

Thanks Dave. I hooked that up but it wasn't the test I was looking for as the point on the ray could be closer to an AABB that is further away at certain viewing angles. That scenario didn't occur to me until after I spent an afternoon implementing it!

[color="#1C2837"]Try to use the distance to the near plane, not to the camera position. Often the distance to the object center is good enough, but if it isn't you can use it's bounding volume.
[/quote]

The object center is currently not good enough so I would like to calculate the distance between an AABB and the near plane. I can compute the distance between the AABB's center and the plane:

 float distance = Vector3.Dot(plane.Normal, aabb.Position) - plane.D; 

How do I find the smallest distance from the AABB to the plane?

EDIT:

Is this correct?

 float r = Math.Abs(aabb.Extents.X * plane.Normal.X) + Math.Abs(aabb.Extents.Y * plane.Normal.Y) + Math.Abs(aabb.Extents.Z * plane.Normal.Z); float distance = Math.Abs(Vector3.Dot(plane.Normal, aabb.Position) - plane.D - r); 

##### Share on other sites
My idea isn't correct after testing today.

Can some please explain how I can obtain the distance between the closest AABB face and a plane? In other words the distance between the closest point on the AABB to the plane.

##### Share on other sites

Can some please explain how I can obtain the distance between the closest AABB face and a plane? In other words the distance between the closest point on the AABB to the plane.

Surely for a convex shape like a cube it'll be one of of the corners (the points on the face will never meet the plane independently of the corners) - So just do point-to-viewplane distances for all 8 corners (and choose the smallest positive distance)?

However.... Returning to the transparency sorting - do your objects overlap or inter-penetrate? If they do, you'll not make it work using neither centres nor nearest-points.

Jans.

##### Share on other sites

[color="#1C2837"]Surely for a convex shape like a cube it'll be one of of the corners (the points on the face will never meet the plane independently of the corners) - So just do point-to-viewplane distances for all 8 corners (and choose the smallest positive distance)?
[color="#1C2837"][/quote]
[color="#1C2837"]
[color="#1C2837"]I thought about testing all 8 corners but I figured there must be a more elegant solution.
[color="#1C2837"]
[color="#1C2837"]

[color="#1C2837"]Returning to the transparency sorting - do your objects overlap or inter-penetrate?
[color="#1C2837"][/quote]
[color="#1C2837"]
[color="#1c2837"]They don't and they are all convex. However, if I were to have overlaps I guess I would abandon the sorting idea altogether. Triangle sorting would be overkill and I think there is a way (pre multiplied alpha?) that doesn't require sorting of any sort.

[color="#1c2837"]EDIT:

[color="#1c2837"]Calculating the distance to the plane suffers from the same problems as the ray test in that the plane is infinite and large boxes that are further away can be closer to the plane, which is incorrect for sorting.

##### Share on other sites
Iam still going with my initial suggestion of find the value T or D from the Ray to the Walls of the AABB. The shortest one is closest to the screen.

##### Share on other sites

Iam still going with my initial suggestion of find the value T or D from the Ray to the Walls of the AABB. The shortest one is closest to the screen.

I don't understand how that will work correctly. Forgive me if am being dim but a ray cast from the camera along its forward vector can easily miss a side of the AABB if that side is (almost) parallel to the ray.

What advantage does the ray to plane distance test have over the corner to near plane distance test in this case?

##### Share on other sites

[quote name='The_Neverending_Loop' timestamp='1311968076' post='4842276']
Iam still going with my initial suggestion of find the value T or D from the Ray to the Walls of the AABB. The shortest one is closest to the screen.

I don't understand how that will work correctly. Forgive me if am being dim but a ray cast from the camera along its forward vector can easily miss a side of the AABB if that side is (almost) parallel to the ray.

What advantage does the ray to plane distance test have over the corner to near plane distance test in this case?
[/quote]

Seems like a problem of precision. (Precision of the order of how close is considered parallel, also the precision of the point of intersection is always +/- a small amount) However when thinking about an AABB (or any box), holding it in front of my face, if I get one side nearly parallel to my forward gaze, there seems to be another side which is orthogonal and shares an edge. Additionally, however, the (almost) parallel side is hardly seen (has little influence on the image seen). So I'm not sure if the (almost) parallel side effect would cause a problem (and could be "fixed" by using higher precision?)

So to sort transparent objects you want to know which are closer and do a front to back rendering effect. So you want to sort sides it seems, rather than objects. (What happens if two objects take up some of the same space? An object partially inside another object?) So you want to deal with the closest side, followed by the next closest side (which isn't guaranteed to be from the same object). I guess that's why it seems like sorting by closest side gives an advantage.

But I've never done transparency so I'm just taking a stab in the dark ^^ Goodluck!

• 9
• 10
• 11
• 12
• 11