**0**

# Closest point on ray to AABB?

###
#1
Members - Reputation: **230**

Posted 27 July 2011 - 05:33 PM

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?

###
#2
Members - Reputation: **629**

Posted 27 July 2011 - 07:52 PM

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.

###
#4
Members - Reputation: **563**

Posted 28 July 2011 - 08:19 AM

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.

###
#5
Members - Reputation: **230**

Posted 28 July 2011 - 07:20 PM

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!

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.

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);

###
#7
Members - Reputation: **251**

Posted 29 July 2011 - 06:47 AM

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.

###
#8
Members - Reputation: **230**

Posted 29 July 2011 - 07:07 AM

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)?

I thought about testing all 8 corners but I figured there must be a more elegant solution.

Returning to the transparency sorting - do your objects overlap or inter-penetrate?

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.

EDIT:

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.

###
#10
Members - Reputation: **230**

Posted 30 July 2011 - 04:38 PM

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?

###
#11
Members - Reputation: **100**

Posted 03 August 2011 - 11:24 AM

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?

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!