Dynamic Sphere vs Sphere Collision
The other day I was working on a small project that involved detecting a collision time between two moving spheres. At first sight this can get a bit complicated, but there are a few things we can do to simplify the problem. The idea here is to retrieve the exact time (or none) that the spheres will collide. One limitation is that this will work for objects with a linear motion (aka. no acceleration).
So let’s say we have the following scenario:
We want to figure out the time where A and B will collide. The first simplification we can do is make the velocity relative between the spheres and in essence make one of the spheres static:
$ V_A = V_A - V_A \Rightarrow V_A = 0$
$ V_B = V_B - V_A$
From here, we can use another trick to simplify the problem, that is, we can convert the moving sphere into a ray, by adding its radius to the static one, this will make the problem into a Ray VS. Sphere collision.
$ R_A = R_A + R_B $
$ R_B = R_B - R_B \rightarrow R_B=0 $
This trick is covered in: https://www.realtimerendering.com/intersections.html
Ray vs Sphere
Finally, we are left with a fairly simple problem to solve, we just need to perform an intersection check to retrieve the distance, from there we can just derive the time to impact using the velocity of the “moving ray”.
For reference, we can calculate the intersection point geometrically:
We can perform some early checks in this code, for example, if h is bigger than R, we don’t have an intersection.