Detect Collision Point of Two Spheres?

224 Views Asked by At

Let's suppose I have two spheres of equal radii in a 3D space, one that's moving along a straight line with a constant speed, and one that's stationary, as such:

enter image description here

Where $d$ is the straight line along which the moving sphere is moving, and $r$ is the radii of the two spheres.

I want to be able to find at what point on the line $d$ the moving sphere will collide with the stationary sphere, if any. That is, I wish to find at what point on the line $d$ the distance between the centers of the two spheres will first be equal to $2r$.

I thought of a couple of ways of solving this, but wasn't able to arrive at a conclusive answer by myself;


The first method of solving that I thought of, if I were attempting this on a computer, would be to implement a brute-force method where I slowly check whether the distance between the centers of the two spheres is less than or equal to $2r$ at certain small intervals along the line $d$. At the first instance where that distance is less than or equal to $2r$, I would consider the two spheres as "collided", and just round to the previous point on the line. However, I'm looking for something a bit more accurate than this method.


Another method of solving that I thought of, was to model the distance between the centers of the two spheres as a function of the moving sphere's position along the $d$ line, and model it after some sort of quadratic function:

enter image description here

Then hypothetically, one would be able to insert different arguments to said quadratic function depending on the angle between the two spheres, the speed of the moving sphere, the distance of the $d$ line, or some other parameters. Then one would hypothetically be able to find the first collision point by just solving the quadratic equation with the desired arguments for $y = 2r$, and just take the first $x$ solution as the answer. However, that would theoretically still provide me with an approximate answer, not an exact one.


The last thing I thought of was to decompose this into a 2D problem, and see how I could solve that. So, let's suppose we have the same problem in a 2D space instead of a 3D one:

enter image description here

I'm able to find the collision point along the line $d$ of the two circles depicted above in a 2D space by doing the following steps:

  1. Find the point on the line $d$ that's closest to the center of the stationary circle.
  2. Check whether the distance between the point on line $d$ found in the previous step and the center of the stationary circle is less than or equal to $2r$ (if it isn't, then there's no collision between the two circles at all).
  3. Construct a right-triangle on the line $d$, with the hypotenuse attaining the length of $2r$ and one of the sides being the distance between the point on the line $d$ found in the first step and the center of the stationary circle.
  4. The remaining side of the constructed right-triangle, one the line $d$, is subtracted from the segment of the line $d$ prior to the closest point found in the first step, to attain the final distance traveled by the moving circle until collision.

enter image description here

While the steps above appear to work for a collision between two circles in a 2D space, I'm struggling to see exactly how this would be brought to a 3D space. I'm assuming it would involve decomposing the 3D space into several 2D planes along each one of the axes, but I may be wrong.


Thanks for reading my post, any guidance is appreciated.

1

There are 1 best solutions below

0
On

You are making things far more complicated than they need to be. The best solution is your second one, but it can easily be solved directly. This searching concept you describe is unnecessary.

You know how the moving sphere is moving. There is some function $$t \mapsto (x(t), y(t), z(t))$$ where $t$ is time, and $(x(t), y(t), z(t))$ is the location of the center of the moving sphere at time $t$. For instance in the scenario you describe in the comment, $(x(t), y(t), z(t)) = (0, 1, 8t)$. Since you say the moving sphere is always moving at constant speed, you can expect that the functions $x(t), y(t), z(t)$ will always be lines: $$x= m_xt + b_x, y = m_yt + b_y, z = m_zt + b_z$$ for some constants $m_x, m_y, m_z, b_x, b_y, b_z$. The technical term for such functions is "affine", with "linear" being reserved for when $b_x = 0, b_y = 0, b_z = 0$.

Now as you've noticed, the spheres collide exactly when their centers are at a distance of $2r$ from each other. If the stationary sphere has center $(x_0, y_0, z_0)$, this is the equation $$\sqrt{(x(t) - x_0)^2 + (y(t) - y_0)^2 + (z(t) - z_0)^2} = 2r$$ or squaring both sides $$(x(t) - x_0)^2 + (y(t) - y_0)^2 + (z(t) - z_0)^2 = 4r^2$$ Since the functions $x(t), y(t), z(t)$ are affine, this is exactly a quadratic function in $t$. And by subtracting the $4r^2$ from both sides and simplifying, you get a quadratic equation in $t$: $$At^2 + Bt + C = 0$$ There is no need to hunt for solutions. The solutions of quadratic equations are well-known: $$t = \dfrac{-B \pm \sqrt{B^2 - 4AC}}{2A}$$ If $B^2 - 4AC < 0$, then the equation has no real solution. This means the two spheres do not hit each other. If $B^2 - 4AC = 0$, the solution is $t = \frac {-B}{2A}$. At that time the two spheres will just touch, but then they separate without any actual harm. If $B^2 - 4AC > 0$, you get two values for $t$. The lower value (where you choose to take the "$\pm$" as "$-$") will be the time when they collide (the other time being where they would separate if they could pass through each other).

In your sample case $(x_0, y_0, z_0) = (0,0,2)$, so the equation for $t$ is $$(0-0)^2 + (1-0)^2 + (8t - 2)^2 = 4(4)^2\\ 1 + 64t^2 - 32t + 4 = 64\\ 64t^2 - 32t - 59 = 0$$

By the quadratic formula (where I will just use the $-$ option, since that is the one we want), $$t = \frac{-(-32) - \sqrt{(-32)^2 - 4(64)(-59)}}{2(64)} = \frac{32 -16 \sqrt{63}}{128} = \frac{2 - 3\sqrt{7}}8 \approx -0.74$$

So at about $0.74$ of a second before reaching your "starting point", the two spheres have already collided (your starting point and fixed sphere center are $\sqrt 5 < 8$ meters apart).

The point on $d$ of the center of the moving sphere when they collide can then be obtained by putting $t$ back in the function for the center. In your example, this is about $(0,1,8(-0.74)) = (0,1,-5.92)$.