Part 2  Ray/Plane Intersection
Collision Detection Tutorial
This tutorial will show you how to create a function that calculates the distance between a given ray (source point and a vector) and an infinite plane.
This figure illustrates what's needed to be calculated.
The little blue arrow is our ray, it points to the plane. What we need is it's distance to the plane  the length of the yellow vector (which of course starts at our ray's origin).
Here's the prototype for our function: (it returns the distance to the plane, or a negative value if there's no intersection).
inline float distRayPlane(vec3 vRayOrigin,
vec3 vnRayVector,
vec3 vnPlaneNormal)
float planeD)
{
...
}
How to do that?
We start by calculating the angle between the plane normal and the ray vector.
Since these vectors are already normalized, there shouldn't be a problem getting the angle between them.
float cosAlpha = DotProduct(
vnRayVector , vnPlaneNormal );
Notice: if cosAlpha equals to zero (alpha=90) then the ray will never intersect with the plane.
The next thing we need to do is to find delta D, which is the plane's distance from the origin minus the ray's distance from the origin.
float deltaD = planeD 
DotProduct(vRayOrigin,vnPlaneNormal);
Now, with some basic trigonometry we can find the yellow line's length by dividing deltaD with cosAlpha.
Return values
Our function returns a float that's specifying the ray's distance to the plane.
If the ray does not intersect with the plane (if it's parallel to the plane, or points the other way) we would expect the return value to be negative.
We have a math flaw here, if the ray is parallel to the plane, cosAlpha would be zero. We can't divide by zero (actually we can, we'll receive an inifinite value), so will have to settle with a false value, that can be any negative number.
The final code
// find the distance between a ray and a plane.
inline float distRayPlane(vec3 vRayOrigin,
vec3 vnRayVector,
vec3 vnPlaneNormal,
float planeD)
{
float cosAlpha;
float deltaD;
cosAlpha = DotProduct(
vnRayVector , vnPlaneNormal );
// parallel to the plane (alpha=90)
if (cosAlpha==0) return 1.0f;
deltaD = planeD 
DotProduct(vRayOrigin,vnPlaneNormal);
return (deltaD/cosAlpha);
}
Part 1 of the Tutorial can be found here.
Collision Detection Tutorial  Part 1: Sphere/Plane intersection
