Go to index of notes. ~ Go to home page.

How to calculate where 2 spheres collide... or, actually, how to calculate the proportion of a sphere's theoretical travel where the impact happens (which you could then use to find the position). This is only intended for use when spheres have linear paths, constant velocity, and are sampled within the same time frame.

What I mean by 'a sphere's theoretical travel' is its full linear travel within the chosen time period (typically a video frame period) as if it did not collide with anything, slow down or speed up.

This method can also be used for circles on a 2D plane (e.g. in a 2D game).

One piece of information you need to feed-into this method is the distance the two objects' centres are appart when they are closest to each other -- to obtain this information you can use my method detailed in NOTES10#. That method will give you where in the objects' travel they come closest, as a proportion of their travel ... then if you know the co-ordnates of their start and end points then you can find that point as a co-ordinate on each object's travel. something similar to the following would do:-

   line1_closest_point_x = ( line1_start_x * ( 1.0 - proportion ) ) + ( line1_end_x * proportion );
   line1_closest_point_y = ( line1_start_y * ( 1.0 - proportion ) ) + ( line1_end_y * proportion );
   line1_closest_point_z = ( line1_start_z * ( 1.0 - proportion ) ) + ( line1_end_z * proportion );

   line2_closest_point_x = ( line2_start_x * ( 1.0 - proportion ) ) + ( line2_end_x * proportion );
   line2_closest_point_y = ( line2_start_y * ( 1.0 - proportion ) ) + ( line2_end_y * proportion );
   line2_closest_point_z = ( line2_start_z * ( 1.0 - proportion ) ) + ( line2_end_z * proportion );
(omit the z ordinates if you're working in 2D)

In the diagram the travel of each sphere is measured between 0.0 and 1.0 -- this is the proportion of each spheres full projected travel in a frame and not any particular unit like cm.

Suggested use of my method in NOTES10# and this method:-
for each new frame in your game, and for each sphere, store the current position and calculate the new position of the your sphere (e.g. a ball) -- thus you have a new point, and a previous point ( marked as 'prev_point' on the below diagram, and marked as 'START' in the diagram in NOTES10#) for each sphere. Then you can compare each shpere with any other sphere within range by using the formula, and you can see if, and where, any spheres collide before you render your new frame, and deflect it as appropriate. How you deflect it is a whole other issue of course.

Note that this is an approximation method... but it produces such good results that I've not had to get around to developing a new one yet... but I do intend to develop a more accurate method (which would be important for some applications).