# Ray Plane Intersection

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

## Recommended Posts

Ray plane intersection is a hard thing that I've seen people struggle with, and I've struggled with it too. When I look up guides on how to have ray plane intersection with a plane given 4 coordinates, I had no success. I only find guides that let you do collision with infinite planes and a lot of complicated math, which I'm not familiar with.

In this guide, I'll be explaining the basics of ray intersection with a plane of 4 coordinates. Its not that hard once you simplify everything down to its simplest form.

# Ray-Plane Intersection

## And that's it! Feel free to ask questions.

##### Share on other sites
Firstly, this is a huge misuse of the word 'plane' which refers to an 'infinitely' large flat surface, so it cannot be split into 2 triangles, it cannot be split into 2 million triangles. There is no such thing as a finite plane, nor an infinite plane; all planes are infinite. Intersections against a plane of any convex shape (not just a ray) due to the infinitely large nature are very trivial too, there are literally no 'edge' cases to deal with.

Ignoring that;

When doing a ray-triangle intersection we would not normally work with angles for numerical stability and speed.

The usual method is to compute barycentric coordinates (can still suffer), or to simply tast the ray-plane intersection against edge normals treating the triangle as an intersection of 3 half-spaces with a plane in which case there is really not much reason to use triangle intersections for a convex quad since you can treat it as an intersection of 4 half-spaces with a plane as a simple extension of a triangle-ray intersection.

The half space method would be like (unoptimised):

[source]
intersect_triangle((t0, t1, t2), (origin, direction)):
normal = (t1 - t0) cross (t2 - t0)
factor = (t0 - origin) dot normal / direction dot normal
intersection = origin + direction * factor

if ((intersection - t0) dot ((t1 - t0) cross normal) <= 0 &&
(intersection - t1) dot ((t2 - t1) cross normal) <= 0 &&
(intersection - t2) dot ((t0 - t2) cross normal) <= 0))
return (Just p)
else
return Nothing
[/source]

(Also: angles in a triangle add to 180 degrees, not 360 ;p) Edited by luca-deltodesco

##### Share on other sites
I'm not hugely familiar with any of the terms, but this method works fine for me, and its the best one I found. Also, its 360 degrees around the collision point, not in the triangle.

##### Share on other sites

I'm not hugely familiar with any of the terms, but this method works fine for me, and its the best one I found. Also, its 360 degrees around the collision point, not in the triangle.

The objections to the terminology are legitimate, and the method is also dubious. Ray-triangle intersection is an affine problem, but your solution uses trigonometry, and trigonometry requires the presence of a metric. In other words, if you need to use trigonometric functions, you are doing it wrong.

The method I've used int the past was to pick an affine reference whose origin is in vertex A of the triangle ABC and such that the first two vectors of the basis are B-A and C-A. You then find the intersection, express the result in the basis and check simply that the coordinates (x,y) along those two vectors satisfy `x>=0 && y>=0 && x+y<=1'. Edited by alvaro

##### Share on other sites
If you want to compare the non-optimised method I posted, and yours you can see that apart from the single division to find intersection on triangles-plane which you must also perform, my test involves only additions/subtractions/multiplications, whereas yours has ontop of all of that, several calls to sqrt() and acos() as well as divisions.

Your method is unstable both because of this, and that you would for a robust implementation require to check that your collision point is not very close/equal to a vertex of the triangle, or else you will have divisions by zero too.

From a numerical standpoint your test is actually against a slightly bloated triangle as points close to, but outside of an edge of the triangel would be classified as internal. etc etc.

##### Share on other sites
I think that in summary, your method mostly works but isn't the fastest or most practical approach. I can appreciate that the blizzard of terminology can be confusing. However I think that it's better to use a best-practice approach for collision testing for speed and robustness. Whether you want to put the hard yards in to understand how it works is up to you.

• 16
• 9
• 13
• 41
• 15