Closest point on ray to AABB?

Started by
9 comments, last by s9ghost 12 years, 8 months ago
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?
Advertisement
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.
See this page , section "Distance between lines, rays, or segments and boxes (3D). "

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.

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

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.

[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.
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.

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?

This topic is closed to new replies.

Advertisement