Check if my trajectory colliding another objects I'm new to Math.stackechange and i'm a programmer not a mathematician :-(.
I'm solving problem in 3D engine for a computer game.
But this time i need to do calculations on server side, matemathically, I can't use a OpenGL.
I have trajectory of a ship from A to B in a Space. 
A=Vector3(x,y,z) 
B=Vector3(x,y,z) 

I need to calculate, if my trajectory is colliding with some planets.
For each planet i have its Position (Vector3(x,y,z)) and a Radius.
My humble try to make solution:
B-A = C //(trajectory vector)
i = C/length_of_C

and loop i-times:
C*i+A // and test if it is close to any planet
But distances are so huge (bilions of Km) and planets are too small (thousands of km). That on trajectory Earth <> Pluto i need to repeat this loop a milion times. That is not possible.
It is possible to calculate for each planet distance to nearest point of trajectory A=>B ?
 A: This is the three-dimensional version of Shortest distance between two objects moving along two lines with one of the velocities set to zero.
If the spaceship travels in a straight line from
$A(x_A,y_A,z_A)$ to $B(x_B,y_B,z_B)$ and
your planet is at the coordinates $P(x_P,y_P,z_P)$,
define the vector $V(x_V,y_V,z_V) = B - A$ 
and the vector $U(x_U,y_U,z_U) = P - A$.
In vector arithmetic, what we do is to project $U$ onto $V$,
obtaining a vector $U_V$, and then
subtract $U_V$ from $U$, which leaves a vector $U_\perp$
perpendicular to $V$.
Then $U_\perp$ is the vector to $P$ from $A + U_V$,
which the closest point to $P$ along the line of $V$.
It's also convenient to define an auxiliary variable $t_{\min}$
so that the calculations go as follows:
\begin{align}
t_{\min} &= \frac{U \cdot V}{\lVert V \rVert^2},\\
U_V &= t_{\min} V, \\
U_\perp &= U - U_V.
\end{align}
If $A + U_V$ is between $A$ and $B$ then $\lVert U_\perp\rVert$
is the distance of closest approach to $P$ on the path from $A$ to $B$.
That occurs when $0 < t_{\min} < 1$.
In that case you want to compare $\lVert U_\perp\rVert$
to the radius of the planet to see if you "hit" it.
If $t_{\min} \leq 0$ then $A$ is the closest
point to $P$ and if $t_{\min} \geq 1$
then $B$ is the closest point to $P$ along the path from $A$ to $B$.
In practice, it is usually faster to compute $\lVert U_\perp\rVert^2$
than to compute $\lVert U_\perp\rVert$,
so if the planet's radius is $r$, you might try testing whether
$\lVert U_\perp\rVert^2 \leq r^2$
to see if you "hit".
Depending on your vector arithmetic library, if a "dot product" function
is defined then that is probably how you would get $U \cdot V$.
Then $\lVert V \rVert^2 = V \cdot V$
and $\Vert U_\perp \rVert = \sqrt{U_\perp \cdot U_\perp}$.
The implementation of the "dot product" function should be something
equivalent to $U \cdot V = x_U x_V + y_U y_V + z_U z_V$
in case you need to implement it yourself.

By the way, if the planet is moving, but both the spaceship and planet
are moving along straight lines at constant speed,
you can subtract the planet's velocity vector from the spaceship's
velocity vector to get the vector $V$, and proceed as before.
A: Edit: Sorry for slightly changing the notation in your question. In this answer, $\vec C$ is the position of the planet.
The line which goes through $\vec{A}$ and $\vec{B}$ can be parameterized as $$\vec{l}(t) = t(\vec{B}-\vec{A}) + \vec{A}$$ Here $t$ is any real number and $\vec{l}(t)$ is any point on the line. Assuming the ship starts at $\vec{A}$ and ends at $\vec{B}$, the trajectory should be taken only for $0\leq t \leq 1$.
The point on this line which is closest to $\vec{C}$ will be the point $\vec{l}(t)$ where the vector from $\vec{l}(t)$ to $\vec{C}$ is perpendicular to the line, i.e. perpendicular to $\vec B - \vec A$. In other words, we need to solve the equation $$(\vec C - \vec l(t)) \cdot (\vec B - \vec A) = 0$$ which can be rewritten as $$\vec C\cdot (\vec B-\vec A) - t|\vec B - \vec A|^2 - \vec A \cdot (\vec B-\vec A) = 0$$ $$t = \frac{(\vec C-\vec A)\cdot (\vec B-\vec A)}{|\vec B - \vec A|^2}$$ If $0\leq t\leq 1$, then this is actually a point on the ship's trajectory. In that case, the closest distance between the trajectory and the planet is $|\vec C - \vec l(t)|$ for the above value of $t$. If $t < 0$, then the closest point on the trajectory to the planet is actually $\vec A$ itself (the ship is flying away from the planet), so this distance is $|\vec C - \vec A|$. If $t > 1$, then $\vec B$ is the closest point and the distance is $|\vec C - \vec B|$ (the ship stops before getting closer to the planet).
