Intersection of a ray and a plane

I previously showed the derivation of how to determine the intersection of a plane and a cone. At the time I had to solve that equation, so after doing so I decided to publish it for anyone to use. Given the positive feedback, it seems this was useful, so I might as well continue with a few more.

Here is probably the most basic intersection: a ray and a plane. Solving it is straightforward, which I hope can be seen below. Like last time, I am using vector notation.

  1. We define a ray with its origin O and its direction as a unit vector D^.
    Any point X on the ray at a signed distance t from the origin of the ray verifies: X=O+tD.
    When t is positive X is in the direction of the ray, and when t is negative X is in the opposite direction.
  2. We define a plane with a point S on that plane and the normal unit vector N^, perpendicular to the plane.
    The distance between any point X and the plane is d=|(XS)N|. If this equality is not obvious for you, you can think of it as the distance between X and S along the N direction. When d=0, it means X is on the plane itself.
  3. We define P the intersection or the ray and the plane, and which we are interested in finding.

Since P is both on the ray and on the plane, we can write: {P=O+tD|(PS)N|=0 Because the distance d from the plane is 0, the absolute value is irrelevant here. We can just write: {P=O+tD(PS)N=0 All we have to do is replace P with O+tD in the second equation, and reorder the terms to get t on one side.
(O+tDS)N=0 ON+tDNSN=0 tDN=SNON t=(SO)NDN

A question to ask ourselves is: what about the division by 0? Looking at the diagram, we can see that DN=0 means the ray is parallel to the plane, and there is no solution unless O is already on the plane. Otherwise, the ray intersects the plane for the value of t written above. That’s it, we’re done.

Note: There are several, equivalent, ways of representing a plane. If your plane is not defined by a point S and a normal vector N^, but rather with a distance to the origin s and a normal vector N^, you can notice that s=SN and simplify the result above, which becomes: t=sONDN


Signed distance to a plane

For the sake of simplicity, in the above we defined the distance to the plane as an absolute value. It is possible however to define it as a signed value: d=(XS)N. In this case d>0 means X is somewhere on the side of the plane pointed by N, while d<0 means X is on the opposite side of the plane.

Distances that can be negative are called signed distances, and they are a foundation of Signed Distance Fields (SDF).