I have some nifty code:

Code:
float yprime = 1 - r.Direction.y;
float m = r.D2.x + r.D2.z + yprime * yprime;
float n = 2 * (r.Origin.x * r.Direction.x + r.Origin.z * r.Direction.z - r.Origin.y * yprime);
float p = r.O2.x + r.O2.y + r.O2.z;
theta = (-n/(2*m));
theta = m * theta * theta + n * theta + p;
theta = sqrtf(theta);
Yeah yeah, theta isn't being used for an angle measure, sue me


Basically I use the 3D distance formula d = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2) and plug in x1 = 0, y1 = 1, and z1 = 0 to be the y-axis. Then I put in a parametric form of a line for x2,y2,z2:

x2 = x0 + xd*t
y2 = y0 + yd*t
z2 = z0 + zd*t

This way I have a formula for my distance based purely on time.


Now I use (d/dt [distance formula]) so I can find a minimum; the derivative's zero is defined by the linear part of the equation, since the other half generated by the chain rule doesn't actually contribute any zeroes. So the zero is located at t=(-n/(2*m)) where m and n are coefficients of t^2 and t in my distance formula.


Now why the heck doesn't this give me the correct minimum distance?