Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ray Frustum intersection

Started by B_old Feb 28, 2007 at 7:44 AM 9 replies 7.6k views
Original Post
B_old
B_old
Hi, I would like to know whether a ray intersects a frustum. I know how to find a ray-plane intersection. But I can't simply test all 6 planes of the frustum, can I? I believe that would give me too many intersections where there actually are none.
Gagyi
Gagyi
Think about it, you need 4 triangle-ray intersections. (Wich are pretty easy, google it or ask on the maths forum.)
-----------------------------------"After you finish the first 90% of a project, you have to finish the other 90%." - Michael Abrashstickman.hu <=my game (please tell me your opinion about it)
Dragon_Strike
Dragon_Strike
u just have to test 3 planes at the most... u can determine which planes to test against using the rays direction vector...

when u have the right planes u test if there is an intersection...

if there is an intersection u check if the intersection falls within the face of the plane...
superpig
superpig
Quote:
Original post by Dragon_Strike
u just have to test 3 planes at the most... u can determine which planes to test against using the rays direction vector...


Careful. If you're doing that then you need to know whether the origin of the ray is inside or outside the frustum.
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
B_old
B_old
Quote:
Original post by Dragon_Strike
u just have to test 3 planes at the most... u can determine which planes to test against using the rays direction vector...

when u have the right planes u test if there is an intersection...

if there is an intersection u check if the intersection falls within the face of the plane...


If you have the time, a more detailed explanation would be very welcome.

I tried this:
Ray3 tmp = ray;tmp.m_dir.normalize();float dist;for (int i = 0; i < 6; ++i)	if (tmp.intersect(m_planes, dist))	{		if (dist > 0.0f) tmp.m_org += tmp.m_dir * dist;		else			 return intersect_out;	}	else return intersect_out;return intersect_in;


[Edited by - B_old on February 28, 2007 9:24:54 AM]
Zakwayda
Zakwayda
Quote:
Original post by B_old
Hi,

I would like to know whether a ray intersects a frustum. I know how to find a ray-plane intersection. But I can't simply test all 6 planes of the frustum, can I? I believe that would give me too many intersections where there actually are none.
If you only need to know if the ray intersects the frustum (not where), you could use the separating axis theorem, which I think would almost certainly be faster than performing multiple ray vs. quad intersections.

Out of curiosity, what is the test for?
B_old
B_old
I only need to know if the ray intersects, not where.

I thought I could cull objects that don't cast a visible shadow that way, but I was wrong.
Christer Ericson
Christer Ericson
Quote:
Original post by jyk
If you only need to know if the ray intersects the frustum (not where), you could use the separating axis theorem, which I think would almost certainly be faster than performing multiple ray vs. quad intersections.
I don't think the separating axis test (not theorem) is going to be the fastest approach (a frustum has 5 different face axes and 6 edge axes).

Instead the best approach is probably to treat the frustum as the intersection volume of 6 halfspaces and compute t_enter and t_exit for the six planes and see if t_enter < t_exit in which case there is an intersection (and otherwise not). This is just 6 ray-vs-plane tests, which can be performed together using SIMD for additional speed.
Zakwayda
Zakwayda
Quote:
Original post by Christer Ericson
...the separating axis test (not theorem)...
Hm. A quick Google search for the phrase 'use the separating axis theorem' seems to indicate that its use in this context is not uncommon (the search returns, among other things, some scholarly papers and some online posts by professional developers). I take it you consider this usage to be strictly incorrect?
Quote:
Instead the best approach is probably to treat the frustum as the intersection volume of 6 halfspaces and compute t_enter and t_exit for the six planes and see if t_enter < t_exit in which case there is an intersection (and otherwise not). This is just 6 ray-vs-plane tests, which can be performed together using SIMD for additional speed.
My thought was that the SAT might be faster for this purpose due to the lack of divisions and the efficiency of the plane tests (which, depending on the nature of the input data, could very well cull many if not most rays without any need for the six cross-product tests).

