# Moving objects colliding when using unalligned collision avoidance (steering)

I'm having trouble with unaligned collision avoidance for what I think is a rare case. I have set two objects to move towards each other but with a slight offset, so one of the objects is moving slightly upwards, and one of the objects is moving slightly downwards.

In my unaligned collision avoidance steering algorithm I'm finding the points on the object's forward line and the other object's forward line where these two lines are the closest. If these closest points are within a collision avoidance distance, and if the distance between them is smaller than the two radii of the two object's bounding spheres, then the objects should steer away in the appropriate direction.

The problem is that for my case, the closest points on the lines are calculated to be really far away from the actual collision point. This is because the two forward lines for each object are moving away from each other as the objects pass. The problem is that because of this, no steering takes place, and the two objects partially collide.

Does anyone have any suggestions as to how I can correctly calculate the point of collision? Perhaps by somehow taking into account the size of the two objects?

• Note that in the screenshot, the green, red and blue lines are just the axes of the 3D world. – James Bedford Mar 16 '11 at 21:53

This is by far the best ball to ball collision detection article I have come across.

Pool Hall Lessons: Fast, Accurate Collision Detection Between Circles or Spheres

• I know how to do a sphere-sphere collision test. My problem is trying to find the position where this collision will happen in the future. Thanks. – James Bedford Mar 17 '11 at 20:43
• @James that article will solve your problems. Look at page 2 and you can use an arbitrarily high number for the "velocity" on one of your spheres to determine the collision point "in the future". – Tetrad Mar 17 '11 at 20:58
• Ok - sorry for not checking it out, it does look pretty good! I'll have to get back to you once I've read it. Thanks :) – James Bedford Mar 17 '11 at 22:03
• Could I just clarify that on page 2 under the section "Bank Shot: Collision between two moving circles", it means that you should use the difference between the two circle's velocity in the algorithm instead of the first circle's velocity? (The second circle's velocity is not used in the stationary version of the algorithm.) This bit wasn't so clear to me. Thanks. – James Bedford Mar 20 '11 at 16:18

You don't want to find the closest point.

You want to find the point on the lines where the distance is equal to the combined radii of both spheres.

• Ah... ok! Do you know how I can figure this out mathematically..? – James Bedford Mar 17 '11 at 8:41

If I'm understanding your question correctly you can simply just use a Sphere vs Sphere intersection test, like AttackingHobo has suggested.

The math to do such a test is as follows (correct me if I'm wrong, it's been awhile). Also, this is considering that your spheres have a center and a radius variable each.

The formula for checking goes something like this:

distanceOfSpheres <= sumOfRadii^2


You have a sphere vs sphere intersection. This is pretty simple, let's see what it looks like in code!

bool sphereIntersectTest(BoundingSphere* s1, BoundingSphere* s2)
{
Vector3 distance;

// Get the distance between each sphere, center is a Vector3 type
distance = (s1->center - s2-> center);

// Determine the sum of both radii

// Determine if we have an intersection