How to find the intersection point of two moving spheres? I'm writing a gas dynamics simulation. I created a two degree of freedom simulation, where all the gas molecules are represented as hard spheres and their collisions are perfectly elastic. However, I want to take that into three dimensions. I have the XYZ coordinates of all of the moving spheres, along with their vectors. By subtracting the difference between two vectors, I can essentially simplify the problem to a moving sphere colliding with a stationary sphere. We can assume that the spheres will collide because the moving sphere is calculated to approach closer than the sum of the spheres' radii.
How can I find when and where the spheres will collide?
Any thoughts would be much appreciated, and let me know if there is anything I can do to clarify the problem.
 A: Let's use a coordinate system where one of the spheres is at origin.  The other travels along a straight line in direction $\vec{p}_\Delta = (x_\Delta, y_\Delta, z_\Delta)$, at $\vec{p}_0 = (x_0, y_0, z_0)$ at time $t = 0$, and at $\vec{p}_0 + t \vec{p}_\Delta = (x_0 + t x_\Delta, y_0 + t y_\Delta, z_0 + t z_\Delta)$ at time $t$.  Let $R$ be the sum of the two spheres radiuses, i.e. the distance of the two spheres centerpoints we are interested in.  At the moment when the two spheres touch, we have
$$\begin{aligned}
\lVert \vec{p}_0 + t \vec{p}_\Delta \rVert &= R \\
\iff \quad \lVert \vec{p}_0 + t \vec{p}_\Delta \rVert^2 &= R^2 \\
\iff \quad (x_0 + t x_\Delta)^2 + (y_0 + t y_\Delta)^2 + (z_0 + t z_\Delta)^2 &= R^2 \\
\iff \quad (x_\Delta^2 + y_\Delta^2 + z_\Delta^2) t^2 + 2 (x_0 x_\Delta + y_0 y_\Delta + z_0 z_\Delta) t + (x_0^2 + y_0^2 + z_0^2 - R^2) &= 0 \\
\end{aligned}$$
This is a quadratic equation in $t$, and easily solved:
$$\begin{aligned}
t = &- \frac{x_0 x_\Delta + y_0 y_\Delta + z_0 z_\Delta}{x_\Delta^2 + y_\Delta^2 + z_\Delta^2} \\
~ &\pm \frac{\sqrt{R^2 ( x_\Delta^2 + y_\Delta^2 + z_\Delta^2) - (x_0 y_\Delta - y_0 x_\Delta)^2 - (x_0 z_\Delta - z_0 x_\Delta)^2 - (y_0 z_\Delta - z_0 y_\Delta)^2}}{x_\Delta^2 + y_\Delta^2 + z_\Delta^2} \\
\end{aligned}$$
Obviously, only positive $t$ are interesting, since only those correspond to collisions in the future.
The denominator, $\lVert \vec{p}_\Delta \rVert^2 = x_\Delta^2 + y_\Delta^2 + z_\Delta^2$, is only zero when the two spheres are stationary with respect to each other (i.e., travelling in the same direction with the same velocity).
The expression within the square root is nonnegative only if the trajectories are such that the two spheres do touch/intersect at some point.

If you have two spheres traveling along straight lines, $\vec{a}_0 + t \vec{a}_\Delta$ and $\vec{b}_0 + t \vec{b}_\Delta$, the position of the second sphere ($\vec{b}_0 + t \vec{b}_\Delta$) in a coordinate system where the first sphere is at origin is
$$\left\lbrace ~ \begin{aligned}
\vec{p}_0 &= \vec{b}_0 - \vec{a}_0 \\
\vec{p}_\Delta &= \vec{b}_\Delta - \vec{a}_\Delta \\
\end{aligned} \right.$$
Calculating the expression in the square root above,
$$D = R^2 ( x_\Delta^2 + y_\Delta^2 + z_\Delta^2) - (x_0 y_\Delta - y_0 x_\Delta)^2 - (x_0 z_\Delta - z_0 x_\Delta)^2 - (y_0 z_\Delta - z_0 y_\Delta)^2$$
is enough to tell you if the two spheres' trajectories may intersect: If $D \lt 0$, there is no intersection.  If $D = 0$, there is one intersection.  If $D \gt 0$, there are two intersections.  You do need to calculate $t$ as above, to find out if the intersection(s) occur in the future, though.
A: The spheres will collide when their centers are a distance apart equal to the sum of their radii. The collision point is a point on the line between the two centers. If you have the vector coordinates of these centers that should be relatively easy to figure out.
A: $$\|(\vec c_0+t\vec v_0)-(\vec c_1+t\vec v_1)\|=r_0+r_1$$
or
$$(\vec{\Delta c}+t\vec{\Delta v})^2=\vec{\Delta c}^2+2\vec{\Delta c}\vec{\Delta v}\,t+\vec{\Delta v}^2\, t^2=(r_0+r_1)^2.$$
Solve for $t$.
