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-Triangle Intersection
Triangles are the most basic polygons in geometry, as far as I know. You can split any polygon in the world into a number of triangles. How? Well, a plane can be split into two triangles, a pentagon into five triangles and so on. Triangles can even be split into triangles!

As you can see, a square, or plane, can indeed be split into two triangles. So, to achieve ray plane or sphere plane collision, we are going to have to be able to deal with ray triangle intersection, which isn't pretty hard when you look at what you have to do.
Now, first you are going to have to get a collision point relative to a plane. For this, you need the normal of the plane, and the 4 coordinates. I don't want to explain how it works, but it just does.
[source lang="cpp"]bool rayPlaneCollision(Vec3D pn, Vec3D rs, Vec3D dir, Vec3D p1, Vec3D p2, Vec3D p3, Vec3D p4) {
float a = dir.x * pn.x + dir.y * pn.y + dir.z * pn.z;
if(a == 0) {
return false;
}
float t = ((p1.x * pn.x + p1.y * pn.y + p1.z * pn.z - pn.x * rs.x - pn.y * rs.y - pn.z * rs.z)) / a;
if(t < 0) {
return false;
}
float x = rs.x + t * dir.x;
float y = rs.y + t * dir.y;
float z = rs.z + t * dir.z;
Vec3D cp(x,y,z);[/source]
where pn is the plane normal, rs is the ray source, dir is the ray direction, p1-p4 is the 4 plane coordinates. As you can see, using the ray formula, you can get the exact point of collision on the infinite plane. Now we just have to check if its inside the plane, or 2 triangles.
Now, from here on, I will assume that a collision point is on the triangle, so everything makes sense. So, in your big triangle, divide it into 3 more subtriangles with a base point of the collision point. Here's how it looks.

So, as you can see, there are three more triangles based at the collision point. Now, there are 360 degrees in a circle. Look at the collision point. How many degrees are there around it? 360 degrees.
The idea here now is to get the degrees of all the angles based at the collision point and add them all together. If they equal 360(sometimes I check for 359 on a computer because of floating point issues), then we have a collision. If we don't, well, we don't have a collision.
To achieve this, we are going to need 2 formulas, the euclidian distance formula, and 1 formula of the laws of cosine as follows below:
Euclidian Distance formula:

Laws of cosines formula

If we combine these 2 formulas, we can get exactly what we want. How? Well the variables needed in the laws of cosines formula are lengths of the lines, where a and b are connected to the collision point and c is opposite to the collision point. Well, how do we find the lengths? Euclidian distance. So we're going to have to use the euclidian distance formula 9 times and the laws of cosines formula 3 times for all three triangles.
So let's imagine some coordinates here. Let's say a triangle is at
p1(1,1) p2(3,5) p3(5,1)
And a collision point is at cp(3,2). (I hope they're plotted correctly xD) I'm drawing these on a paper as I write, so I think you should too. Now, draw the lines to each corner to create 3 subtriangles from the collision point.
So now we're going to get the angle from p1 to p2 to cp.
[source lang="cpp"]float t1a = distance(p1,cp);
float t1b = distance(p2,cp);
float t1c = distance(p1,p2);
float t1cos = acos((pow(t1a, 2) + pow(t1b, 2) - pow(t1c, 2)) / (2 * t1a * t1b)) * 180 / M_PI;[/source]
Write a distance() function to get the distance between two vectors, it isn't that hard at all. So, we are treating t1a and t1b as the lengths of the lines connected to the collision point, and t1c is the length opposite. Now we just use the laws of cosines formula, except we use arc cosine to get the angle itself. Since C++ uses radians for a degree system, I multiply by 180 and divide by Pi(3.141592653589793) to convert it to degrees, out of 360, not 3.14.
Now, we do this for each subtriangle, using the exact same process and procedures, and we should have ourselves three angle variables. If we add these 3 angle variables, and they are 360 degrees, we have a collision. Make sure to check for 359 some times.
And there you have it! Ray triangle intersection is done!