# Sweep collision between two moving spheres?

Say I have a scenario like this:

Here I got two spheres (sphere A and B) that moves from on spot to another during a frame. How can I predict where these two collide if they collide at all? In between these frames?

I tried to look it up and found this page, but I didn't understand much of it. Could someone try to explain this to me? I'm coding this in C# with XNA and I've been stuck with this issue for a very long time now

• I found a great explination of my problem here! This article even came with an great example in XNA, I recommend people with the same problem to read it! – user1974555 Mar 19 '14 at 13:19

Change the frame of reference and everything will become wonderfully simple.

You currently have:

• sphere A (position Pa, velocity Va, radius Ra)
• sphere B (position Pb, velocity Vb, radius Rb)

In terms of collision detection, this is equivalent to:

• sphere A' (position at origin, velocity zero, radius Ra + Rb)
• point B' (position Pb - Pa, velocity Vb - Va)

Now you just need to check whether (and when) the point hits the sphere. This becomes a simple segment/sphere intersection test. If there is a collision, the change in the frame of reference doesn’t affect the collision time t_coll. You can then compute the new positions of the two original spheres: Pa + Va * t_coll and Pb + Vb * t_coll.

The linear segments

A(u) = A0 + u * Va

and

B(u) = B0 + u * Vb

are just parametric representations of the segments Va = (A0, A1) and Vb = (B0, B1). You should recall that the vector can be represented by the difference of the end points. That's why Va = A1 - A0 and Vb = B1 - B0.

A(u) and B(u) are said to be parametric because as you vary the parameter u from 0 to 1 you get points A(u) or B(u) that will build the segment Va or Vb (respectively) from the starting point A0 or B0 (respectively) to the the end point A1 or B1 (respectively). You can see that by thinking of the cases where u = 0, where A(u) = A0 and B(u) = B0 (starting points) and where u = 1, where A(u) = A1 and B(u) = B1 (end points). Any value of u between 0 and 1 will result in points between A0 and A1 or B0 and B1 (respectively).

The expression

( B(u) - A(u) )* ( B(u) - A(u) )

is just the dot product of the vector that represents the distance of the points B(u) and A(u) with itself. The formula of the squared distance between two points is

d^2[ A(u), B(u) ] = ( xA(u) - xB(u) )^2 + ( yA(u) - yB(u) )^2

Derivation come from the Pythagorian theorem and can be seen here. By the definition of the dot product, we see that

( B(u) - A(u) ) * ( B(u) - A(u) ) = d^2[ A(u), B(u) ]

, since the dot product sums the product of the components of the vector.

The last statement of the article is that when the collision occurs

[ B(u) - A(u) ] * [ B(u) - A(u) ] = ( ra + rb ) ^ 2

where ra and rb are the radii of the spheres. What this expression means is that the first time of collision occurs when the distance between A(u) and B(u) is equals to the distance between the centers of the spheres. This is true because ra + rb represents the distance of the centers when the spheres are side-by-side. It is squared just because the other side of the equation also is, so they are in the same measuring unit.