However, you practically 'wrote the book' on collision detection (literally!), so if you say clipping would be faster then I believe it :)
Christer Ericson
Christer Ericson
Quote:
Original post by jyk
Quote:
Original post by Christer Ericson
...the separating axis test (not theorem)...
Hm. A quick Google search for the phrase 'use the separating axis theorem' seems to indicate that its use in this context is not uncommon (the search returns, among other things, some scholarly papers and some online posts by professional developers). I take it you consider this usage to be strictly incorrect?
Yes. Here's something I wrote elsewhere on this topic:

I'm of the opinion that there is no such thing as a "separating-axis theorem."

A lot of papers talk about the "separating-axis theorem [X]" where X is a reference to (typically) the OBBTree paper by Gottschalk, Lin, and Manocha, but that paper doesn't ever talk about any separating-axis theorem! Indeed, I can't even locate a paper that attempts to formally state a separating-axis theorem! (There was a tech report by Gottschalk by that name but it was never published/completed. I do have an early draft of it.)

Furthermore, obviously one could easily state (and prove) a "separating-axis theorem" if one wanted, but I find this silly as such a theorem trivially follows from the "separating hyperplane theorem" which is a KNOWN theorem that's _actually_ proved, used, and referred to in the theory of convex sets. See e.g. section 2.5.1 of Boyd and Vandenberghe's Convex Optimization. Downloadable from here: http://www.stanford.edu/~boyd/cvxbook/

I.e. my objection is that talking about the "separating-axis theorem" is a bit like, err, referring to addition as the "addition theorem."

The reason you see references to people using the term "separating-axis theorem" is because they've seen other people use it. Somewhat annoyingly the term "separating-axis theorem" now even shows up on Wikipedia with its own entry

http://en.wikipedia.org/wiki/Separating_axis_theorem

but again without any references to an actual theorem being proved anywhere, I might point out.

This might seem like nitpicking, but language (and terminology) is important.


Quote:
Quote:
Instead the best approach is probably to treat the frustum as the intersection volume of 6 halfspaces and compute t_enter and t_exit for the six planes and see if t_enter < t_exit in which case there is an intersection (and otherwise not). This is just 6 ray-vs-plane tests, which can be performed together using SIMD for additional speed.
My thought was that the SAT might be faster for this purpose due to the lack of divisions and the efficiency of the plane tests (which, depending on the nature of the input data, could very well cull many if not most rays without any need for the six cross-product tests).
I used the fuzzy terms "I think" and "probably" deliberately, as these comparisons depend on so many things (cost of individual operations, amount of data read, whether data is cached, branches and branch prediction, etc). It's really just an educated guess from my point; I could be wrong.

Also, note that because we don't need the intersection point, there's no need to perform the divisions. If they're expensive, you can get rid of them by tracking rational instead of "real" numbers, comparing values by cross-multiplying numerators/denominators.

Zakwayda
Zakwayda
Quote:
Original post by Christer Ericson
I'm of the opinion that there is no such thing as a "separating-axis theorem."

A lot of papers talk about the "separating-axis theorem [X]" where X is a reference to (typically) the OBBTree paper by Gottschalk, Lin, and Manocha, but that paper doesn't ever talk about any separating-axis theorem! Indeed, I can't even locate a paper that attempts to formally state a separating-axis theorem! (There was a tech report by Gottschalk by that name but it was never published/completed. I do have an early draft of it.)

Furthermore, obviously one could easily state (and prove) a "separating-axis theorem" if one wanted, but I find this silly as such a theorem trivially follows from the "separating hyperplane theorem" which is a KNOWN theorem that's _actually_ proved, used, and referred to in the theory of convex sets. See e.g. section 2.5.1 of Boyd and Vandenberghe's Convex Optimization. Downloadable from here: http://www.stanford.edu/~boyd/cvxbook/

I.e. my objection is that talking about the "separating-axis theorem" is a bit like, err, referring to addition as the "addition theorem."

The reason you see references to people using the term "separating-axis theorem" is because they've seen other people use it. Somewhat annoyingly the term "separating-axis theorem" now even shows up on Wikipedia with its own entry

http://en.wikipedia.org/wiki/Separating_axis_theorem

but again without any references to an actual theorem being proved anywhere, I might point out.
Thanks for the explanation - I'll keep those points in mind :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.